• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help on NoClassDefFoundError

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They are both the same. "." is your current folder.
 
Luigi Plinge
Ranch Hand
Posts: 441
Scala IntelliJ IDE Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
except java might not understand the backslashes and expect you to replace them with "/"
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic