This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error when running packaged code

 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

For the past two hours, I have been trying to follow code from Head First Java, page 598, and I keep getting error when running the code. I've attached the directory structure, directory_structure.png

Also, here is the code for PackageExercise.java.



I make sure I am in the source directory and do the following:

rsaska@dell-desktop:~/Documents/JavaExamPrep/myProject/source$ javac -d ../classes com/headfirstjava/PackageExercise.java

The compiler is silent. Then according to the book I go to the classes directory, and run the code as shown below:


rsaska@dell-desktop:~/Documents/JavaExamPrep/myProject/classes$ java com.headfirstjava.PackageExercise

This is the error I get:

Exception in thread "main" java.lang.NoClassDefFoundError: com/headfirstjava/PackageExercise
Caused by: java.lang.ClassNotFoundException: com.headfirstjava.PackageExercise
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:319)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:264)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:332)
Could not find the main class: com.headfirstjava.PackageExercise. Program will exit.


But here is the best part; I can use Ubuntu GUI to navigate to classes -> com -> headfirstjava -> PackageExercise.class. However, when I use terminal, I can only go through classes -> com; it says headfirstjava doesn't exist.

By the way, when type echo $CLASSPATH in the terminal, I get ../classes


Please help
directory_structure.png
[Thumbnail for directory_structure.png]
 
Rajeev Rnair
Ranch Hand
Posts: 310
1
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is obviously a classpath related error. Please try
java -classpath . com.headfirstjava.PackageExercise and see how it goes (from the myProject/classes directory)

Also what is the value of full $CLASSPATH environment variable ? Which O/S you are trying?
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajeev Rnair wrote:This is obviously a classpath related error. Please try
java -classpath . com.headfirstjava.PackageExercise and see how it goes (from the myProject/classes directory)

Also what is the value of full $CLASSPATH environment variable ? Which O/S you are trying?


I'm using Ubuntu 9.04

value of $CLASSPATH is ../classes

When I tried:

rsaska@dell-desktop:~/Documents/JavaExamPrep/myProject/classes$ java -classpath . com.headfirstjava.PackageExercise

I get the following error

Exception in thread "main" java.lang.NoClassDefFoundError: com/headfirstjava/PackageExercise
Caused by: java.lang.ClassNotFoundException: com.headfirstjava.PackageExercise
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:319)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:264)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:332)
Could not find the main class: com.headfirstjava.PackageExercise. Program will exit.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic