I have two class files: Generator.class(Main) and Approach.class(Helper), which are created when I compile Generator.java code. The code is dependent on two external jars - argparse4j-0.7.0.jar and org.apache.commons.io.jar.
In normal scenario, if I need to execute the code, I would have to run below :
java -cp .:argparse4j-0.7.0.jar:org.apache.commons.io.jar Generator
I want to create a jar file so that it is easy to share a single file.
One approach that I found was, to include below in the jar file (Main.jar) -
1. Generator.class
2. Approach.class
3. manifest.txt
Manifest file : manifest.txt
Main-Class: Generator
Class-Path: argparse4j-0.7.0.jar org.apache.commons.io.jar
In this case, I need to share below 3 with the user -
1. Main.jar
2. org.apache.commons.io.jar
3. argparse4j-0.7.0.jar
To run the jar :- java -jar Main.jar
Is it possible to create a single jar file (Main.jar) with below files -
1. Generator.class
2. Approach.class
3. org.apache.commons.io.jar
4. argparse4j-0.7.0.jar
5. manifest.txt
Then only Main.jar needs to be shared with the user and it can be run using : java -jar Main.jar
I gave this a try with the same manifest file as mentioned above but the java command fails to run the jar.
Related
After converting my java program to executable jar file using commands in command prompt in windows 10,while executing jar file I am getting error:
Could find or load main class Combine.class" caused
by:java.lang.ClassNotFoundException:Combine.class
My jdk-11.0.1 has javamail api and excelapi.While executing I have set my classpath as:
classpath=%classpath%;C:\Program Files\Java\jdk-11.0.1\javamail_api\javax.mail-1.6.2.jar;C:\Program Files\Java\jdk-11.0.1\javamail_api\activation.jar;C:\Program Files\Java\jdk-11.0.1\jexcelapi\jxl.jar;.;
It was compiling and executing properly but after converting to executable jar file it is not running and giving above error.
Any help would be appreciated.
Thank you
The clue is in the exception message. It is trying to load a class with the name Combine.class. But the classes real name is Combine.
You have created the JAR file incorrectly.
echo Main-Class: Combine.class > manifest.txt
jar cmf manifest.txt FinalExecutable.jar Combine.class
If Combine is in the default package (i.e. it doesn't have a package statement) then the above should be:
echo Main-Class: Combine > manifest.txt
jar cmf manifest.txt FinalExecutable.jar Combine.class
If Combine is declared in package foo.bar, then the above should be.
echo Main-Class: foo.bar.Combine > manifest.txt
jar cmf manifest.txt FinalExecutable.jar foo/bar/Combine.class
and you need to be in the directory above the foo directory.
NB: the "Main-Class" attribute in the manifest must be a Java fully qualified class name, NOT a filename or file pathname.
It also should be noted that the CLASSPATH environment variable and the -cp argument will be ignored when you run a JAR using java -jar .... If your executable JAR depends on other JAR files, you should either combine them (to create a shaded JAR) or you should add a "Class-Path" attribute to the manifest; see https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
Finally, my advice would be to use a build tool (e.g. Maven) to compile your code, create the executable JAR file, etc rather than doing it by hand.
I'm trying to create a jar file and run it using java -cp main.jar com.test.Foo.Main but I keep getting:
Error: Could not find or load main class com.test.Foo.Main
Caused by: java.lang.ClassNotFoundException: com.test.Foo.Main
This is my file structure. So I'm thinking the line in my Main.java should be package com.test.Foo correct?
I'm compiling my Main.java with javac Main.java which outputs a Main.class file. Afterward, I create a jar file using jar cfm main.jar META-INF/MANIFEST.MF Main.class and finally while I'm in the same directory as the jar file <root>/src/com/test/Foo/ I run java -cp main.jar com.test.Foo.Main and that's when I run into the above error. Any idea how I can run this file like this (and yes I need it to run with this command specifically)?
Main.java
package com.test.Foo;
public class Main {
public static void main (String args[]) {
System.out.println("I am com.test.Foo.Main");
}
}
META-INF/MANIFEST.MF
Manifest-Version: 1.0
Main-Class: com.test.Foo.Main
I tried using some of the options given in this popular SO question and nothing helped.
The picture you're showing in your question is your project structure not your jar structure.
When you create a jar file, the structure for that jar file might be
different with your source code folder structure.
Every IDE (such as eclipse, netbeans, IntelliJ) has a mechanism for creating JAR files. In your case when you open the created jar file (using zip apps like winrar) you should see something like this :
com
|
test
|
Foo
|
Main
META-INF
|
MANIFEST.MF
This should be the ordering of your files and folders, otherwise Java can not find your main class from MANIFEST.MF
Now to solve this problem:
Open your jar file using a zip application like winrar
check the folder structure residing inside your jar file as I draw
Fix it right away within the winrar or try to correct your project structure to produce the structure I mentioned.
The class is called com.test.Foo.Main you need to specify the full name in the command:
java -cp main.jar com.test.Foo.Main
or you can use the simpler
java -jar main.jar
Check your META-INF/MANIFEST.MF for the attribute of Manifest-Version: 1.0
This attribute must be there.
Edit:
You need to move to the source root src/ and issue below command to create a valid jar.
javac com/test/Foo/*.java
and, create the jar using,
jar cmf com/test/Foo/MANIFEST.MF main.jar com/test/Foo/*.class
The thing is, package structure should match with the folder structure apparently.
I added two program jar files as input jar
1] entry.jar contains the main class of my java application created by using cmd jar cfm entry.jar Manifest.txt Mainclass.class (in manifest file -> Main-Class: Mainclass)
2] jar -cvf com.jar ./com (com package contains all other packages and class files)
and one jar file as out put file
3] out.jar
and library jars are
1] /opt/jdk1.7.0/jre/lib/jre.jar
and all external jar files needed to run my application like mail.jar,lucene.jar,driver.jar etc
I processed in proguard and got out.jar file while running it using
java -jar out.jar am getting the main class and while going to other action like login getting exception like driver not found .
How can I add driver.jar class path and my package com's classpath
I have an interface Operator in directory d:\oprinterface\Operator and four classes Plus,Minus,Multiply and Divide in diresctory d:\operators* .These classes impelemet the Operator interface . My main program (FileProcess) in directory d:\source\main\FileProcess use this interface and classes .Now I want to create 3 seprate jar file from these interface and classes . First of all I make jar file from Operator interface .I do it like so :
javac oprinterface/Operator.java
jar -cf oprInterface/Operator.class
After this step I compile and make jar file from classes Plus,Minus,Divide,Multiply .because these classes depends on oprInterface.jar I create a manifest.txt file and write on it :
Manifest-Version: 1.0
Class-Path: oprInterface.jar
And then compile and make jar file like so :
javac -cp oprInterface.jar operators/*.java
jar -cvfm operators.jar manifest.txt operators/*.class
After doing this again I create a manifest.txt file for my main program :
Manifest-Version: 1.0
Main-Class: source.main.FileProcess
Class-Path: oprInterface.jar;operators.jar
(after last line in manifest.txt I press enter)
Then I compile and make jar file like so :
javac -cp oprInterface.jar;operators.jar source/main/FileProcess.java
jar -cvfm FileProcess.jar manifest.txt source/main/*.class
When I run my jar file (java -jar FileProcess.jar) I got this exception :
NoClassDefFoundException : oprInterface/Operator
Did I make a mistake in creating jar files?
We are trying to make a jar out of our class files but somehow it does not work. The creation of the jar-file works fine with
jar -cvfm client.jar Mainfest.txt /home/pi/Desktop/Client/*.class
But when we try to run it comes with a classnodefferror. Our application is using 2 property files - one for database and one for log4j.
The directory and the one subdirectory included looks like this:
See link - image nr. 1 and 2
When we try to execute the jar file it shows this error:
(See link image 3)
Normally when we run it, we type (see link image 4)
And the manifest-file looks like this (see link image 5)
We have tried different solutions like changing paths, leaving out environment (-D) etc. and we really can't figure out what we do wrong.
There are several problems here:
It is better to explicitly mention each included JAR file in the manifest - wildcards like * isnt working too well
You can't use absolute paths in a JAR file - use relative paths instead
So, 1) change Manifest.txt to
Main-Class: Client
Class-Path: . includes/log4j-1.2.17.jar includes/mysql-connector-java-5.1.33-bin.jar includes/sqlitejdbc-v056.jar includes/RXTXcomm-2.2pre2.jar includes/..... (repeat for all pi4j JAR files)
And, 2) copy ALL the JAR files you need (including RXTXcomm-2.2pre2.jar and the pi4j JARs) into the include/ subdirectory, and change command to
jar -cvfm client.jar Manifest.txt *.class *.properties includes/*