aspose file tools*
The moose likes Cattle Drive and the fly likes HelloWorld NoClassDefFoundError Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "HelloWorld NoClassDefFoundError" Watch "HelloWorld NoClassDefFoundError" New topic
Author

HelloWorld NoClassDefFoundError

Erin Quick-Laughlin
Greenhorn

Joined: Dec 31, 2005
Posts: 5
Hoping that I can still take the classes using java version "1.5.0_06", I've created my HelloWorld.java using Windows edit and after running 'javac HelloWorld.java' and then 'java HelloWorld' I get a NoClassDefFoundError!?!


Here's my HelloWorld.java:



Any thoughts?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
It sounds like a CLASSPATH issue. What is currently in your CLASSPATH environment variable?


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Stuart Goss
Ranch Hand

Joined: Mar 21, 2001
Posts: 169
Howdy Erin!

Are you sure you are typing

"javac HelloWorld.java" to compile
and
"java HelloWorld" to run ?

Do not use the .class or even .java extension when running the java programme.

Welcome to the Cattle Drive!
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Howdy Erin! Welcome to the ranch.

...after running 'javac HelloWorld.java' and then 'java HelloWorld' I get a NoClassDefFoundError!?!



So you get no error when you run javac HelloWorld.java?

Pauline
Oscar Wu
Greenhorn

Joined: Dec 14, 2005
Posts: 12
I actually got the same thing when I tried do a simple program.

I never knew where the problem was, but uninstalled any previous java development kits and virtual machines, and reinstalled the latest version. Somehow this fixed the issue.

Also, make sure your enviroment variables are setup correctly.


SCJP 1.4 - 03/06<br />SCWCD - In progress
Erin Quick-Laughlin
Greenhorn

Joined: Dec 31, 2005
Posts: 5
Well I'm glad to hear someone else has actually run into this - I apogize for not responding right away, but I will check my classpaths first.

Thanks!
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Argh, I just ran into this myself. Between the last time I used java on my Windows machine and today, a program installation apparently changed the environment classpath on me. (It was pointing to a QTjava.zip file, maybe the problem was the Quicktime something or other that I just updated today.)

I uninstalled the java 5 runtime environments (3 in all) that I found and left the 1.4 jre. Now I'm back in business. I suppose I could have reset the classpath, but I'll leave that until it's necessary again.

long live retrofreaks!
Erin Quick-Laughlin
Greenhorn

Joined: Dec 31, 2005
Posts: 5
I did have to reinstall j2sdk1.4.2_10 and reset my classpath and add the path settings for BIN and JRE\BIN, but I didn't have to uninstall 5.0. I was able to successfully compile and run HelloWorld and yet when I execute 'java -version' from cmd, it still tells me 1.5.0_06 - go figure.
Joe Ford
Greenhorn

Joined: Jan 18, 2009
Posts: 4
Hello All,

I'm having problems, I think with my classpath. I don't know how to use packages and command line Java development. Perhaps someone can help.

I've got a NoClassDefFoundError when running HelloWorld. Any help would be appreciated. I want to start to use packages because I know it's important.

Below is source code, the error on the command line I'm getting, as well as the output of "set classpath".

Thanks,
Joe


File: C:\src\com\joseph\hello\HelloWorld.java


Executing javac:
C:\src\com\joseph\hello>javac HelloWorld.java

Executing java:
C:\src\com\joseph\hello>java HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong nam
e: com/joseph/hello/HelloWorld)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
4)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: HelloWorld. Program will exit.

Here's my classpath:
C:\src\com\joseph\hello>set classpath
CLASSPATH=.;C:\src;C:\Program Files\Java\jre6\lib\ext\QTJava.zip
Joe Ford
Greenhorn

Joined: Jan 18, 2009
Posts: 4
Here's my PATH variable also, in case it's needed. I installed the Java compiler to C:\Java

C:\src\com\joseph\hello>echo %path%
C:\Java;C:\Java\bin;C:\Program Files\ThinkPad\Utilities;C:\WINDOWS\system32;C:\W
INDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Common Files\Lenovo;C:\Program
Files\QuickTime\QTSystem\;C:\Program Files\ThinkPad\ConnectUtilities


C:\Java>dir
Volume in drive C has no label.
Volume Serial Number is 9075-5699

Directory of C:\Java

01/18/2009 11:32 AM <DIR> .
01/18/2009 11:32 AM <DIR> ..
01/18/2009 02:38 AM <DIR> bin
11/10/2008 06:16 AM 3,841 COPYRIGHT
01/18/2009 02:38 AM <DIR> demo
01/18/2009 02:37 AM <DIR> include
01/18/2009 02:37 AM <DIR> jre
01/18/2009 02:37 AM <DIR> lib
01/17/2009 06:45 AM 17,400 LICENSE
01/17/2009 06:45 AM 17,496 LICENSE.rtf
01/17/2009 06:45 AM 29,318 README.html
01/17/2009 06:45 AM 25,828 README_ja.html
01/17/2009 06:45 AM 21,275 README_zh_CN.html
01/17/2009 06:48 AM 5,294 register.html
01/17/2009 06:48 AM 5,713 register_ja.html
01/17/2009 06:48 AM 4,891 register_zh_CN.html
01/18/2009 02:37 AM <DIR> sample
11/10/2008 06:17 AM 19,621,235 src.zip
01/17/2009 06:45 AM 231,168 THIRDPARTYLICENSEREADME.txt
11 File(s) 19,983,459 bytes
8 Dir(s) 191,649,320,960 bytes free

