Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trouble with packages!

 
Stephanie Keeler
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, all, I'm new to the site and to Java, trying to learn by self-study and I could really do with some help! I can't seem to get packages working. I won't go through what I did originally to try to get my code to package, but instead explain that I went through the tutorial found here. I'm working off my laptop, running Windows XP, and what I've done is as follows:

1. Created a directory C:\world
2. Saved the source code into this directory as per instructions
(source code as follows, copied direct from site so as to avoid inexperienced-newbie mistakes)

3. At command line, changed directory until I was at C:\world and typed javac HelloWorld.java. No apparent problems compiling.
4. At command line, still in C:\world, typed java world.HelloWorld as per instructions. The compiler replies as follows:
C:\world>java world.HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: world/HelloWorld
Caused by: java.lang.ClassNotFoundException: world.HelloWorld
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: world.HelloWorld. Program will exit.

5. Wondering if this is an operating system thing, try with a slash instead of a dot. Nope.
6. Double-check classpath. Classpath is set to .;..;C:\Program Files\Java\jre1.5.0\lib\ext\QTJava.zip;C:\Documents and Settings\Stephanie\My Documents\JavaDev
7. Add to classpath, so it is now .;..;C:\Program Files\Java\jre1.5.0\lib\ext\QTJava.zip;C:\Documents and Settings\Stephanie\My Documents\JavaDev;C:\world
8. When this doesn't work, despair, followed by posting on this forum.

(as a note, the code I was working on and trying to package included imports, which is what I thought I was doing wrong first.)
 
Stephanie Keeler
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to add to this, I tried 'java HelloWorld' too in the same directory. Didn't work.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go to your C directory and type java world.Helloworld
 
Stephanie Keeler
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Boy, do I feel stupid now. Thank you very much!
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, note that "classpath" and "path" are two different things.

"Path" is an OS-level concept and (simplistically) indicates to the shell where it should look for programs to run. The "classpath", by contrast, is a Java-level concept, and (also simplistically) indicates to the JVM where it should look for classes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic