exception in thread 'main' java.lang.NoClassDefFoundError - java

When I try to execute the following program from DOS I get the results below..
The following program is in C:\Users\Apostolos\Documents\NetBeansProjects\Java1\src\java1
package java1;
public class MyProgram{
public static void main(String[] args){
System.out.println("Rome wasn’t burned in a day!");
}
}
javac MyProgram.java
works fine
But java MyProgram gives the following:
Exception in thread "main" java.lang.NoClassDefFoundError: MyProgram (wrong name
: java1/MyProgram)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
................
Why this is happening??
My environment variables:
CLASSPATH: .;C:\Program Files (x86)\Java\jre7\lib\ext\QTJava.zip;C:\Program Files\Java\jdk1.7.0_05\bin
PATH: C:\Program Files\Java\jdk1.7.0_05\bin
JAVA_HOME: C:\Program Files\Java\jdk1.7.0_05
I have seen similar problems here but i cannot find the solution to my problem.
Thank you in advance!

This is caused when there is a class file that your code depends on and it is present at compile time but not found at runtime. Look for differences in your build time and runtime classpaths.
Refer this Link

2 points you should keep in mind when using java tool:
Add the class to the classpath.
Use the fully qualified name of the class to be run.
Hence:
java -cp C:\Users\Apostolos\Documents\NetBeansProjects\Java1\bin java1.MyProgram
assuming the following file exists after compilation:
C:\Users\Apostolos\Documents\NetBeansProjects\Java1\bin\java1\MyProgram.class
For more info, see:
Mastering the Java CLASSPATH.
"Setting the class path" Documentation.

NoClassDefFoundError in Java comes when Java Virtual Machine is not
able to find a particular class at runtime which was available during
compile time. For example if we have a method call from a class or
accessing any static member of a Class and that class is not available
during run-time then JVM will throw NoClassDefFoundError.
Obvious reason of NoClassDefFoundError is that a particular class is not available in Classpath, so we need to add that into Classpath or we need to check why it’s not available in Classpath if we are expecting it to be. There could be multiple reasons like:
Class is not available in Java Classpath.
You might be running your program using jar command and class was
not defined in manifest file's ClassPath attribute.
Any start-up script is overriding Classpath environment variable.

Try in this way
run command prompt as Administrator, and
cd C:\Users\Apostolos\Documents\NetBeansProjects\Java1\src
then
javac java1/MyProgram.java
Then
java java1.MyProgram
This will work.

Exception in thread "main" java.lang.NoClassDefFoundError: MyProgram (wrong name
This exception is thrown when the JVM cannot finf your class at run time
From C:\Users\Apostolos\Documents\NetBeansProjects\Java1\src
execute " "java java1.MyProgram" –

Related

Why adding class path caused main class not found?

I get a NoClassDefFoundError when trying to run a Java class without providing the proper class path. However when added the needed class path, java complaints it cannot find the main method. If you have any idea of what's happening here, please point me in the right direction. Thank you
$ java MyClass
Exception in thread "main" java.lang.NoClassDefFoundError: cern/colt/matrix/DoubleMatrix1D
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2615)
at java.lang.Class.getMethod0(Class.java:2856)
... 6 more
$ java -cp resources/colt.jar MyClass
Error: Could not find or load main class MyClass
Assuming MyClass isn't in a package, you need something like (on Windows)
java -cp resources/colt.jar;. MyClass
or (otherwise)
java -cp resources/colt.jar:. MyClass
To also include the current directory. Alternatively, you can set the CLASSPATH environment variable.
On Windows,
set "CLASSPATH=resources/colt.jar;."
otherwise something like (depending on your shell)
export CLASSPATH="resources/colt.jar:."
then
java MyClass
try to include the current directory in the classpath as well. usually we add new jars to classpath liek this:
java -cp %CLASSPATH%;resource/colt.jar MyClass
or on Linux as:
java -cp $CLASSPATH:resource/colt.jar MyClass
Additionally u can also add .
i.e the currrent directory to the classpath.

noclassdeffound error in java, linux

running a java class by shell script:
java -cp $CLASSPATH CG_GpsRequest "dbname","oracle.jdbc.driver.OracleDriver","abc","abc"
while running the script in unix, getting error,
Exception in thread "main" java.lang.NoClassDefFoundError: CG_GpsRequest
Caused by: java.lang.ClassNotFoundException:
CG_GpsRequest is the class file name.
Okay.. If I understand you correctly..
I think its problem with the Package name specification..
Your calss CG_GpsRequest you must have specified in Packaging way for example
com.xxx.yyy.CG_GpsRequest
So I think JVM is searching your class com.xxx.yyy.CG_GpsRequest in this patter so Please run your script with the following modifications.
java -cp $CLASSPATH com.xxx.yyy.CG_GpsRequest "dbname","oracle.jdbc.driver.OracleDriver","abc","abc"
This is just assumption that you have created your class in Pacakgin hirarechy since you havent specified more information.
Add the folder/path in which your java class exists to your CLASSPATH

Java NoClassDefFoundError even though the class exists

