• Post Reply Bookmark Topic Watch Topic
  • New Topic

Error: Could not find or load main class HelloWorldApp  RSS feed

 
Dian D Chen
Greenhorn
Posts: 10
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I am new to Java.
I just followed the tutorial here :
http://docs.oracle.com/javase/tutorial/getStarted/cupojava/win32.html

After running "java HelloWorldApp", I got "Error: Could not find or load main class HelloWorldApp".

Check my attachments for details.

According to "http://docs.oracle.com/javase/tutorial/getStarted/problems/index.html",


Could not find or load main class HelloWorldApp.class

A common mistake made by beginner programmers is to try and run the java launcher on the .class file that was created by the compiler. For example, you'll get this error if you try to run your program with java HelloWorldApp.class instead of java HelloWorldApp. Remember, the argument is the name of the class that you want to use, not the filename.


But apparently I didn't add ".class" at the end of the command. Why do I still get this error?

Thanks for the help!
helloworld_code.JPG
[Thumbnail for helloworld_code.JPG]
Code
helloworld_javac.JPG
[Thumbnail for helloworld_javac.JPG]
Run javac
helloworld_java.JPG
[Thumbnail for helloworld_java.JPG]
Java Error
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Do you have a CLASSPATH environment variable set?

Henry
 
Dian D Chen
Greenhorn
Posts: 10
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I have added it to sys path.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dian D Chen wrote:Yeah, I have added it to sys path.



For what you are doing, you should *NOT* have a CLASSPATH set. But if you must set one, then it should include the current directory for what you are doing to work.

Henry
 
Dian D Chen
Greenhorn
Posts: 10
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What should I setup in CLASSPATH? I saw some .jar directories in my CLASSPATH.

I just added the java bin location to PATH sys variable.
 
Dian D Chen
Greenhorn
Posts: 10
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my current path, I can do "javac".

syspath_javac.JPG
[Thumbnail for syspath_javac.JPG]
Sys Path Javac
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dian D Chen wrote:What should I setup in CLASSPATH? I saw some .jar directories in my CLASSPATH.

I just added the java bin location to PATH sys variable.



As mentioned, just make sure that the current directory is part of the CLASSPATH. Or better yet, don't set it at all... And BTW, CLASSPATH and PATH are NOT the same thing. I meant CLASSPATH, and not the system PATH variable.

Henry
 
Dian D Chen
Greenhorn
Posts: 10
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As your suggested, I didn't touch the CLASSPATH. I thought you meant PATH before. But now how can I make the code work?
 
Dian D Chen
Greenhorn
Posts: 10
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Errr.. sorry, I am bit confused here. Do you mean that I should run my code under the path which is set in my PC default CLASSPATH?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally, most beginners should simply NOT have a CLASSPATH environment variable set AT ALL. It should NOT exist.

If you DO have to have it, it should list the directories where you want Java to look for .class files. And you should know Java will ONLY look in the directories listed there. If you want java to look in whatever is the "current directory", you need to ADD that to your classpath. And the way you say "look in the current directory" is to put a dot. It needs to be separated like all the other directories, so depending on your OS, you use either a semicolon or a colon.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dian D Chen wrote:As your suggested, I didn't touch the CLASSPATH. I thought you meant PATH before. But now how can I make the code work?



Yeah, we kinda figured out that you didn't touch CLASSPATH -- since you didn't even know what it was... However, there is a chance that it was set due to something else that was installed earlier. If so, either unset it, if you don't care about the other installation; or make sure that the current directory is on the CLASSPATH.

Henry
 
Dian D Chen
Greenhorn
Posts: 10
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies.

Since the CLASSPATH contains some variables, I don't want to change them.

After Google some articles about CLASSPATH concept, I work out with this command:

java -cp . HelloWorldApp

Now it works.

Thank you so much.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just add a dot to your CLASSPATH.
Suppose your CLASSPATH is (I'm using Windows style):

c:\foo\bar\baz.jar;c:\foo\baz\bar.jar;c:\foo\

Then you need to make it:

.;c:\foo\bar\baz.jar;c:\foo\baz\bar.jar;c:\foo\

The dot means "current directory".
If you do this you won't need to run your example with "-cp ." anymore
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!