aspose file tools*
The moose likes Beginning Java and the fly likes Exception in thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception in thread "main" java.lang.NoClassDefFoundError: MyFirstApp. Please help basic issue" Watch "Exception in thread "main" java.lang.NoClassDefFoundError: MyFirstApp. Please help basic issue" New topic
Author

Exception in thread "main" java.lang.NoClassDefFoundError: MyFirstApp. Please help basic issue

Sudheer Kolanu
Greenhorn

Joined: Nov 04, 2010
Posts: 13
Hello,

I am very new to Java. I have written MyFirstApp progrm, the below is the code. I compiled it successfully using Command Prompt but when I run it giving the below error. I have opened the class file using notepad just to see how it looks like and when ever I compile the java file the defult class file is saving as notepad file. I am not sure this is the cause of the below error. Please help




Exception in thread "main" java.lang.NoClassDefFoundError: MyFirstApp
Caused by: java.lang.ClassNotFoundException: MyFirstApp
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)
Could not find the main class: MyFirstApp. Program will exit.
Kushan Athukorala
Ranch Hand

Joined: Aug 09, 2010
Posts: 33
Hi Sudeer,

This should work.

You need to do the compilation and execution from same location

c:\test>javac MyFirstApp.java
c:\test>java MyFirstApp

Thanks,
Kushan

Kushan Athukorala
OCPJP 6.0 [86%]
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Hi Sudheer and welcome to the JavaRanch.

Please UseCodeTags when posting code. It will highlight your code and make it much easier to read. It probably will also increase the number of people helping you. I'll add them for you since you are new so that you can see the difference.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Sudheer Kolanu
Greenhorn

Joined: Nov 04, 2010
Posts: 13
Hello,

I am executing the code from the same folder. However, the same error is displayed. I don't see any issues in the code it must be something in the set up. So my opening of the class file in Note pad is not the cause for it?

I will make sure I use the CODE TAGS here after.

Thank you very much for your time.
saidi reddy
Greenhorn

Joined: Dec 25, 2007
Posts: 1
Please check the class path setting once, before you run the program, Find the details at

.http://jumpintojava.blogspot.com/2010/07/setting-class-path-to-java-on-windows.html

Once you set the classpath, type the java command in command prompt, if you find the java details then its ok, if you go the error, then one again check the path.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18965
    
  40

Sudheer Kolanu wrote:
I am executing the code from the same folder. However, the same error is displayed. I don't see any issues in the code it must be something in the set up. So my opening of the class file in Note pad is not the cause for it?



Unfortunately, you didn't provide enough information -- or to be more exact, there is something obscure going on, and what you provided isn't enough.

Tell us...

-- The location of the java and class files
-- The exact commands that you ran
-- The exact output from those commands
-- If possible, do a directory listing before and after those command too.
-- You environment variables, particularly the CLASSPATH variable, if set.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sudheer Kolanu
Greenhorn

Joined: Nov 04, 2010
Posts: 13
Hello,

I am sorry for not providing all the details:

-- The location of the java and class files
C:\Library\Headache\Pro


-- The exact commands that you ran
javac MyFirstApp.java then java MyFirstApp


-- The exact output from those commands
It complied succesfully and class file got created with first command and when I run it throwing this error
Exception in thread "main" java.lang.NoClassDefFoundError: MyFirstApp
Caused by: java.lang.ClassNotFoundException: MyFirstApp
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)
Could not find the main class: MyFirstApp. Program will exit.


-- If possible, do a directory listing before and after those command too.
Before compiling
Directory of C:\Library\Headache\Pro

11/13/2010 08:02 PM <DIR> .
11/13/2010 08:02 PM <DIR> ..
11/13/2010 09:10 AM 151 MyFirstApp.java
1 File(s) 151 bytes
2 Dir(s) 297,129,836,544 bytes free

After Compiling

Directory of C:\Library\Headache\Pro

11/13/2010 08:04 PM <DIR> .
11/13/2010 08:04 PM <DIR> ..
11/13/2010 08:04 PM 448 MyFirstApp.class
11/13/2010 09:10 AM 151 MyFirstApp.java
2 File(s) 599 bytes
2 Dir(s) 297,129,811,968 bytes free

-- You environment variables, particularly the CLASSPATH variable, if set.
C:\Program Files\Java\jdk1.6.0_22\bin


Please let me know if there anything else is missing.

I am trying to execute the first program given in Head First Java. I have installed JDK, set the class path and trying to run this program.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18965
    
  40

Sudheer Kolanu wrote:
-- You environment variables, particularly the CLASSPATH variable, if set.
C:\Program Files\Java\jdk1.6.0_22\bin


Please let me know if there anything else is missing.

I am trying to execute the first program given in Head First Java. I have installed JDK, set the class path and trying to run this program.


This last part may be the issue -- don't set the classpath.

Henry
Sudheer Kolanu
Greenhorn

Joined: Nov 04, 2010
Posts: 13
Thank you so much Henry, I have deleted the CLASSPATH from User variables and guess what It works!!!

Thank so so so much, my opinion has gone so high on JavaRanch. It rocks!!!


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception in thread "main" java.lang.NoClassDefFoundError: MyFirstApp. Please help basic issue