Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Main not found, but it exists!

 
Frank Serkland
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Java tutorial about Package
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 241
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 =)
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Frank Serkland
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
None of the above seems to be working. I'm lost for what else to do. Any thoughts?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49447
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Delete (or better //comment out) the package declaration and start again.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49447
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lester Burnham wrote:Is the PrimeSieves.class file in the C:\Java programs\Java\bin\com\math directory?


That is correct.
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Frank Serkland
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49447
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic