I've developed a Java program in Eclipse and ready to release it. I exported it to Java Runnable JAR file, then tried to run it from command line by 'java -jar myprog.jar'. I'm getting a message "no main manifest attribute in myprog.jar". What should I do to create and run the executable jar file properly? Thanks.
-
1look at: stackoverflow.com/questions/9689793/…PrR3– PrR32014-03-01 14:42:23 +00:00Commented Mar 1, 2014 at 14:42
-
This question is answered at least a thousand times on the web already. Please do some research before asking...l4mpi– l4mpi2014-03-01 15:01:09 +00:00Commented Mar 1, 2014 at 15:01
-
@l4mpi When you see questions that demostrate no research effort, you should down vote them.Raedwald– Raedwald2014-03-01 15:21:15 +00:00Commented Mar 1, 2014 at 15:21
-
@Raedwald I already did that, and left the comment to explain why. But the question was already at +2 when I found it, not sure why anyone would upvote it.l4mpi– l4mpi2014-03-01 15:24:41 +00:00Commented Mar 1, 2014 at 15:24
-
After fixing this problem, as described, you may use NSIS to create exe for your jar.baris.aydinoz– baris.aydinoz2014-03-01 16:28:18 +00:00Commented Mar 1, 2014 at 16:28
2 Answers
You have to include a Main-Class property in the META-INF/Manifest.mf file like this:
Main-Class: full.packagename.ClassName
This will allow you to run the program with java -jar myprog.jar in any platform that supports Java, not only Windows.
You can read more about this in this Oracle Java Tutorial chapter Setting an Application's Entry Point which shows you how to set that programmatically (although you can just edit the file before packaging if you want).
In Eclipse you can set that in 'Run Configurations' (see how to setup "Main Class" in "Run Configurations" in Eclipse)
If your project uses Maven, you can configure that with the Maven Shade Plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>your.packagename.ClassName</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
If you are using Ant, you can use the <manifest> option from the JAR Task
<target name="make-executable-jar" depends="compile">
<jar destfile="myprog.jar">
<fileset dir="build/main/classes"/>
<manifest>
<attribute name="Main-Class" value="your.packagename.ClassName"/>
</manifest>
</jar>
</target>
Comments
You have to add an entry point to your bundled jar application. This information is provided within the jar:META-INF/Manifest.mf file. The main entry description can be done by adding the Main-Class header as follows:
Main-Class: packagename.classname
The packagename.classname should have a method with signature public static void main (String[] args)
This will allow the java process to recognize your main class at runtime and execute it.
Recall that this stays a valid method within Windows box or any platform supporting java.