943,871 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 1402
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
May 25th, 2009
0

Exception in thread "main" java.lang.noclassdef............

Expand Post »
When I tried to compile my HelloWorldApp.java file using the following command:
javac HelloWorldApp.java
it worked (for being able to do this simple thing I had to change a lot of variables).

But then when I tried to Run the program using:
java HelloWorldApp
it gave me the error:
Exception in thread "main" java.lang.noclassdef


To solve this error I tried various commands like:
set classpath=%classpath%;.;
java -classpath . HelloWorldApp
java -classpath . HelloWorldApp.class

but to no avail. Can anyone help me out with this problem?

Regards,

-sgsawant
Last edited by sgsawant; May 25th, 2009 at 3:08 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sgsawant is offline Offline
9 posts
since May 2009
May 25th, 2009
0

Re: Exception in thread "main" java.lang.noclassdef............

While compiling you need to specify the .java file, you are right on that part, but while running the file through the java command you cannot specify the .java file, such a file won't simply run, the JVM would need a .class file. So you would have to execute the command in this way :
javac HelloWorldApp the class loader would look for a file with that name and a .class extension.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
May 25th, 2009
0

Re: Exception in thread "main" java.lang.noclassdef............

Click to Expand / Collapse  Quote originally posted by verruckt24 ...
While compiling you need to specify the .java file, you are right on that part, but while running the file through the java command you cannot specify the .java file, such a file won't simply run, the JVM would need a .class file. So you would have to execute the command in this way :
javac HelloWorldApp the class loader would look for a file with that name and a .class extension.
My sincerest apologies. As you have rightly pointed out it will not run if I command "java HelloWorldApp.java" but I also tried "java HelloWorldApp". I have also corrected the entry in the original post.

The error still persists. In fact if I had typed "java HelloWorldApp.java" it would have given me a different error.


Can you find the mistake I am making?

Regards,

-sgsawant
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sgsawant is offline Offline
9 posts
since May 2009
May 25th, 2009
0

Re: Exception in thread "main" java.lang.noclassdef............

> So you would have to execute the command in this way :
> javac HelloWorldApp the class loader would look for a file with that
> name and a .class extension

A typo maybe? It should be java HelloWorldApp

> Exception in thread "main" java.lang.noclassdef

This question has been asked a lot of times on almost all java related message boards. Have you tired a web search with the given error message? Have you tried the solutions suggested in the existing threads?

BTW, the `java' command expects a "package qualified" class name and not just a class name. So if you are using a package, make sure you use the fully qualified class name when invoking `java'. If not, then executing the `java' command from the same directory in which the class file lies shouldn't be a problem as such.

Also, is the NoClassDefFoundError error thrown for the main class or some other class which the main class is using? In this case, you might have unresolved runtime dependencies which you will have to resolve by setting the appropriate classpath entries.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
May 25th, 2009
0

Re: Exception in thread "main" java.lang.noclassdef............

Thanks for your reply. I have searched using google the error I have posted above. I thoroughly exhausted the first 10 results given by google; after which the solutions have become repetitive.

I believe I have set the paths correctly. Yet as has been suggested on many threads I also asked java to look into the same folder for the class file. One of the ways in which I tried doing that was:
java -classpath . HelloWorldApp
(this i did right after javac command successfully created the .class file)
Yet I am facing the same errors.

To see if the problem is universal I tried a java program which I knew worked. It came with a camera I have installed on 1 of my robot.
After copying it to an arbitrary folder (C:\jtemp) I tried
java CMUcamGUI
Which gave errors. Thinking that it may be because of the path I tried the following:
java -classpath . CMUcamGUI

This time the program worked and opened up the expected interface. Buoyed by this success I copied the HelloWorldApp.java file to the folder C:\jtemp
I compiled it using the javac command. That was successful as the .class file appeared right besides it.

Now when I tried the same method:
java -classpath . HelloWorldApp
I am still receiving the same errors. Is there a way around this problem? Please help.
Last edited by sgsawant; May 25th, 2009 at 4:13 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sgsawant is offline Offline
9 posts
since May 2009
May 25th, 2009
0

