• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sudheer Kolanu
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sudheer Kolanu
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sudheer Kolanu
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21024
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic