The following program is throwing error:
public class HelloWorld {
public static void main(String args[]) {
System.out.println("Hello World!");
}
}
CLASSPATH C:\Program Files\Java\jdk1.6.0_18\bin\
Path C:\Program Files\Java\jdk1.6.0_18\bin\
JAVAHOME C:\Program Files\Java\jdk1.6.0_18\bin
Can you please tell me the root cause?
This question is related to
java
noclassdeffounderror
I found one another common reason. If you create the java file inside a package using IDE like eclipse, you will find the package name on the top of your java file like "package pkgName". If you try to run this file from command prompt, you will get the NoClassDefFoundError error. Remove the package name from the java file and use the commands in the command prompt. Wasted 3 hours for this. -- Abhi
if your Program.java is in "src/mypkg/subpkg/" directory:
go to "src" dir
Then to compile use "javac mypkg/subpkg/Program.java"
To run use "java mypkg.subpkg.Program.class"
package com.beans;
public class Flower{
.......
}
packages :=> com.beans,
java class Name:=> Flower.java,
Folder structure (assuming):=> C:\com\beans\Flower.*(both .java/.class exist here)
then there are two ways of executing it:
1. Goto top Folder (here its C:\>),
then : C:> java com.beans.Flower
2. Executing from innermost folder "beans" here (C:\com\beans:>),
then: C:\com\beans:> java -cp ./../.. com.beans.Flower
Add the jar in your classpath.
For example, if you have NoClassDefFoundError
for class XXXXXX
, get the jar containing the class and add it to your classpath manually.
It works for me. Hopefully it will work for you as well.
Try doing
javac Hello.java
and then, if it comes up with no compiler errors (which it should not do because I cannot see any bugs in your programme), then type
java Hello
Easy & Simple solution: I solved this problem (NetBeans) by exporting the original project into zip file, deleting the original project directory and importing the project back from the zip file.
The Problem here is the setting the environment and the running of the class file. a. To set the environment path run the following command: set path=C:\Program Files (x86)\Java\jdk1.7.0\bin b. run the program from the package like com.test.TestJavaClass
Command: java com.test.TestJavaClass
The general issue here is we run it either from inside the package like src/package/name. We should not include src, package name is enough.
I had the same problem, and stumbled onto a solution with 'Build Main Project F11'. The ide brought up an "option" that I might want to uncheck 'Compile on Save' in the Build > Compiling portion of the Project configuration dialog. Unchecking 'Complile on Save' and then doing the usual (for me) 'Clean and Build' did the trick for me.
This is the long form of the Java commands that can be run from a Windows command prompt:
"C:\Program Files\Java\jdk1.6.0_18\bin\javac.exe" -classpath "C:\Users\Scott\workspace\myproject" com\mycompany\myapp\HelloWorld.java
"C:\Program Files\Java\jdk1.6.0_18\bin\java.exe" -classpath "C:\Users\Scott\workspace\myproject" com.mycompany.myapp.HelloWorld
Notice the classpath has no slash at the end. The javac.exe commands expects the file to end with ".java". The java.exe command expects the full class name and does not end with ".class".
There are a few ways to simplify these commands:
You don't have to enter the entire classpath (ie, you can just use -classpath "."). Enter the directory you will be working in:
cd "C:\Users\Scott\workspace\myproject\"
You can use the default package (put the HelloWorld.java file directory in your working directory and don't use the Java package directive)
If you make these changes you would run something like this (and you might be able to leave out -classpath "."):
cd "C:\Users\Scott\workspace\myproject\"
javac -classpath "." HelloWorld.java
java -classpath "." HelloWorld
type the following in the cmd prompt, within your folder:
set classpath=%classpath%;.;
Exception in thread "main" java.lang.NoClassDefFoundError
One of the places java tries to find your .class
file is your current directory. So if your .class
file is in C:\java
, you should change your current directory to that.
To change your directory, type the following command at the prompt and press Enter:
cd c:\java
This .
tells java that your classpath is your local directory.
Executing your program using this command should correct the problem:
java -classpath . HelloWorld
The CLASSPATH
variable needs to include the directory where your Java programs .class file is. You can include '.' in CLASSPATH
to indicate that the current directory should be included.
set CLASSPATH=%CLASSPATH%;.
If your package is helloworld
you would go to parent dir of your package then run:
java helloworld.HelloWorld
Had the same problem tried above solutions but none worked. I had to go through my java code only to find that the main function could not be recognised since there was no space btw it and (String) ie initial code:
public static void main(String[]args){
working code.
public static void main (String[]args){
Hope I helped someone.
I had this error because I had my files within a package. So my foo package I had to call like:
java foo.HelloWorld
You can find information about required libraries inside pom.xml
, it is much easier to use tools like Apache Maven to build java applications.
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.20</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>24.0-jre</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-assistedinject</artifactId>
<version>4.2.0</version>
</dependency>
I spent four hours trying various permutations & search suggestions.
At last, found this post that worked but not the best solution to change the original code to test it.
set classpath=JDKbin;JDKlib;JREbin;JRElib;myClassLib;.
java <packageName>.<className>
also tried the java ..\<packageName>.<className>
Neither worked.
However first response alone worked. Thank you so much Abhi!!!
"I found one another common reason. If you create the java file inside a package using IDE like eclipse, you will find the package name on the top of your java file like "package pkgName". If you try to run this file from command prompt, you will get the NoClassDefFoundError error. Remove the package name from the java file and use the commands in the command prompt. Wasted 3 hours for this. -- Abhi"
Curious if there is any other way to make it work without having to change the original code? Thank you!
I finally found this as a bug with Apache Netbeans editor:
Below steps will remove the error:
Hope that helps, if you are using new Apache Netbeans (not old Netbeans)
The javadoc of NoClassDefFounError itself would be a good start (here), and then I'll suggest you clean and rebuild your project.
If you want to 'compile and execute' any java file that you have created using any IDE(like eclipse), just run the below commands:
Compile:
javac Users\dhiraj01\workspace\Practice\src\PracticeLogic\Logics.java
Execute:
java -classpath Users\dhiraj01\workspace\Practice\src\ PracticeLogic.Logics
Here is what finally worked.
`@echo off
set path=%path%;C:\Program Files\Java\jdk1.7.0_71\bin;
set classpath=C:\Program Files\Java\jdk1.7.0_71\lib;
cd <packageDirectoryName>
javac .\trainingPackage\HelloWorld.java
cd ..
java trainingPackage.HelloWorld
REM (Make sure you are on the parent directory of the PackageName and not inside the Packagedirectory when executing java).`
Your CLASSPATH
needs to know of the location of your HelloWorld
class also.
In simple terms you should append dot .
(means current directory) in the CLASSPATH
if you are running javac
and java
commands from DOS prompt.
Run it like this:
java -jar HelloWorld.jar
Source: Stackoverflow.com