I'm trying to create a jar file for one of my Java files. However, once I create it and try to run it, I get a java.lang.NoClassDefFoundError.
I did the following to create the jar. The file is Worker.java and it's in src/beatDB.
jar cfm Worker.jar Manifest.txt src/beatDB
My Manifest.txt file is just
Main-Class: Main-Class: beatDB.Worker \n (with a new line afterwords)
When I run java -jar Worker.jar, I get this error.
Exception in thread "main" java.lang.NoClassDefFoundError:
beatDB/Worker
Caused by: java.lang.ClassNotFoundException: beatDB.Worker
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:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
However if I run java beatDB/Worker, the file runs fine.
Does anyone know how to fix this problem?
You should jar the manifest along with the .class files, not the .java files.
.java files contain the source code.
.class files contain the compiled code. For Java it is called byte code.
You want the package structure of your classes to be reflected in your jar file. If you have an Animal class under com.example, then your jar file should contain com/example/Animal.class
See How to create jar file with package structure? for the proper jar file package structure.
Related
I'm trying to create a JAR file of my Java code. There are four classes - A.java, B.java, C.java, and D.java. A.java has the main method. These are in a folder called src, and my manifest file is outside of src, and all of my .class files are in bin which is inside src. I created a MANIFEST.MF with the following contents:
Manifest-Version: 1.0
Main-Class: src/A
I then use the command jar cmf MANIFEST.MF A.jar src/bin/*.class src/GraphFile.txt src/ProblemA.txt src/ProblemB.txt (there are a few text files I need to include as well in my JAR file). Then, when I run java -jar A.jar ProblemA.txt GraphFile.txt (note that ProblemA.txt and GraphFile.txt are input files that my program accepts and uses), I get the following error:
Error: Could not find or load main class src.A
Caused by: java.lang.ClassNotFoundException: src.A
Can anyone explain what I am doing wrong or any steps I am missing? Unfortunately, I cannot post the actual code because this is for a class project, but I just need to get it compiled into a JAR file for submission! Thanks for any help.
I'm trying to create a proof of concept for Kotlin calling Scala code.
Here's what the project looks like at the moment:
kotlin-src/
hello.kt
scala-src/
Hello.scala
Then to compile both languages:
kotlinc kotlin-src/*.kt
scalac scala-src/*.scala
Which produces the following files in the root directory:
META-INF/
main.kotlin_module
HelloKt.class
HelloScala.class
That I attempt to turn into a JAR with:
jar cvfM run.jar *.class META-INF
However, it won't run (and I assume it's because I'm not specifying a main class).
$ java -jar run.jar
Error: Invalid or corrupt jarfile run.jar
So, I've created the following manifest:
Main-Class: HelloKt
Before compiling the jar, this file gets copied into the META-INF directory and the structure of the resulting jar is as follows:
HelloKt.class
HelloScala.class
META-INF/
META-INF/main.kotlin_module
META-INF/MANIFEST.MF
The new JAR will execute, but always fails with a runtime exception.
$ java -jar run.jar
Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
at HelloKt.main(hello.kt)
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
What are the remaining steps needed to get the JAR compiled and running the main method in the Kotlin class?
Are there runtime classes that need to be bundled with the Scala/Kotlin code to make this work?
I haven't tested it, but it should work:
Compile your code as you have
Download kotlin-stdlib jar from Maven Central
Verify that you used the same version of the stdlib while compiling
Add directory with the stdlib jar to the classpath parameter of the java command when running the jar
I am working on a little java program which I want to run on a unix machine.
First, I compiled the .java file without any errors.
javac CallACMDFromJAVA.java
Then my .class file was created.
CallACMDFromJAVA.class
Then I used the jar command to create an executable .jar file.
jar cfm cmd.jar manifest.txt CallACMDFromJAVA.class
So far so goo. The last cmd gave me a cmd.jar file.
But when I want to run it, some error suddenly appears. I typed:
java -jar cmd.jar
The Error:
Exception in thread "main" java.lang.NoClassDefFoundError: CallACMDFromJAVA (wrong name: serviceCall/CallACMDFromJAVA)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
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)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
Do you need more information about java environment or something else to help me?
Your class is apparently in a package serviceCall. Ideally, you should make your source directory structure match the package structure, so you have the source file in a directory called serviceCall. Even without that, if you compile it with
javac -d . CallACMDFromJAVA.java
then that should create a serviceCall directory containing CallACMDFromJAVA.class. You should try to run this directly before putting it in a jar file:
java serviceCall.CallACMDFromJAVA
... that should work, whereas java CallACMDFromJAVA would fail with the same error using your existing workflow. It's always worth trying it without the jar file before putting it in a jar file.
You then need to change your Main-Class in the manifest to serviceCall.CallACMDFromJAVA, and create the jar file to include the serviceCall directory, e.g.
jar cfm cmd.jar manifest.txt serviceCall
Then it should work. Basically, you need your Main-Class attribute to refer to the fully-qualified class name, and you need the corresponding class file to be in a suitable directory within the jar file.
Note that if you use a good IDE, all of the directory structuring is likely to be handled for you.
I have tried to create Jar file using Command Line.
My Manifest file:
Manifest-Version: 1.0
Created-By: 1.6.0 (Sun Microsystems Inc.)
Main-Class:Home
My Files and Location of that files:
Location : D:\Application
Files :
images
add.png
home.png
minus.png
Database.java
Home.java
UiDesign.java
Database.class
Home.class// This is my main class
UiDesign.class
Manifest.txt
mysql-connector-java-5.1.15-bin.jar
To create jar file i tried:
D:\Application>jar cmf Manifest.txt MyApp.jar *.class mysql-connector-java-5.1.15-bin.jar images
But the jar file is created. If i click that jar file, Error message shows like below,
Failed to Load Main-Class manifest attribute from
D:\Application\MyApp.jar
Thank for all....My problem solved by adding new line in Manifest File....Thank you all...
But now i have another problem.....
D:\JavaApplication-13-8-2011\Application>jar cfm MyApp.jar Manifest.txt *.class
mysql-connector-java-5.1.15-bin.jar images
D:\JavaApplication-13-8-2011\Application>java -jar MyApp.jar
Connect to MySQl
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
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)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at Database.getDBConnection(Database.java:14)
at UiDesign.<init>(UiDesign.java:58)
at Home.main(Home.java:6)
java.lang.NullPointerException
at Database.getBrand(Database.java:31)
at UiDesign.<init>(UiDesign.java:59)
at Home.main(Home.java:6)
Exception in thread "main" java.lang.NullPointerException
at UiDesign.<init>(UiDesign.java:64)
at Home.main(Home.java:6)
D:\JavaApplication-13-8-2011\Application>
I think this problem occurs due to to class path not set to mysql-connector....I have added this jar file in my Application.....How to setclass path and run my Application jar file successfully.....
please help me....
Thank you...I cleared..this problem also...As Trisstan said, I added classpath in Manifest file...Now My Application Jar file Run Succesfully.....
Thank You all for your Quick Response......
Add a newline at the end of your manifest file.
Warning: The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return.
From: http://download.oracle.com/javase/tutorial/deployment/jar/appman.html
It may be an error about classpath, because your jar depends on mysql-connector-java-5.1.15-bin.jar
make sure there is a carriage return in your manifest file and I am also guessing your jar file name should precede the manifest file as your option says cfm.
Failed to Load Main-Class manifest attribute from D:\Application\MyApp.jar
From the error it looks like it thinks that the jar file is your manifest file. That should be the problem.
update the argument or change the option parameter to mcf I guess..
For your second problem:
Your jar probably contains a copy of the mysql jar. That does not work because jars are supposed to contain classes. The simplest way to fix this is to indicate in your manifest that your code depends on the mysql jar. Basically add the following:
Class-Path: mysql-connector-java-5.1.15-bin.jar
In your jar creation command, do not include the mysql jar. Finally, make sure that your jar and the mysql one are distributed together (in the same directory).
If you really want a single jar, extract the mysql jar and include its contents in the jar you are going to distribute.
I need to create jar file of my java app. For that first i had created a file called Manifest.MF , in that file i had stored the following code
Manifest-Version: 1.0
Main-Class: com.demo.test.JavaTest
and then i had exceuted the following command in command prompt
jar cvfm JavaAppDemo.jar MANIFEST.MF "C:\JavaSamples\MyApp"
where MyApp is my project directory, there i had created packages and used other jar files too
and then i try to run the jar using java command
java -jar JavaAppDemo.jar
and i got following Exception
Exception in thread "main"
java.lang.NoClassDefFoundError:
com/demo/test/JavaTest
Caused by:
java.lang.ClassNotFoundException:
com.demo.test.JavaSamp
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:
com.demo.test.JavaTest. Program will
exit.
pls tell me, i want to create a jar and i have to bundle the files in my project directory to that jar files, when i extracted the jar i got my project folder, but when i run my jar i got the above exception so i got confused where the loop hole is present.
You should probably use this syntax for building your jar :
jar cvfm myapp.jar myapp.MF -C classes myclasspath
Okay well a few things:
Here is how you should run your application:
java -classpath myapplication.jar:.. -Dlog4j.info -Dlog4j.configuration=file:../../conf/log4j.xml com.mywebsite.Benchmarks
Take that log4j stuff out if you don't need it. I am sure you are just missing the "com.mywebsite.Benchmarks" line which tells the jar which compiled class to start with. If you have Benchmarks, FullTest, MainApp all compiled in your jar and the main method is in Benchmarks, you need to tell the jar that.
As far as building your jar...
It is the easiest to use some kind of ant build script.
<target name="build">
<delete file="MyApplication.jar" />
<delete dir="_classes" />
<mkdir dir="_classes" />
<javac srcdir="src" destdir="_classes" debug="true">
<classpath path="../../3rdParty/log4j/log4j-1.2.16.jar" />
</javac>
<jar jarfile="MyApplication.jar" basedir="_classes"/>
</target>
That should get you started. But you need to make sure of a few things when your run it:
You have all the dependencies on your class path.
You tell the jar which class to start with.
Some things I can think of:
The error message says that it can't find the file com/demo/test/JavaTest.class. You could try looking at your jar file with a zip file program (a .jar file is compressed just like a .zip file) to see if that file is really not in there. I suspect the error code is actually correct in that it is not actually there, but at least you can see it (or not see it) with your own eyes. And remember that it has to begin with a "com" folder.
Perhaps you are confusing .class files and .java files. Are you sure your .java files are actually compiled into .class files? This might be unlikely, but it may as well be said.
Maybe you need to run your jar command with "C:\JavaSamples\MyApp\*" at the end to pick out all the files in the right directory. Maybe you were telling the jar command to include the folder "MyApp". You don't actually want that folder; you want all the files inside it.