Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

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

 
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.
 
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
 
Bartender
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.
 
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.
 
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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!!!


 
I am going to test your electrical conductivity with this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic