File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Newbie: Run-time error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Newbie: Run-time error" Watch "Newbie: Run-time error" New topic
Author

Newbie: Run-time error

Steve Davies
Greenhorn

Joined: Apr 14, 2006
Posts: 6
I entered the first example from Head First Java, compiled and attempted to run via the JVM. But, I am getting an error that has me stumped:

Exception in thread "main" java.lang.NoClassDefFoundError: MyFirstApp

I'm a bit puzzled by this. The code is about as simple as it gets:


public class MyFirstApp
{
public static void main (String[] args)
{
System.out.println("I rule!");
System.out.println("The World");
}
}

I'd appreciate some assistance.

TIA


Steven E. Davies<br /> <br />"Those are my principles, and if you don't like them... well, I have others." - Groucho Marx
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
did you compile the class (running the java compiler using javac or in your IDE) ? is the class file on the classpath ?

because the error message just means that the class you want to run can not be found on the classpath.

the classpath is something like a collection of classes (.class files, but also class files in jar files) and resources (e.g. images you can load without using system specific/absolute file paths). you specify it using the -cp or -classpath option.


e.g.
java -cp .;somedir/classes;somejar.jar MyApp


for simple programs (consisting of just the classes, no libraries that are used) it is usualy enough to change to the directory where the class files reside (root of your package structure).


pascal
Yogendra Joshi
Ranch Hand

Joined: Apr 04, 2006
Posts: 213
Hey Steve ,

Welcome to the world of Java.. :-) Hey Bert , A new Novice Added to our Ranch..

Steve , What you can try to figure out is , Have you set the neccesary things to run your first app.. I mean Have you defined the path for your application ? Path variable is very important. What i would suggest you is this.

1)Add path variable to your environmental variables of your machine , If you dont know where they are , Here's it. Point ur mouse to My Computer -> Right Click -> Properties-> Goto Advanced Tab-> Environmental Variables.

2)Click on New under "User Variables" and give the name of the variable as path and value as the Address of the place where you installed j2sdk. For instance , I have it on my c: , So example can be variable name=path, variable value=c:\j2sdk1.4.0\bin, AFter setting this , Save your program in c:\j2sdk1.4.0\bin and then try to run your app again.. i.e save it as c:\j2sdk1.4.0\bin\MyFirstApp.

Have a great day !
Yogendra.


Meri Zindagi Hain Tab Tak.. Jab Tak Tera Sahara.... Har Taraf Tu Hi Tu Hain SAI Tera Hi Hain Nazara.....
Steve Davies
Greenhorn

Joined: Apr 14, 2006
Posts: 6
I had the PATH EnVar set to the BIN directory. I had no problems compiling.

I'm confused with teh class settings. I did the typical install of JDK 1.5. The CLASSPATH is set to as follows:

C:\Program Files\Java\jre1.5.0_06\lib\ext\QTJava.zip;

Since I did not set it, it must have been done at install. I am creating a very simple program. The compile placed the .class file in the same directory as the .java file.

I suspect once I get this resolved I'll be off and running. Something just is clicking for me.

Again, thanks for the help.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

After compiling, have you confirmed that you actually have a class file (MyFirstApp.class) in the directory? If it's compiling okay, then your PATH variable should be correct.

When running, make sure that you're in the proper directory. The easiest way to do this is to change directories (using the command cd) to where your files are located. For example...

cd c:\myFiles\java
javac MyFirstApp.java
java MyFirstApp

NOTE: You should not need to change any CLASSPATH settings at this point.
[ April 14, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Steve Davies
Greenhorn

Joined: Apr 14, 2006
Posts: 6
The class file is in the same directory as the source code.

Directory of C:\Documents and Settings\xxxxxx\My Documents\My Programming\Java\Tutorials\Head First Java

04/14/2006 06:20 PM <DIR> .
04/14/2006 06:20 PM <DIR> ..
04/14/2006 09:05 PM 448 MyFirstApp.class
04/14/2006 06:20 PM 174 MyFirstApp.java
04/14/2006 05:13 PM 153 MyFirstApp.java.bak
3 File(s) 775 bytes
2 Dir(s) 25,477,156,864 bytes free

I ran the JVM from that directory as follows:

C:>java MyFirstApp
Exception in thread "main" java.lang.NoClassDefFoundError: MyFirstApp
[ April 14, 2006: Message edited by: Steve Davies ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Steve Davies:
...I ran the JVM from that directory as follows:

C:>java MyFirstApp
Exception in thread "main" java.lang.NoClassDefFoundError: MyFirstApp

When you change to that directory (using the cd command), then your prompt should not be just...

C:\>

At the prompt, type cd followed by the directory path...

C:\>cd C:\Documents and Settings\xxxxxx\My Documents\My Programming\Java\Tutorials\Head First Java

This should change your prompt to...

C:\Documents and Settings\xxxxxx\My Documents\My Programming\Java\Tutorials\Head First Java>

Then try to run...

C:\Documents and Settings\xxxxxx\My Documents\My Programming\Java\Tutorials\Head First Java>java MyFirstApp
[ April 14, 2006: Message edited by: marc weber ]
Steve Davies
Greenhorn

Joined: Apr 14, 2006
Posts: 6
I'm sorry. I was trying to make it more readable. The prompt can be whatever you wish it to be.

For instance:

prompt $N$G

Will get you:

C>

A prompt of:

C:\Documents and Settings\Steven\My Documents\My Programming\Java\Tutorials\Head First Java>

... is just too much information. When I need the path info, I just type

C> CD

I was in the proper directory.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11254
    
  16

if you're in the proper directory, try this:

java -cp . MyFirstApp

that tells java to use the current directory as (part of?) the classpath.

this is a short term solution, but it might at least let you get the code to run.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Steve Davies
Greenhorn

Joined: Apr 14, 2006
Posts: 6
Thanks, Fred. I tried it and received the following:

C>java -cp . MyFirstApp
Exception in thread "main" java.lang.UnsupportedClassVersionError: MyFirstApp (Unsupported major.minor version 49.0)
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)

Maybe I should have stuck with .Net (Just kidding!)

I appreciate the help. Thanks all.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
You have compiled the source with a version 1.5 compiler, and now you are attempting to run the source with some lesser version.

You can verify this with:
javac -J-version (you will see 1.5.x)
java -version (you will see 1.y.z where y < 5)


Tony Morris
Java Q&A (FAQ, Trivia)
Steve Davies
Greenhorn

Joined: Apr 14, 2006
Posts: 6
Found it!!!

Darn Oracle SQL*Net threw in their own version of the JVM. Nice of them. I removed it from the PATH and Oila!!!

Thanks all. I appreciate the overwhelming assistance. You don't get this type of response on MSDN.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Newbie: Run-time error