yet another: Exception in thread "main" java.lang.NoClassDefFoundError - java

I have a common problem but the common solutions seems don't work for me.
I have a file called test.java in my current directory with others class files in the myclasses directory. Namely if i type
ls:
myclasses test.java
I type:
javac test.java
and all compile fine.
When i try to execute test.class and then type:
java test
i get:
Exception in thread "main" java.lang.NoClassDefFoundError: test (wrong name: myclasses/test)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: test. Program will exit.
I guess that this means that java can't locate the test.class file. But why? the file is in the current directory and all classes are in the myclasses directory. I have tried different combinations of flags (-cp , -d -sourcepath) and i have moved the test.class around the directories.
Where is the mistake?

Make sure that test.java contains public static void main method.
Also, if your class is inside myclasses, you should java myclasses.test, i believe

In addition to the above, make sure there is no package name (myclasses) declared in your .java file.

You should specify classpath for searching the Class.
java -cp . test

Related

Compiling and running a java program in command prompt

I have written a simple hello world program given below
package helloworld;
public class helloWorld
{
public static void main(String[] args)
{
System.out.println("Hello, World!");
}
}
I'm using Windows 7 and have set my CLASSPATH and PATH variables in the environment variables window as
C:\Program Files\Java\jdk1.6.0_24\bin;
The program is located at
C:\Users\Admin\Documents\NetBeansProjects\javaAssignment\src\helloWorld
So, basically when i try to run it in my command prompt, the following happens
C:\Users\Admin\Documents\NetBeansProjects\javaAssignment\src\helloWorld>javachelloWorld.java
C:\Users\Admin\Documents\NetBeansProjects\javaAssignment\src\helloWorld>java helloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: helloWorld (wrong name: helloworld/helloWorld)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: helloWorld. Program will exit.
The program seems to be compiling fine and does not throw any errors. But when i try to run it, it says than exceptions have occurred.
I tried looking up on Google thinking that setting my PATH and CLASSPATH was wrong, but I don't seem to find any issues with that.
In case your helloWorld.java file is in a package, you can run as follows :
C:\Users\Admin\Documents\NetBeansProjects\javaAssignment\src\
java packageName.helloWorld
Run the code from outside the helloworld package directory i.e from src directory (C:\Users\Admin\Documents\NetBeansProjects\javaAssignment\src)
Also check if the name of your java file is the same as your class name (helloWorld.java)
Also it seems that the name of your package is helloworld (all in small letters) however the folder name is helloWorld (note that W is capital) both the names need to match.
You need to make sure you are in the directory containing your helloWorld.class file when you issue the command
java helloWorld

Java Compile NoClassDefFoundError

So I'm trying to compile and run a problem in java and I can't get it to run. I'm just trying to do a basic package with a HelloWorld class and a main function that prints "hello world."
package helloworld;
public class HelloWorld {
public static void main(String args[]) {
System.out.println("Hello world \n");
}
}
then I call javac HelloWorld.java to compile it. Then when I try to execute the code I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong name: helloworld/HelloWorld)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Edit:
I am trying to run the code using the command "java HelloWorld"
Lets see if you understand what I did
tmp$ mkdir helloworld
tmp$ vi helloworld/HelloWorld.java
#paste the content of the class *including* package definition
tmp$ cd helloworld/
helloworld$ javac HelloWorld.java
helloworld$ java HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong name: helloworld/HelloWorld)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: HelloWorld. Program will exit.
helloworld$ cd ..
tmp$ java helloworld.HelloWorld
Hello world
Also, read this: http://www.cs.usfca.edu/~parrt/course/601/lectures/java.tools.html
Perform the following steps
1. Copy the HelloWorld.java file to a empty directory
2. javac -d . HelloWorld.java
3. java -classpath . helloworld.HelloWorld
There were 2 problems with your setup.
1. you need to use -d option while compiling. This will generate .class files according to the package structure. This is useful for running the .class file. This is so because java expects .class files in package structure.
2. specify the classpath as '.' which stands for current directory to run the program.
you can also use this
1. javac -d c:\temp HelloWorld.java
3. java -classpath c:\temp helloworld.HelloWorld
just change your classname from Helloworld to any other(like hl) and run like this (assume in e drive)
E:>cd helloworld
E:\helloworld>javac hl.java
E:\helloworld>cd ..
E:>java helloworld.hl
Hello world