Re: Exception in thread "main" java.lang.noclassdef............

Post the code of the class "as it is" without *any* modification, including the package declaration. Also paste the *exact* error message along with a screenshot of the entire session.

This shouldn't be happening if you are well aware of the points I mentioned in my previous post.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
May 25th, 2009
0

Re: Exception in thread "main" java.lang.noclassdef............

I think your classname is different then your .java file. After you compiled a .java file, a .class file will be produced. Determine the name of .class file and try to execute with,

>java classname

NOTE: Lets us know the version of jdk you are using.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
May 26th, 2009
0

Re: Exception in thread "main" java.lang.noclassdef............

To write the program I used Netbeans IDE 6.5.1
The version of my JDK is 1.6.0_13

My program is as follows
Java Syntax (Toggle Plain Text)
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package helloworldapp;
  7.  
  8. /**
  9.  *
  10.  * @author Shashank Sawant
  11.  */
  12. public class HelloWorldApp {
  13.  
  14. /**
  15.   * @param args the command line arguments
  16.   */
  17. public static void main(String[] args) {
  18. System.out.println("Hello World!");
  19. }
  20.  
  21. }

The source file is stored at the following address on my hard disk:
C:\Documents and Settings\Shashank Sawant\My Documents\NetBeansProjects\HelloWorldApp\src\helloworldapp\HelloWorldApp.java

The compiled file is stored at the address:
C:\Documents and Settings\Shashank Sawant\My Documents\NetBeansProjects\HelloWorldApp\src\helloworldapp\HelloWorldApp.class

One seemingly important thing I discovered after viewing the properties of the HelloWorldApp.class file through the Netbeans IDE is that it had the following "Runtime Classpath":
C:\Documents and Settings\Shashank Sawant\My Documents\NetBeansProjects\HelloWorldApp\build\classes

Following link is the screenshot of the error and a few commands that have preceded it:
I have tried to commands after compiling the HelloWorldApp.java file. One is the simple "java HelloWorldApp" {the last but one error} and the second is "java -classpath . HelloWorldApp
Both produce failure.
http://1.bp.blogspot.com/_NpujkFKQb0w/ShuJI7so4pI/AAAAAAAAMf8/DsT1qyhS6eQ/s1600-h/Java+Error+1.JPG


I have linked the image as I was unable to find a method to upload the image on this post. I guess there is none. Thank You for considering my error and trying to help me out.

Regards,

-sgsawant
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sgsawant is offline Offline
9 posts
since May 2009
May 26th, 2009
0

Re: Exception in thread "main" java.lang.noclassdef............

Let me again point you to my first post in this thread which clearly mentioned:
Quote ...
BTW, the `java' command expects a "package qualified" class name and not just a class name. So if you are using a package, make sure you use the fully qualified class name when invoking `java'. If not, then executing the `java' command from the same directory in which the class file lies shouldn't be a problem as such.
Does that ring a bell? Think what could have gone wrong, give it a second try and reproduce the entire session again in case you still face problems.

BTW, it's advisable to have the project placed in your personal folder; something like D:\personal\projects to avoid dealing with excessively long paths which IMO is messy.
Last edited by ~s.o.s~; May 26th, 2009 at 3:56 am.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
May 26th, 2009
0

Re: Exception in thread "main" java.lang.noclassdef............

Because of this statement
Java Syntax (Toggle Plain Text)
  1. package helloworldapp;
  2. ..
  3. ..

you have to change the your method of compilation and also execution.

If package is mentioned then your .class must be placed under the package named folder.

Now, compile your program
>javac HelloWorldApp.java -d .

>java helloworldapp.HelloWorldApp

Note:
>javac HelloWorldApp.java -d .
where, -d option create a package folder and
. means current folder
>java helloworldapp.HelloWorldApp

If class definition placed inside the package then qualify the class with package name.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: getting files in the linux server
Next Thread in Java Forum Timeline: j2me help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC