NoClassDefFoundError

J

John-FL

Hey,

I get a "NoClassDefFoundError" in my program when I call it using:
java -classpath /path/to/program/ /path/to/program/Program
Exception in thread "main" java.lang.NoClassDefFoundError: /path/to/program/Program

However, when I specify the path to the java program (relative), it
works:
java Program

In both cases I am in the /path/to/program directory, and in there is
Program.class and Program.java. Could someone let me know why this
isn't working?

Thanks!
John
 
M

Mark Space

John-FL said:
Hey,

I get a "NoClassDefFoundError" in my program when I call it using:
java -classpath /path/to/program/ /path/to/program/Program

What Eric said. In other words, if you have some class file, like:

/path/to/program/mypack/Program.class

and the Program class is defined like this:

package mypack;
public class Program {
public static void main( String... args ) {
// Do something
}
}

The you want to run:

java -cp /path/to/program mypack.Program

Nothing else will do. You have to call the program by it's proper name
("mypack.Program") because the JVM won't run it if you specify a
different name (which would be an incorrect name) and the classpath must
point to the root of the hierarchy of the classes.

If you can run your program just as java -cp Program then it's not in a
package, but don't get confused. Even though packages names are
converted into subdirectory names (so the loader can find them), package
names are not changed by changing the directory that a class-file
resides in. Package names are controlled by that "package" keyword
above, and if your subdirectories don't match, it's an error.
 
Joined
Oct 22, 2008
Messages
4
Reaction score
0
Mark Space said:
John-FL wrote:
> Hey,
>
> I get a "NoClassDefFoundError" in my program when I call it using:
> java -classpath /path/to/program/ /path/to/program/Program
>> Exception in thread "main" java.lang.NoClassDefFoundError: /path/to/program/Program


What Eric said. In other words, if you have some class file, like:

/path/to/program/mypack/Program.class

and the Program class is defined like this:

package mypack;
public class Program {
public static void main( String... args ) {
// Do something
}
}

The you want to run:

java -cp /path/to/program mypack.Program

Nothing else will do. You have to call the program by it's proper name
("mypack.Program") because the JVM won't run it if you specify a
different name (which would be an incorrect name) and the classpath must
point to the root of the hierarchy of the classes.

If you can run your program just as java -cp Program then it's not in a
package, but don't get confused. Even though packages names are
converted into subdirectory names (so the loader can find them), package
names are not changed by changing the directory that a class-file
resides in. Package names are controlled by that "package" keyword
above, and if your subdirectories don't match, it's an error.

you have have to set classpath
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top