File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Trouble with packages! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Trouble with packages!" Watch "Trouble with packages!" New topic

Trouble with packages!

Stephanie Keeler

Joined: Jan 04, 2010
Posts: 6
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 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$ Source)
at Method)
at 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\;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\;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

Joined: Jan 04, 2010
Posts: 6
Just to add to this, I tried 'java HelloWorld' too in the same directory. Didn't work.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Go to your C directory and type java world.Helloworld

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Stephanie Keeler

Joined: Jan 04, 2010
Posts: 6
Boy, do I feel stupid now. Thank you very much!
David Newton

Joined: Sep 29, 2008
Posts: 12617

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.
I agree. Here's the link:
subject: Trouble with packages!
It's not a secret anymore!