Java NoClassDefFoundError in Command Line

I'm getting the NoClassDefError when I try to run my program from the command line. It works fine in Netbeans, and javac compiles correctly.
I have a class called DistributedSystem which so far is only supposed to print "hello.".
The directory is src/distributedsystem/ which contains DistributedSystem.java, and DistributedSystem.class after the compile.
If I'm inside src/distributedsystem/ and run
java DistributedSystem
then I get
Exception in thread "main" java.lang.NoClassDefFoundError: DistributedSystem (wrong name: distributedsystem/DistributedSystem)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
I also get the exact same error if I'm inside src/distributedsystem/ and run
java -cp . DistributedSystem
which is what I thought would fix the problem. I have also tried making sure classpath isn't set somewhere else, even though -cp should overwrite it. Anyone have any ideas what could be wrong?
Go to folder src then compile from there and then run
The classpath should point to the base directory. It looks as if you are attempting to run class DistributedSystem in package distributedsystem, but your classpath is set to project/bin/distributedsystem instead of project/bin.

error in java basic test program

$cat JarTest.java
package edu.testjava.simple;
public class JarTest
{
public static void main(String args[])
{
System.out.println("Hello World\n");
}
}
$javac JarTest.java
$java JarTest
I get error:
Exception in thread "main" java.lang.NoClassDefFoundError: JarTest (wrong name: edu/testjava/simple/JarTest)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: JarTest. Program will exit.
Why so?
Anything wrong with package etc?
Thanks!
You need to do:
$ java -cp . edu.testjava.simple.JarTest
You need to specify the name of the class with the full package name (in this case edu.testjava.simple). You don't need .class or .java on the end.
You can also see the answers to How to execute a java .class from the command line.
Yes, it's because of the package. Java enforces the project structure to be the same as the package structure. That means, that the class in the edu.testjava.simple package must be located in the edu/testjava/simple/ directory.
You may either:
Remove package declaration (this, however, is only acceptable in such "hello world" cases)
Compile the file using javac -d . JarTest.java (this will put the .class file in the appropriate directory) and launch it via java edu.testjava.simple.JarTest

Creating a jar file with multiple class dependencies

I am trying to run a jar file I made, I keep getting this error:
java -jar Client.jar
Exception in thread "main" java.lang.NoClassDefFoundError: Client (wrong name: Client/Client)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
I used this command
jar cmf Client.mf Client.jar Client.class Client.java Client
where Client.mf is:
Manifest-Version: 1.0
Main-Class: Client
The last argument is a folder containing all the depency classes:
ButtonListen.class
ButtonListener.class
Client$1.class
Client$2.class
Client$3.class
NewPage.class
NewPage.java
NextPage.class
NextPage.java
OptIPDemo$1.class
OptIPDemo$2.class
OptIPDemo$3.class
OptIPDemo$4.class
OptIPDemo$5.class
OptIPDemo$Doc$1.class
OptIPDemo$Doc.class
OptIPDemo.class
OptIPDemo.java
ServerStats.class
ServerStats.java
blue2.jpeg
blue3.JPG
For whatever reason it is giving me a NoClassDefFoundError, but I dont know why, or how to resolve it.
Client.class seems to be missing.
And I would get into the habit of using packages to organize your code.
You either need to compile with -d
javac -d classes
...or move all your classes into a classes/Client directory
mkdir classes/Client
mv *.class classes/Client
Then, adjust your manifest to use a package-qualified Main-Class:
Main-Class: Client.Client
And finally, build the jar so that the classes are located within the Client directory of the jar:
cd classes
jar cmf ../Client.mf ../Client.jar *

Categories