Win a copy of Svelte and Sapper in Action this week in the JavaScript 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

Trying to run my first program: java.lang.NoClassDefFoundError

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have Windows 7.
I am running JDK 6 update 26, 64-bit.
I have my PATH variable set to C:\Program Files\Java\jdk1.6.0_26\bin

This is my program:


The file HelloWorld.java is located on my desktop.

I Run:

C:\Users\Eric\Desktop>javac HelloWorld.java

//HelloWorld.class is created on my desktop

C:\Users\Eric\Desktop>java HelloWorld.class
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld/class
Caused by: java.lang.ClassNotFoundException: HelloWorld.class
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: HelloWorld.class. Program will exit.



Does anyone have any ideas why this is happening?

Thank you,
Eric

 
Sheriff
Posts: 11343
Mac Safari Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch!

When you run java from the command line, supply the class name without the ".class" extension. Just type...

java HelloWorld
 
Greenhorn
Posts: 21
Oracle Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with marc, when you compile you need to do:
javac <yourfilename>.java
and while running it you just need to do:
java <yourfilename>
 
Greenhorn
Posts: 14
PHP Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, but make sure the file name is in same font case because java is case sensitive....
In your example, you should not use >java helloworld because it is not in same case where your class is defined.
>java HelloWorld is the correct format
 
Ranch Hand
Posts: 174
Java ME Opera Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you're still getting this no class def found error, try running in this way: java -cp . HelloWorld
 
zameel amjed
Greenhorn
Posts: 14
PHP Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read This
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



please explain me. whether this program could be run or not.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Saorwumni Arun, welcome to the Ranch.

In order to be able to run your Java program as a stand-alone program, it must have a public static void main(String[] args) method. In your code, the method is called class1 instead of main. So, you cannot run this - if you try, Java will complain that it cannot find the main method.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The programmer just did java HelloWorld.class.

just javac file.java

then

java file
 
Saorwumni Arun
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you jesper de jong
 
Saorwumni Arun
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am using jre 1.6 and jdk 6. when run my program the error message occur.the message is "Exception in thread "main" java.lang.NoClassFoundError"


please help me out this problem
 
Eric Fancis
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

marc weber wrote:Welcome to the ranch!

When you run java from the command line, supply the class name without the ".class" extension. Just type...

java HelloWorld



Late response, but thanks!
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope you got the solution for your problem but still I would like to add something which is some times more important in debugging such problems.
Even if you try to run code using
java <fileName>

and you get NoClassDefFoundException error, then first thing you should check is your system classpath. In case your system classpath is not pointing to current directory then it may give you an error.
You can check classpath in DOS using command
set classpath

and you can set the classpath as

set classpath=%classpath%;.;

here .(dot) indicates current directory. So what would happen when you will try to run the program it will search .class file in current directory also.

or in short you can use the solution as mentioned by

zameel amjed wrote
if you're still getting this no class def found error, try running in this way: java -cp . HelloWorld

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rajat Jindal wrote:I hope you got the solution for your problem but still I would like to add something which is some times more important in debugging such problems.
Even if you try to run code using
java <fileName>



That would be an error. The argument to the java command is a fully qualified class name, not a file name.

and you get NoClassDefFoundException error, then first thing you should check is your system classpath. In case your system classpath is not pointing to current directory then it may give you an error.
You can check classpath in DOS using command
set classpath

and you can set the classpath as

set classpath=%classpath%;.;



It's better not to use the CLASSPATH environment variable at all. Just use -cp or -classpath on the command line.
 
Rajat Jindal
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. <fileName> meant to be fully qualified file name
2. You cant escape yourself by setting classpath on command line, in many situations you always have a classpath defined in your system environmental variables, so better way is to just make a check.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rajat Jindal wrote:1. <fileName> meant to be fully qualified file name



Class name, not file name. They are two different things.

2. You cant escape yourself by setting classpath on command line, in many situations you always have a classpath defined in your system environmental variables, so better way is to just make a check.



The command line one will override the environment variable. Different apps need different classpaths, and some installers (QuickTime) mess with your CLASSPATH environment variable. It really is better to just ignore it completely and always specify the exact classpath you want on the command line.
 
Rajat Jindal
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jeff Verdegan wrote
Class name, not file name. They are two different things.



I mean the <class> file Name which contains main method in case of core java.

The command line one will override the environment variable. Different apps need different classpaths, and some installers (QuickTime) mess with your CLASSPATH environment variable.



In case you are using a set of packages then you need to set classpath every time on command line for those packages which is quite difficult. For instance one classpath is pointing to some Database driver jar, other belongs to some 3rd party libraries and one which belongs to your self set of classes. So I would say its better to set it in environmental variable but always remember it and dont forget it to check once
For small programs your solution is best.
 
Marshal
Posts: 70266
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rajat Jindal wrote: . . . So I would say its better to set it in environmental variable . . .

Disagree. If you set a classpath environment variable for every possible application, that environment variable will expand and become too large for you to handle.
 
Rajat Jindal
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not saying, it's a good practice to set classpath in environmental variable but I am saying, you cant escape out because sometimes new applications installed can also set classpath in the system and if you don't check that it will also create problem, moreover, in case you are developing application which needs both servlet-jsp jars as well as jdbc driver jars, wouldn't it be a better option to set in classpath temporarily ??

 
Campbell Ritchie
Marshal
Posts: 70266
282
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. You should not set a classpath ever. If you find something else has set a classpath, you will need to amend that classpath by adding . for current directory, probably as its first entry.
I think it is better to use the command line to set the classpath; repeatedly altering a system environment variable is liable to introduce errors into it.
You can use the -cp option and once you have found the correct classpath, you can retrieve that command for reuse with the ↓ and ↑ keys.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Issue-java.lang.NoClassDefFoundError
Root Cause- Incorrect Java path set in Environment Variable Section
Solution- Set correct JAVA_HOME Path
Steps->Environment Variable Setting (My Comp-Right Click ->Properties->Env Variable->Advance Tab ->Variable)
1) Create new JAVA_HOME Environment Variable.
JAVA_HOME .;C:\Program Files (x86)\Java\jdk1.6.0_14
Amend JAVA_HOME classpath by adding . for current directory
2) Set JAVA_HOME variable in PATH Variable section.
PATH %JAVA_HOME%\bin
3) Set JAVA_HOME variable in CLASSPATH Variable
CLASSPATH %JAVA_HOME%\jre\lib
4) Restart System
5) Verify all variable
echo %CLASSPATH%
echo %JAVA_HOME%
echo %PATH%
6) Compile java class javac Test.java
7) Run Java program java Test
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jaibardhan Ruwari, thanks for you answer - but what you propose (setting JAVA_HOME) is not a solution for the problem.

In fact, Java itself does not use the JAVA_HOME environment variable at all, so setting it has no effect. Only some third-party software (Apache Tomcat, for example), uses JAVA_HOME to locate the Java runtime environment.

Setting JAVA_HOME will not solve a NoClassDefFoundError.
 
catch it before it slithers away! Oh wait, it's a tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic