Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Exception in thread "main" java.lang.NoClassDefFoundError - Java2sdk1.4.2_06

Posted on 2004-10-22
23
Medium Priority
?
87,658 Views
Last Modified: 2012-06-21
I've obviously a newbie to java, *ATTEMPTING* to teach myself, but I've run into a problem that's really held me up.

As you can see below, I'm attempting to run a java application called "helloworldapp".  helloworldapp.class (and .java) is located in c:\j2sdk1.4.2_06\bin\helloworldapp.
It compiles just fine but will not run.  I think there may be a classpath problem, but I've changed the path a ton of times and can't get it right.

_________________________________

C:\j2sdk1.4.2_06\bin\HelloWorldApp>javac helloworldapp.java

C:\j2sdk1.4.2_06\bin\HelloWorldApp>java helloworldapp
Exception in thread "main" java.lang.NoClassDefFoundError: helloworldapp (wrong
name: HelloWorldApp)
        at java.lang.ClassLoader.defineClass0(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)

C:\j2sdk1.4.2_06\bin\HelloWorldApp>dir
 Volume in drive C has no label.
 Volume Serial Number is 549D-DF6C

 Directory of C:\j2sdk1.4.2_06\bin\HelloWorldApp

10/22/2004  03:46 PM    <DIR>          .
10/22/2004  03:46 PM    <DIR>          ..
10/22/2004  04:17 PM               432 HelloWorldApp.class
10/22/2004  03:59 PM               157 HelloWorldApp.java
               2 File(s)            589 bytes
               2 Dir(s)   3,418,599,424 bytes free
_____________________________________

Running WinXP SP2, and here's what I have for my environmental variables:

User variables:
CLASSPATH - .;C:\j2sdk1.4.2_06\bin
PATH - .;C:\j2sdk1.4.2_06\bin

System variables:
CLASSPATH - .;C:\j2sdk1.4.2_06\lib\tools.jar;C:\j2sdk1.4.2_06\jre\lib\rt.jar;C:\j2sdk1.4.2_06\jre\classes
JAVA_HOME - c:\j2sdk1.4.2_06
Path - %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;C:\Program Files\NMapWin\\bin;"C:\Program Files\Symantec\Norton Ghost 2003\";C:\j2sdk1.4.2_06\bin;JAVA_HOME;

I found many instances of this issue here and elsewhere and have tried most of their suggestions, and now I'm afraid I may have just completely messed it all up!

Please help!
0
Comment
Question by:tebacher
  • 6
  • 3
  • 3
  • +9
23 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 12385398
Try

java -classpath . helloworldapp
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 12385428
Java of course is case-sensitive, so the class name must be exactly as declared in the class file, and the same as the file name
0
 

Expert Comment

by:Ori_Kremer
ID: 12385438
As the exception suggests - you have a problem with names.
Try renaming the helloworldapp.java file to HelloWorldApp.java and running "java HelloWorldApp" .
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 8

Expert Comment

by:kiranhk
ID: 12385461
are you running your command from this path
C:\j2sdk1.4.2_06\bin\HelloWorldApp>java helloworldapp

check if your helloworldapp.java has got any package?? if it has any package you need to create the folders as in the package names in your java files and then place the class file in the last folder of your package. Then you can run ur app like

C:\j2sdk1.4.2_06\bin\HelloWorldApp>java com.helloworldapp.helloworldapp


the folder from where u run the command should also have the package starting  for your java class.
 so like in the above command if your package is com.helloworldapp then u should be having folders
C:\j2sdk1.4.2_06\bin\HelloWorldApp>
---------------->com------->helloworldapp

pl check out all these and test again.
hope this helps

0
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12385600
See CEHJ's comment, if still no luck, try this:
just add this line, and press enter, before running (and preferably prior to compiling) your file:

set classpath=%classpath%;%cd%

Best of luck,
[r.D]
0
 
LVL 2

Expert Comment

by:ThummalaRaghuveer
ID: 12385702
Heres what your problem is...
 as suggested by CEHJ you are using wrong class name

You are using java helloworldapp

while the contents of the directory say its HelloWorldApp.class

so You should be using java HelloWorldApp

Here is a simple scenario that I tried without looking much into the problem and it even sounds the same....

Your file name is helloworldapp.java

class HelloWorldApp
{
public static void main(String[] args)
{
System.out.println("Hello");
}
}

It compiles and when U look at the contents of the directory U will get
helloworldapp.java
HelloWorldApp.class
Now try this in same file removing the above contents

public class helloworldapp
{
public static void main(String[] args)
{
System.out.println("Hello");
}
}

It compiles as the public class name and file name are same but If You look at contents of directory You should get
helloworldapp.java
helloworldapp.class

but surprisigly I found HelloWorldApp.class instead of helloworldapp.class( I donno why this is happening)

but to execute it I need to give java helloworldapp

If I give java HelloWorldApp I get the following error

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp (wrong
name: helloworldapp)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:605)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
4)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
        at java.net.URLClassLoader.access$100(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:290)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:279)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:236)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:303)

Which is exactly the error shown above......

So open HelloWorldApp.java

check the class name that contains main...

and then use that classname after java while trying to run.
0
 
LVL 92

Expert Comment

by:objects
ID: 12386023
run it using the following:

java HelloWorldApp

(your classpath is fine, and everything else appears fine)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12386042
That's already been said (several times)
0
 
LVL 92

Expert Comment

by:objects
ID: 12386070
Not directly (in fact only Ori did actually), and the above comments primarily talk about changing the classpath and filename which are all unnecessary.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12386097
However reliable the evidence of the classpath that's *actually being used is* (and that shouldn't be assumed to be 100% correct) all the information necessary to get this working has already been given
0
 
LVL 3

Expert Comment

by:oratim
ID: 12386110
Not to mention that it is not necessary to have the environment variable classpath set. I had a similar problem when I started coding in java, and fixed it by deleting the classpath env variable

0
 
LVL 92

Expert Comment

by:objects
ID: 12386120
So what exactly is your problem, with my clarifying what I believe to be the correct approach to run the command? As well as useful information, there is also a lot of erroneous information posted which could well confuse the asker. For eaxample, the suggestion that you posted will certainly *not* work.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12386145
>>So what exactly is your problem, with my clarifying what I believe to be the correct approach

Simply that it's unnecessary. Your comments simply repeat what Ori Kremer said, which was the specific form of my more generic second comment
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12387988
CEHJ & objects, don't fight! It's becoming a habit!

;JOOP!
0
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12388009
lol - last time I had a disagreement with someone, I was threatened that I will loose my account for it! :o\

0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12388016
By whom? Only moderators may tell you so.
;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 12388021
tebacher, next time you have such a problem, post your source;
it would have made this all easier and shorter.
;JOOP!
0
 
LVL 9

Expert Comment

by:DrWarezz
ID: 12388022
Yup -- it was moderators!
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 12390976
By the way, putting your java app in "C:\j2sdk1.4.2_06\bin" is not a good idea.
Using a directory of your own is much better (something like "C:\MyJavaProjects\HelloWorld").
0
 
LVL 5

Author Comment

by:tebacher
ID: 12399946
Wow, lots of help in this TA!  Thanks to everyone who has helped so far.

I *swear* I tried 'java HelloWorldApp' (with Caps as they should be) before I posted here!  But, after the weekend I booted up my PC and tried it with the caps, and it worked.   Just a case problem  :-)
Maybe I tried the Caps BEFORE I got the correct class path in and then just disregarded it from then on.

Anyway, thanks for the help!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12400412
8-)
0
 
LVL 6

Expert Comment

by:SamsonChung
ID: 12563294
Aside from all the others' comments,

I suggest you to use an IDE for java..

some popular ones includes the paid ones such as borland, or JCreator Pro.

while the free ones such as Gel.

the IDE helps you a lot when it comes to memorization of different java packages and therefore allows you to easily learn Java.
0
 
LVL 1

Expert Comment

by:SchmidlinF
ID: 13323097
> Aside from all the others' comments,
> I suggest you to use an IDE for java..
> some popular ones includes the paid ones such as borland, or JCreator Pro.
> while the free ones such as Gel.

You must be kidding!?! Don't even think of looking further than the Eclipse IDE!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month10 days, 10 hours left to enroll

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question