The moose likes Beginning Java and the fly likes confusion about classpath and packages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "confusion about classpath and packages" Watch "confusion about classpath and packages" New topic

confusion about classpath and packages

Karen Nelson
Ranch Hand

Joined: Aug 19, 2005
Posts: 57
I am confused about classpaths, packages, and directory structure. I had a program that was running, but when I made some changes to the directory structure, it stopped running. Here's what i have:

1.a directory (myclasses) with several graphing utilities I wrote a while back. Inside this directory there are the paths source\com\rephunk (where the java files are) and classes\com\rephunk. Each java file starts with �package com.rephunk�.
2.a project directory (punnett) with the same paths. In source\com\rephunk, there are two java files, one of which has the main procedure, called PunnettGui.java. These files also start with the same package statement.
3.I compile from the source folder of the project directory, using javac �cp .;c:\java\myclasses\classes;c:\java\punnett\classes �d ..\classes *.java This seems to work OK.
4.I try to run from the classes folder of the project directory, using java �cp .;c:\java\myclasses\classes com.rephunk.PunnettGui, and I get the NoClassDefFound error on the class PunnettGui. (I�ve also tried with java �cp .;c:\java\myclasses\classes;c:\java\punnett\classes com.rephunk.PunnettGui).

I would really appreciate any help!! Thank you,
Joel McNary

Joined: Aug 20, 2001
Posts: 1817

NoClassDefFound is actually different from ClassNotFound -- and is one of the most unhelpful and confusing errors out there.

Usually when you receive a NoClassDefFound, it can find the class that it's looking for. However, it cannot find a dependant class. But it won't tell you which dependant class it can't find. It jsut tells you that there's a problem with the main class.

Make sure that the classpath contains all dependant classes, and try again.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Ray Horn
Ranch Hand

Joined: Oct 20, 2005
Posts: 39
You could try adding -verbose switch on the java command line. It will show which classes the JVM is loading. Might help isolate the problem class.
It creates a long load list so you may want to redirect the output to a file and use an editor to search for your directory path/files.
ex: java -verbose ... > output.txt
Karen Nelson
Ranch Hand

Joined: Aug 19, 2005
Posts: 57
Thank you for the help! Problem solved.

For the record, I looked in the classes directory and found that some of my classfiles were ending up in the wrong directory, because I had neglected to put a package statement in one file.

In addition I had left a slash out of the classpath name in the java statement. Either one was enough to scuttle the program!

Thank you again!!!
[ November 04, 2005: Message edited by: Karen Nelson ]
I agree. Here's the link: http://aspose.com/file-tools
subject: confusion about classpath and packages