kshitij kaushik
Greenhorn

Joined: Jan 14, 2009
Posts: 11
well joe no need to worry.....just chek your class path properly..
on command prompt......
set path= *the directory where javac lives*

E.g.,

set path=C:\Program Files\Java\jdk\1.6.0_06\bin

and den Press Enter. For more information......
http://java.about.com/od/gettingstarted/ss/firstprogram.htm
Joe Ford
Greenhorn

Joined: Jan 18, 2009
Posts: 4
kshitij,

Thanks for the reply, but I don't believe the problem is in my path variable not able to find my javac program. As you can see from the text in my posts, I was able to compile the program successfully. It was upon execution of "java HelloWorld" that I ran into the problem. "java" is for executing already compiled programs. "javac" is for compiling source code into .class files.

I found an article that is helping, but I'm still struggling and so far it is not working.

http://www.jarticles.com/package/package_eng.html

If I am able to get this to work, I will post my results here.
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1197
If the java compiler was put directly in C:\ shouldn't the classpath variable be C:\Java\jdk\1.6.0_06\bin instead of C:\Program Files\Java\jdk\1.6.0_06\bin?
(Classpath stuff gives me the HEEBEEJEEBEES)
Just when you think you get it........................................ :!:
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1197
Joe Ford wrote:

Here's my classpath:
C:\src\com\joseph\hello>set classpath
CLASSPATH=.;C:\src;C:\Program Files\Java\jre6\lib\ext\QTJava.zip


Did you see Pauline's post earlier in this thread about installing QuickTime and having a problem?
This variable looks like the one she posted as causing a problem:
C:\Program Files\Java\jre6\lib\ext\QTJava.zip
You might want to check that out. Are your class files located in QTJava.zip?
Also, did you put your development environment inside C:\Program Files|, or directly off C:\ ?
Joe Ford
Greenhorn

Joined: Jan 18, 2009
Posts: 4
I figured it out!

Let me write down my findings in case someone else finds it useful.

First of all, PATH has NOTHING to do with Java. PATH is a way for you to reference programs (.exe files) that are located in a directory other than your current directory. For example, let's say you have an old-school program that you run from the command line. If the program is normally stored in C:\Program Files\whatever\whatever.exe, you would need to navigate to the "C:\Program Files\whatever\" directory to execute it. But when you open a command window, you normally start in "C:\" (let's just say).

Setting your PATH variable to "include" the "C:\Program Files\whatever" directory means that if you type "whatever.exe" on the command prompt, it will first look in the current directory, and it will not find whatever.exe, so it will start looking at the alternative paths that are in the PATH variable.

To do so, you would type
PATH = %PATH%;C:\Program Files\whatever

This means that you want the path variable to now be equal to the old path variable (that's the %PATH% part), plus whatever you put after that. Always separate directories with a semi-colon (;).

Why does this matter for Java? Well, you need to execute javac.exe and java.exe to run Java programs, right? Well, they are normally stored in some weird directory (like C:\Program Files\Java\jdk\1.6.0_06\bin). But you put your source code in a separate location, such as C:\source\.

To run javac.exe to compile your .java files, you need to type C:\Program Files\Java\jdk\1.6.0_06\bin\java.exe whatever.java. That's a lot of typing each time you want to run a compile. To make it easier on yourself, you change your PATH variable so that DOS knows where to find javac.exe and java.exe.

Make sense?

Now let's talk about the classpath. The classpath is a Java thing.

Classpath is a way to tell the java virtual machine (ie java.exe) where to find the .class files.

If you start to use packages (and you should!) it's important to understand this.

Suppose you have a file with a package declaration statement. You should store this file in a directory that matches the package hierarchy.

This file should be stored at:
CLASSPATH ROOT\com\joseph\hello\



Now you need to make sure that the directory that holds the "com" folder is in your classpath.

Executing this:
set classpath

Will result in the system displaying the current classpath.

Executing this:
set classpath = C:\

Will make java know where to look for your packages.

To execute the program, after compiling, you write:

java com.joseph.hello.HelloWorld

java.exe will look for a com folder in the classpaths root (C:\) and then it will look for a folder called joseph in the com folder, etc., until it finds the HelloWorld class.

Clear as mud?

Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1197
Doncha just love those Eureka moments?
That package thing catches me a lot. It's so obvious and so easily overlooked!

very torpe
Greenhorn

Joined: May 27, 2009
Posts: 1
Let me write down my findings in case someone else finds it useful.


Sure it has been. Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HelloWorld NoClassDefFoundError