Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Main not found, but it exists! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Main not found, but it exists!" Watch "Main not found, but it exists!" New topic
Author

Main not found, but it exists!

Frank Serkland
Ranch Hand

Joined: Jun 21, 2010
Posts: 104
The following code compiles correctly and runs from Eclipse, but not from a DOS command line. I get this error message.



Here's the problem: below is the code, showing a main method! I know it's in the correct place because it runs from Eclipse. Shouldn't it also run from the DOS prompt as well? I'm typing java PrimesSieve. Why doesn't it work?

Raymond Tong
Ranch Hand

Joined: Aug 15, 2010
Posts: 230
    
    2


Java tutorial about Package
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Hi Frank,

it's not the main() method which causes this error message - it's the wrong class

You have to use the full qualified class name including your package!



Marco

PS: Oops, too late
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
Have you compiled you class by using javac PrimesSieve.java? You have to the in the directory the file is in. It will produce PrimesSieve.class than can be then run by invoking java PrimesSieve.

edit: well this shoot missed the target =)


SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Also note that the error message says nothing about a "main" method (it seems like that's how you interpreted it), but instead talks about a "main" thread:

Exception in thread "main" java.lang.NoClassDefFoundError: PrimesSieve

As mentioned before, the actual problem is the latter part: there is no class PrimesSieve - it's com.math.PrimeSieve.
Soumyajit Hazra
Ranch Hand

Joined: Jun 26, 2007
Posts: 136
Copy the code in any location, for example in D:\test.Now compile the class by the following command
You will see in the current location a directory structure will be created (com\math) and inside the math directory the PrimesSieve.class present.Now run the code from D:\test location by command. Better move the source file which is also in the D:\test to some other place. Your current location should be in classpath. Have a try.

Java Programmer | SCJP 1.5 | SCWCD 1.4
Frank Serkland
Ranch Hand

Joined: Jun 21, 2010
Posts: 104
None of the above seems to be working. I'm lost for what else to do. Any thoughts?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37953
    
  22
Delete (or better //comment out) the package declaration and start again.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Frank Serkland wrote:None of the above seems to be working. I'm lost for what else to do. Any thoughts?


I think that you have to tell us specifically what you've tried, what the results have been. Else we'll have no idea what you're doing wrong now, and will only be able to repeat what has been stated above.
Frank Serkland
Ranch Hand

Joined: Jun 21, 2010
Posts: 104
pete stein wrote:I think that you have to tell us specifically what you've tried, what the results have been. Else we'll have no idea what you're doing wrong now, and will only be able to repeat what has been stated above.


I tried this:



and I still got the same error message.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Is the PrimeSieves.class file in the C:\Java programs\Java\bin\com\math directory?

By the way, you shouldn't keep any of your source code or class files in any od the JDK installation directories. That's an accident just waiting to happen.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37953
    
  22
Specifically means specifically. Tell us some details. For example, which directory is your PrimeSieve.java file in, and which directory is the corresponding .class file in?
Frank Serkland
Ranch Hand

Joined: Jun 21, 2010
Posts: 104
Lester Burnham wrote:Is the PrimeSieves.class file in the C:\Java programs\Java\bin\com\math directory?


That is correct.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
OK. And there is no other PrimeSieves.class file anywhere, the source file still has a "package com.math;" and you're still getting precisely the error "java.lang.NoClassDefFoundError: PrimesSieve"?
Sudipta Laha
Ranch Hand

Joined: Aug 23, 2010
Posts: 49
Try using
java -cp . <filename>

go to the directory
C:\Java programs\Java\bin\
the java file should be present in this path

javac -d . PrimesSieve.java

After that try
java -cp . com.math.PrimesSieve


Regards,
Sudipta Kumar Laha
Frank Serkland
Ranch Hand

Joined: Jun 21, 2010
Posts: 104
Sudipta Laha wrote:Try using
java -cp . <filename>

go to the directory
C:\Java programs\Java\bin\
the java file should be present in this path

javac -d . PrimesSieve.java

After that try
java -cp . com.math.PrimesSieve



This finally worked for me. Thanks for everyone's help.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37953
    
  22
If the -cp . option is working, you have probably got an incorrect system CLASSPATH. Did you set your CLASSPATH yourself? That is usually a mistake. If you didn't, edit it by adding .; to its beginning.
 
Don't get me started about those stupid light bulbs.
 
subject: Main not found, but it exists!
 
Similar Threads
read next byte
Java vs. Perl (Speed)
Sieve of Eratosthenes
Java Code puzzle
Faster Sum of Prime Numbers