*
The moose likes Beginning Java and the fly likes java. lang. NoClassDefFoundError and it's not my Classpath (I think) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java. lang. NoClassDefFoundError and it Watch "java. lang. NoClassDefFoundError and it New topic
Author

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

simon coldcat
Greenhorn

Joined: Jul 22, 2005
Posts: 4
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

Joined: Jul 22, 2000
Posts: 9044
    
  10
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"


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
simon coldcat
Greenhorn

Joined: Jul 22, 2005
Posts: 4
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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
simon coldcat
Greenhorn

Joined: Jul 22, 2005
Posts: 4
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

Joined: Mar 24, 2004
Posts: 83
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

Joined: Dec 14, 2004
Posts: 148
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


Money for nothing and Java for Free
SCJP, SCWCD
simon coldcat
Greenhorn

Joined: Jul 22, 2005
Posts: 4
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: java. lang. NoClassDefFoundError and it's not my Classpath (I think)
 
Similar Threads
Javac compiler won't start!! can't compile any code
Problem with my Java
can't compile .java file
Can not run a simple program
Tomcat 5 point at prev JDK/JRE not 1.5