File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Help on NoClassDefFoundError Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Help on NoClassDefFoundError" Watch "Help on NoClassDefFoundError" New topic
Author

Help on NoClassDefFoundError

Francis Zabala
Ranch Hand

Joined: Aug 11, 2010
Posts: 30
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.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3826

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.


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

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.
Francis Zabala
Ranch Hand

Joined: Aug 11, 2010
Posts: 30
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.

@Luigi,

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.
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

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:

http://download.oracle.com/javase/tutorial/java/package/managingfiles.html
http://pages.cs.wisc.edu/~cs368-1/JavaTutorial/NOTES/Packages.html
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
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.

rc
Francis Zabala
Ranch Hand

Joined: Aug 11, 2010
Posts: 30
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?
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

They are both the same. "." is your current folder.
Luigi Plinge
Ranch Hand

Joined: Jan 06, 2011
Posts: 441

except java might not understand the backslashes and expect you to replace them with "/"
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
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.

rc
Francis Zabala
Ranch Hand

Joined: Aug 11, 2010
Posts: 30
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.
Francis Zabala
Ranch Hand

Joined: Aug 11, 2010
Posts: 30
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


Francis Zabala
Ranch Hand

Joined: Aug 11, 2010
Posts: 30
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?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3826

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.
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
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.

rc
Francis Zabala
Ranch Hand

Joined: Aug 11, 2010
Posts: 30
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.
 
Consider Paul's rocket mass heater.
 
subject: Help on NoClassDefFoundError