Reputation: 702
I m trying to create a jar and execute it for the package structured java program. I m able to create jar but I m executing its throwing error.
Folder Structure : Java_Example > pack > A.java
package pack;
public class A{
public void msg(){System.out.println("Hello");}
}
Folder Structure : Java_Example > mypack > B.java
package mypack;
import pack.*;
class B{
public static void main(String args[]){
A obj = new A();
obj.msg();
}
}
Folder Structure : Java_Example > bin > Manifest.txt
Mainfest-Version: 1.1
Main-Class: mypack.B
Manifest file is present with line feeder
Executing from Command Line
E:\Java_Example\bin>java -cp E:\Java_Example mypack.B
Hello
Creating Jar
E:\Java_Example\bin>jar cvf MyJar.jar Manifest.txt E:\Java_Example\pack\A.clas
E:\Java_Example\mypack\B.class
added manifest
adding: Manifest.txt(in = 60) (out= 61)(deflated -1%)
adding: Java_Example/pack/A.class(in = 376) (out= 268)(deflated 28%)
adding: Java_Example/mypack/B.class(in = 307) (out= 237)(deflated 22%)
Executing Jar
E:\Java_Example\bin>jar -jar MyJar.jar
Illegal option: j
Usage: jar {ctxui}[vfmn0PMe] [jar-file] [manifest-file] [entry-point] [-C dir] f
iles ...
java -jar command execution
E:\Java_Example\bin>java -jar MyJar.jar
no main manifest attribute, in MyJar.jar
Trying execute Jar by giving main class
E:\Java_Example\bin>jar -cp MyJar.jar mypack.B
Illegal option: p
Usage: jar {ctxui}[vfmn0PMe] [jar-file] [manifest-file] [entry-point] [-C dir] f
iles ...
jar -tf output
E:\Java_Example\bin>jar -tf MyJar.jar
META-INF/
META-INF/MANIFEST.MF
Manifest.txt
Java_Example/pack/A.class
Java_Example/mypack/B.class
Upvotes: 0
Views: 919
Reputation: 159155
You're building the jar file wrong. Do it from the base folder, and add the m
option to specify that you're listing the manifest file right after the jar file name (because f
is before m
).
E:\Java_Example>jar cvfm MyJar.jar bin\Manifest.txt pack\A.class mypack\B.class
added manifest
adding: pack/A.class(in = 376) (out= 268)(deflated 28%)
adding: mypack/B.class(in = 307) (out= 237)(deflated 22%)
As you can see, the class files are now correctly qualified, and it didn't say adding: Manifest.txt
because that file is now the manifest
.
Now you run it like this:
java -jar MyJar.jar
If you want the jar file inside the bin folder, you'd of course just qualify that:
E:\Java_Example>jar cvfm bin\MyJar.jar bin\Manifest.txt pack\A.class mypack\B.class
E:\Java_Example>java -jar bin\MyJar.jar
Upvotes: 1
Reputation: 25980
You can't run a jar using the jar
command, use java
instead. For your edited question, the manifest is not in the right place. See the output of your jar extraction:
E:\Java_Example\bin>jar -tf MyJar.jar
META-INF/
META-INF/MANIFEST.MF
Manifest.txt
Java_Example/pack/A.class
Java_Example/mypack/B.class
You shouldn't have Manifest.txt
, the manifest is MANIFEST.MF
Upvotes: 1