I'm trying to figure out how class importing works in Java. I have the following file structure:
testProject
pkgA
A.java
A.class
dir
pkgB
B.java
B.class
The contents of A.java are:
package pkgA;
public class A {
public static String funcA() {
return "funcA in class A in pkgA";
}
}
The contents of B.java are:
package pkgB;
import pkgA.A;
public class B {
public static void main(String[] args) {
System.out.println((new A()).funcA());
}
}
In testProject I run:
javac pkgA/A.java
The above command doesn't print anything.
In testProject/dir I run:
javac pkgB/B.java -classpath ..
The above command doesn't print anything.
In testProject/dir I run:
java pkgB/B -classpath ..
The above command prints the following:
Exception in thread "main" java.lang.NoClassDefFoundError: pkgA/A
at pkgB.B.main(B.java:7)
Caused by: java.lang.ClassNotFoundException: pkgA.A
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 1 more
What am I doing wrong?
Giving the classpath as an absolute path doesn't help.
$ java -version
java version "1.7.0_51"
OpenJDK Runtime Environment (IcedTea 2.4.4) (7u51-2.4.4-0ubuntu0.12.04.2)
OpenJDK 64-Bit Server VM (build 24.45-b08, mixed mode)
OS is Ubuntu 12.04
Thanks!
Note that your classpath spec must preceed classes or .java files e.g.
java -classpath .. pkgB/B
(the same applies to the javac invocation).
I would compile everything at the same time e.g.
javac -classpath {whatever} {complete list of .java files}
for consistency's sake.
Specify the compiler output directory to be separate to your source (e.g. a directory called classes). That makes life simpler in terms of managing your code and the compiled artifacts.
Going forward, you should investigate a build tool such as Maven or Ant (or Gradle or Sbt etc.). That will make life much more manageable as you add source files, config or dependencies.
Check your classpath and make sure the current directory(directory that holds pkgA and pkgB) is in the classpath.
It's not always about the class you first see, it's also about what's in them.
1) You have a class called "com.my.Clazs".
2) The class imports "com.my.other.Clazzzz";
3) You have a static method inside Clazs
public static void doFoo() {
/* The call to Clazs.doFoo() will result in NCDF Error if you
* skip Clazzzz lookup in classpath.
*
*/
Clazzzz anObject = new Clazzzz();
}
if you are trying do something from Clazs statically, it will give you NoClassDefFoundError because it cannot load all the dependencies fully to qualify this as a valid class. In other words, all the URLs cannot be resolved fully.
This kind of problem is not unusual even when you have dependency manager e.g. Maven. You might forget that you have a compile-time dependency on certain jars/classes (e.g. Java Tools.jar) which you also need to load when you start the application.

cannot run a simple java code

I have downloaded a java developers kit for my 64bit windows 7, wrote down my code in the notepad, though the code is compiling from the command prompt and creating a .class file, but its refusing to run showing the error code:
java.lang.NoClassDefFoundError: first Caused by: java.lang.ClassNotFoundException: first
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: first. Program will exit. Exception in thread "main"
I have made sure more than once that the file name and the class name are exactly same(i have kept them smallcase 'a' just to be sure). But still no avail, could you please suggest a few solutions please.. I'm new to java i'm basically a C/C++ programmer.
A java program has this basic structure:
ClassName.java
public class ClassName
{
public static void main(String[] args)
{
}
}
try using this outline to generate your code.
compile and run with:
javac ClassName.java
java ClassName
I used to get this error when I ran a Class file.
Try doing: java NameOfClass
You don't need the .java extension when running, but you need it for compiling. That was always the problem I used to have.
Did you set your classpath?
http://download.oracle.com/javase/1.3/docs/tooldocs/win32/classpath.html
java -classpath <path> <classname>
You must provide with the code.
Anyway, from the Java Docs, class ClassNotFoundException:
Thrown when an application tries to
load in a class through its string
name using:
The forName method in class Class.
The findSystemClass method in class ClassLoader.
The loadClass method in class ClassLoader.
but no definition for the class with the specified name
could be found.
Must read link : Tip: Causes of java.lang.ClassNotFoundException
It is quite possible after compiling your code you would be writing java filename.java.
Because this sought of exception occurs then.
After compiling your program using javac filename.java use the command to start your interpreter java filename.
As you enter this command java interpreter automatically starts interpreting filename.class.
commands :
javac filename.java // to start compiling
java filename // to start interpreting

could not find the main class

I'm using the JDK 3.1. I am using XML Publisher. I'm getting this error:
Could not find the main class. Program will exit.
After I click on "OK", I get
Java execution failed. Please check the Java Option in the option dialog
Sounds like you're trying to execute .jar file and there's no Main-Class entry in the manifest file. Other than that obvious point, your question does not give much information for assistance.
Your question is tricky to understand, but I'm guessing that you haven't actually compiled your Java code, or your compiled code isn't on the classpath.
When I try and execute a non-existent class (this would work if there was a MyClass.class on the classpath with a main() method):
paul#paul-laptop:~$ java MyClass
Exception in thread "main" java.lang.NoClassDefFoundError: MyClass
Caused by: java.lang.ClassNotFoundException: MyClass
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: MyClass. Program will exit.
Is that what you're seeing? If so:
compile your class with javac if you haven't done so
check that your classpath includes the location of the class
(You can specify the classpath explicitly when you execute the java program using -classpath, check the documentation for details.)
I include this answer as someone who has made an error someone consuming java rather than programming in it would make:
On the command line when executing a JAR file, be sure your line reads
java -jar whatever.jar
instead of
java whatever.jar
Without the -jar you sometimes get the "Could not find the main class" error.
Here are some good answers What does "Could not find or load main class" mean?
But, I will share one possibility I had. I used the JDK1.7 to compile my code and run the jar package using the JDK1.6, the error is:
Could not find the main class. Program will exit.
So, check if the JDK version you used to run your code is lower than that used to compile your code.
Check this website: "Could not find main class" error when previewing BI Publisher for Word. It directly references Java issues with Oracle BI Publisher Plugins for Word.
It basically says that you need to set your Java Home by going to Options in the BI Publisher Tab in the MS Office Ribbon.

Categories

Resources