juranga
Posts: 183
Joined: Fri Nov 06, 2015 11:39 am
Location: Basque Country

Cannot execute .jar file [manifest]

Mon May 29, 2017 8:38 am

Hi,

I am facing some problem compiling a .java file. I just jave a onlySyn.java with two classes (onlySyn and InputStreamRunnable), only onlySyn is a public class, and it has a main function.

This are the steps I have followed:

Code: Select all

javac onlySyn.java
This creates two .class files: InputStreamRunnable.class and onlySyn.class

Then, I create a .jar file:

Code: Select all

sudo jar cvf Test.jar InputStreamRunnable.class onlySyn.class 
added manifest
adding: InputStreamRunnable.class(in = 1076) (out= 612)(deflated 43%)
adding: onlySyn.class(in = 5223) (out= 2734)(deflated 47%)
But when I try to execute a jar file I get the next error:
pi@raspberrypi:~/Documents/CPS $ sudo java -jar ./Test.jar 52.25.25.25 5454 1
no main manifest attribute, in ./Test.jar
What am I missing??

ghans
Posts: 7883
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: Cannot execute .jar file [manifest]

Mon May 29, 2017 10:36 am

You are missing a manifest file.

https://en.wikipedia.org/wiki/JAR_%28fi ... 9#Manifest

You can write it by hand , this is a minimal but valid example :

Code: Select all

Manifest-Version: 1.0
Main-Class: com.example.MyClassName
https://stackoverflow.com/questions/127 ... le-in-java

Alternatively , specify the main class manually :
https://stackoverflow.com/questions/202 ... at-runtime

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

knute
Posts: 612
Joined: Thu Oct 23, 2014 12:14 am
Location: Texas
Contact: Website

Re: Cannot execute .jar file [manifest]

Mon May 29, 2017 4:28 pm

juranga wrote:Hi,
But when I try to execute a jar file I get the next error:
pi@raspberrypi:~/Documents/CPS $ sudo java -jar ./Test.jar 52.25.25.25 5454 1
no main manifest attribute, in ./Test.jar
What am I missing??
There needs to be something to tell the java runtime what main class you want to run. There are three ways to make this work.

1) Put your jar file in the class path (this will work with what you have). java -cp YourJar.jar TheClassWithTheMain

2) Jar it with the e option and give it the main class. jar -cvfe YourJar.jar program *.class

3) Create a manifest as ghans suggested and include that in the jar command with the m option. jar -cvfm Your.jar Your.man *.class

User avatar
clicky
Posts: 501
Joined: Thu Oct 25, 2012 7:34 am

Re: Cannot execute .jar file [manifest]

Tue May 30, 2017 7:39 am

If you decided to put some more into coding with Java I wholeheartedly suggest you pick up something like maven and organise project as per it. And adopt IDE of choice to do it in.

I've done something recently and it was similar to:

http://www.mkyong.com/maven/how-to-crea ... ith-maven/

You can, then, build it with:

Code: Select all

mvn clean install
To get executable jar just add section as explained here:

Code: Select all

https://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html
(don't forget to change <mainClass>org.sonatype.haven.HavenCli</mainClass> to include your class instead of "org.sonatype.haven.HavenCli".

If you are doing it from PC (before transferring it to RPi) you can use Eclipse (or IntelliJ's Idea) and import existing maven project. Speaking of Eclipse (I suspect same goes with Idea) you can even export runnable Jar from 'regular' (non-maven) project, too, but I wouldn't recommend that as you wouldn't like your builds being easily reproduced.

juranga
Posts: 183
Joined: Fri Nov 06, 2015 11:39 am
Location: Basque Country

Re: Cannot execute .jar file [manifest]

Tue May 30, 2017 8:41 am

I have finally solved it. I have followed the next commands:

javac OnlySin.java
jar cfe Main.jar OnlySin OnlySin.class InputStreamRunnable.class



Many thanks for all the answers!

Return to “Java”