0

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
Pravin Satav
  • 702
  • 5
  • 17
  • 36
  • the manifest file should be under `META-INF/MANIFEST.MF`, can you check the generated jar to see if this is the case? – rjeeb Aug 18 '19 at 08:03
  • Try `java -jar MyJar.jar`. –  Aug 18 '19 at 08:08
  • @RJB: can see MANIFEST.MF file created, I have edited question and added -tf output.. – Pravin Satav Aug 18 '19 at 08:24
  • @LutzHorn: java -jar is also throwing an error, I have edited question and pasted command and its output – Pravin Satav Aug 18 '19 at 08:24
  • "no main manifest attribute" means there is no `Main-Class` in file `META-INF/MANIFEST.MF` - it is not the same as `Manifest.txt` (wrong location, wrong extension). Add either `--main-class=mypack.B` or `--manifest=Manifest.txt` when creating the jar. [Documentation](https://docs.oracle.com/en/java/javase/12/tools/jar.html#GUID-51C11B76-D9F6-4BC2-A805-3C847E857867) – user85421 Aug 18 '19 at 08:44

2 Answers2

1

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

Dici
  • 25,226
  • 7
  • 41
  • 82
1

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
Andreas
  • 154,647
  • 11
  • 152
  • 247