Forums Register Login
Help on NoClassDefFoundError
I am having trouble with some running some of the exams codes I have found.

Here is the directory where all my source code and classes are located:

I have set the classpath in windows 7 as so:

I am able to normally run any codes from

But when I tried to create a new folder called learnkey:

I have to do this:

and to run it

So far so good.

But when I have a code located at:

and its code is:

I have the other class placed in:

having the codes:

When I compiled the code like so:

Now... how am I going to run the compiled code? I did want to put all what I have tried as all of them failed. Any help is highly appreciated.

Francis Zabala wrote:
I have set the classpath in windows 7 as so:

Don't do this. Just remove it. you should set your PATH to this location which helps the executables like javac/java etc... run from anywhere.

When you run a Java program, by default Java uses the current directory (if you have not set the classpath explicitly) to find the classes it needs. If you need other third party classes etc... you may set them in the classpath for your convenience. Read How To Set The Classpath to see how you can do this.
Vijitha, I think by classpath he means the PATH variable; if he didn't, java and javac would not have run successfully for him!

Francis - you need to put your User class in the xcom folder, because you have the "package xcom" statement at the top.
Hi Guys,

I apologize for the delay in my reply. I have been busy with work this week and this is the only time I had to view this forum again.

Thank you for the inputs it really helped me alot.


What if it put my class outside of xcom? What I put User.java here:

and the Stuff.java here:

then what should I do if I put my User.java here?

What I am trying to achieve is to understand the various what if cases and how to run them. Oh by the way, I learn more by example.
I'm afraid I am no expert on this since I do everything in an IDE... messing around trying to get everything in the right folders can be a headache as you have found... Maybe some others could chime in with detailed answers to your questions.

Here are couple of resources I found on the web:

You need to look up package statements and how they relate to directory/folder structures. Each name in a package refers to one directory/folder level by that same name.

Every Java class is in a package. The default package has no name, and classes in it are expected in a root directory of one element of your classpath.

So you can have a class named User, with no package, and put it in whatever directory you want. To access that class, either the classpath needs to specify the directory, or the classpath specifies "." and the directory can be your default directory (i.e., the one you have "changed directory (cd)" to on the command line.

For a package example, if your class user is in the package xcom.innerPackage, then you can have a classpath that includes the value "C:\Users\francis\Dropbox\javaTemp", and the class in xcom\innerPackage\User.class within the javaTemp directory.

Or you can have your default directory be C:\Users\francis\Dropbox\javaTemp, put "." in your classpath (if it isn't a default, I can never remember), and have User in the same location.

Uh... I think my brain broke. :P

Ok, from what I understand,

either I do this:

or this:

Did I correctly understand what you said?
They are both the same. "." is your current folder.
except java might not understand the backslashes and expect you to replace them with "/"
Yes, as near as I can tell without trying it, these are correct -- assuming User is a class with a main method in it, so Java can run the class directly.

You could also enter

set classpath="<value>"

instead of putting "-cp <value>" on the command line.

Sorry for the dense explanation -- without writing a small chapter for a book, I can't cover the subject completely (we haven't mentioned jar files yet); if you need that much explanation, there are better places to get it than here.

Something went wrong with my codes. I can't run them now even with the simpliest setup. I'll be back after I managed to get them running.
Ok now I get it.

Let me share what I found out.

My setup is

For C:\Users\francis\Dropbox\javaTemp\learnkey\User.java

For C:\Users\francis\Dropbox\javaTemp\learnkey\xcom\Stuff.java

For C:\Users\francis\Dropbox\javaTemp\learnkey\xcom\User.java

To successfully compile: C:\Users\francis\Dropbox\javaTemp\learnkey\Stuff.java

This will not work:
because it cannot find the package xcom

To run the code

This will not work:

To successfully compile: C:\Users\francis\Dropbox\javaTemp\learnkey\xcom\Stuff.java

and to run it:

What I understand from this is that adding -cp sets the "root" location of the classes.

Uhm... did I get it right?

What I understand from this is that adding -cp sets the "root" location of the classes.

Yes, the entries for the classpath should point to the root where package hierarchies reside.
Yes, you got it right. Congratulations on sticking with it.

I'm now going to extend the lesson one more step. If it confuses you, you can read more about it in all kinds of references.

The classpath can contain more than one item; each item points to a root of classes. It can be a directory, like you have been using, or it can be a jar file.

A jar file is a zip file with java classes and maybe some other things in it. As you may know, zip files store the directory path as well as the name of each file stored in that file. So a jar file is created from a set of classes in a directory tree that maps the package structure, just like the (small) directory tree you've been using.

To put an additional item on a classpath, you separate it with with a semicolon in Windows, a colon on Unix. Let's say you had a communications library written in java and distributed as a jar file named comm.jar. You could have:

set classpath=".;comm.jar"

That's assuming the jar file is in the current directory, and the other classes you want to use are rooted at the current directory. The java runtime, when it needs a class, starts with the first item on the classpath, then goes through them one at a time looking for the classfile it is trying to find.

So I hope that's a useful addition. I thought it worthwhile to mention it while you were finishing up the topic.

Thanks for the input guys! Really appreciate it. I don't have anything against setting the classpath using the environment variables, it's that I like to use the -cp option every time so that I can practice using it and when I want to test my codes on different machines. That's why it's in my dropbox folder.

I am going to try with the jar files soon.

This thread has been viewed 1163 times.

All times above are in ranch (not your local) time.
The current ranch time is
Oct 18, 2018 21:21:50.