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 *
Related
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.
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
$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
I am currently trying to make a Jar with all my libraries included. What I have done, I have created folders like this :
eancopy (which contain all my classes)
lib (containing all my libraries : mongo, jedis...)
META-INF/MANIFEST.MF
My main class is named process (within the package eancopy)
My manifest is like this:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Class-Path: lib\commons-pool-1.5.6.jar lib\jedis-2.0.0.jar lib\mongo-2.6.3.jar
Created-By: 1.6.0_24-b07 (Sun Microsystems Inc.)
Main-Class: eancopy.process
I have generated the JAR with this command :
jar cvmf META-INF/MANIFEST.MF EANcopy.jar eancopy/*.class lib/*.jar
My problem : When executing the JAR by java -jar EANcopy.jar, it works when it is executed in the place where I have generated the JAR but in another place I have the message :
Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/DBObject
Caused by: java.lang.ClassNotFoundException: com.mongodb.DBObject
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
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: eancopy.process. Program will exit.
Any ideas ? Thanks
This exception shows you the cause of the problem:
Exception in thread "main" java.lang.NoClassDefFoundError: com/mongodb/DBObject
You haven't specified these libs lib\commons-pool-1.5.6.jar lib\jedis-2.0.0.jar lib\mongo-2.6.3.jar in your CLASSPATH environment variable, hence why it fails when run in other places.
I got some problems with the java. Check it out.
sebastian#sebastian-desktop:~/scaaaaaaaaala$ java -cp /home/sebastian/.m2/repository/org/scala-lang/scala-library/2.8.0.RC3/scala-library-2.8.0.RC3.jar:target/scaaaaaaaaala-1.0.jar scaaalaaa.App
Hello World!
That's cool, right, but how bout this:
sebastian#sebastian-desktop:~/scaaaaaaaaala$ java -cp /home/sebastian/.m2/repository/org/scala-lang/scala-library/2.8.0.RC3/scala-library-2.8.0.RC3.jar -jar target/scaaaaaaaaala-1.0.jar
Exception in thread "main" java.lang.NoClassDefFoundError: scala/Application
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)
at scaaalaaa.App.main(App.scala)
Caused by: java.lang.ClassNotFoundException: scala.Application
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
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)
... 13 more
Wat the heck? Any idea why the first works and not the 2nd? How do I -jar my scala??
My thanks in advance, brother.
If you define -jar the -classpath is ignored:
Java manual:
-jar When you use this option, the JAR file is the source of all user
classes, and other user class path
settings are ignored.
You can define the classpath dependencies in the Manifest metadata.
The easiest way to start your app is using the scala scripts:
scala -classpath target/scaaaaaaaaala-1.0.jar scaaalaaa.App Hello World!
Simply running
scala scaaaaaaaaala-1.0.jar
works for me with 2.11.6
use sbt-assembly sbt plugin to produce a jar containing all dependencies
sbt-assembly github
e.g. add a line in project/plugins.sbt:
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.10")
adjust your build.sbt e.g. with
mainClass in (assembly) := Some("mypackage.mysubpackage.MyMainClass")
assemblyJarName in assembly := "my_assembly.jar"
then build your assembled jar with
sbt assembly
and launch it with
java -jar ./target/scala-2.12/my_assembly.jar
Et voilĂ , no class-path this & thats needed any more. only 1 jar.
For an executable jar, the classpath should be in the jar's manifest. For help on doing that, look through the answers to Stackoverflow: How to create an executable jar with dependancy jars.
Below is the way to execute the Scala Executable Jar
We need scala installed in your system.
Here first will give the jar path and jar name
If your code needs any dependency jar then keep all jar in one directory and give a path of this in command like below after the ":".
At last give your class/object name.
scala -cp "/your/jar/path/jar_name.jar:/your/dependency/jar/path/*"
SampleCode