Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java. lang. NoClassDefFoundError and it's not my Classpath (I think)

 
simon coldcat
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Help! I can compile my MyFirstApp.Java file, but I cannot run the MyFirstApp.Class File. I've tried several suggestions posted at various websites that say it is my CLASSPATH environment variable and suggest changes, but nothing works. Below I have included my code and the latest settings I am working with. I am running Windows XP, and I wonder if this is part of the problem?

I am so frustrated! I would greatly appreciate some help!
Thank you,
Simon

This is my file MyFirstApp.Java:
------------------------------------------------------------
class MyFirstApp{

void main(){
System.out.print("Hello World");
}
}
-------------------------------------------------------------
My PATH variable is the following: "C:\Program Files\Java\jdk1.5.0_04\bin"
My CLASSPATH variable is the following: "."
Please note that my PATH variable is a system variable, and I can query it through the Command Prompt. My CLASSPATH variable is User Variable. I can see it in the Control Panel->System->Advanced->Environment Variables...but when I query it through the Command Prompt I get an error.
-------------------------------------------------------------
When I type "javac MyFirstApp.java" at the command prompt, the file compiles fine and the MyFirstApp.class file is created (in the same directory)
-------------------------------------------------------------
When I type "java MyFirstApp.class" I get the following error:
"Exception in thread "main" java.lang.NoClassDefFoundError: myfirstapp/class"
--------------------------------------------------------------
When I type "java -version" I get the following:
"java version "1.5.0_04"
java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)
Java HotSpot(TM) Client UM (build 1.5.0_04-b05, mixed mode, sharing)"
-----------------------------------------------------------------
 
Marilyn de Queiroz
Sheriff
Posts: 9063
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by simon coldcat:
When I type "java MyFirstApp.class" I get the following error:
"Exception in thread "main" java.lang.NoClassDefFoundError: myfirstapp/class"

Rather than "java MyFirstApp.class", you should type "java MyFirstApp"
 
simon coldcat
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marilyn de Queiroz:

Rather than "java MyFirstApp.class", you should type "java MyFirstApp"


Thanks for your reply Marilyn. I tried typing "java MyFirstApp", and I received the following error:

"Exception in thread "main" java.lang.NoSuchMethodError: main"
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The declaration of "main" has to be

public static void main(String[] argv) {
...

The only thing you can change on that line is the word "argv", which is an arbitrary variable name. You've left out the "public", the "static," and the "String[] argv", and so the launcher can't find the method it's looking for.
 
simon coldcat
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
The declaration of "main" has to be

public static void main(String[] argv) {
...

The only thing you can change on that line is the word "argv", which is an arbitrary variable name. You've left out the "public", the "static," and the "String[] argv", and so the launcher can't find the method it's looking for.


Hi Ernest. Thanks for your input. I made my code the following:

class MyFirstApp{

public static void main(String[] myarg){
System.out.print("Hello World");
}
}

Now I get the following from the Command Prompt:
--------------
C:\User\Java>java myfirstapp.class
Exception in thread "main" java.lang.NoClassDefFoundError: myfirstapp/class
------------------
C:\User\Java>java myfirstapp
Exception in thread "main" java.lang.NoClassDefFoundError: myfirstapp (wrong nam
e: MyFirstApp)
at java.lang.ClassLoader.defineClass1(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)

------------------
Any Ideas? Thanks, Simon
 
Jon Egan
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even though you're working in DOS/Windows environment, which is not generally case-sensitive, java IS case-sensitive.


won't work because there's no file named "myfirstapp.class". You need to type:

to match the filename - which, by the way, must match the name of the public class within the source file... again, case-sensitive (so you can't just solve your problem by renaming the class file).

Hope this helps,
Jon
 
madhup narain
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I beleieve that all has been done to take care of your problem and there is nothing much that i can say except that "WELCOME TO CRAZY WORLD OF JAVA"
crazy but powerful

Regards
 
simon coldcat
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All. I tried Jon's suggestion this morning and it worked! I am off and running with JAVA. I have written a couple of little programs, and they are working.

I want to thank you all for your help. All of the suggestions that you posted were necessary changes for me to get started. I don't think I would have figured out all three changes by myself!

Thank you, thank you, thank you!

Simon
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic