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?
Related
I have below project structure:
->bin
->lib
->resources
->src
->DemoFramework
->FirstDemo.java
In lib folder, I have an external jar that I need in my application. Its name is ext.jar. In resources, I have Manifest.txt file, whose content is given below.
Manifest-Version: 1.0
Class-Path: . lib/ext.jar
Main-Class: DemoFramework.FirstDemo
I am using below command to generate jar:
javac -cp ".;./lib/ext.jar" src/DemoFramework/*.java -d bin
Basically I am putting all class files into bin folder so that in final jar file, source code is not visible.
Then I am issuing below command:
jar cmf resources/Manifest.txt project.jar bin lib
The jar file is successfully created but when I run it, it says:
no main manifest attribute, in project.jar
I am confused about this error, no idea why it is happening.
Can you guys help me to sort it out?
Thanks.
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.
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 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've test simple scala program and compiled it with scalac
object Test {
def main(args: Array[String]) {
println("hongseok yoon")
}
}
If I run that 'java -cp .;scala-library.jar Test' and it works okay.
YES, scala-library.jar filee is in same directory.
This is my manifest file manifest.txt
Class-Path: scala-library.jar
Main-Class: Test
and I make jar with 'jar cfm Test.jar manifest.txt *.class'
If I unzip generated Test.jar file and open manifest file, it does not contain Main-Class field. So, It cannot be run with 'java -jar Test.jar'
I can edit it manually and Test.jar runs well.
Why does jar miss Main-Class field? and how to fix this?
(If I switch order of fields, only first field is shown)
ONE more empty line is needed at the end of manifest file.
What a stupid restriction! :(