File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Error when running packaged code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Error when running packaged code" Watch "Error when running packaged code" New topic
Author

Error when running packaged code

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
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


[Thumbnail for directory_structure.png]


Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

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?


SCJP6, SCWCD5, OCP-JBCD5, OCE-JWSD6 OCE-JPAD6 , OCM-JEA5 1,OCM-JEA5 2,3 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error when running packaged code