0

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.

Brandon
  • 16,382
  • 12
  • 55
  • 88
user3062233
  • 179
  • 1
  • 2
  • 7
  • 1
    look at: http://stackoverflow.com/questions/9689793/cant-execute-jar-file-no-main-manifest-attribute – PrR3 Mar 01 '14 at 14:42
  • This question is answered at least a thousand times on the web already. Please do some research before asking... – l4mpi Mar 01 '14 at 15:01
  • @l4mpi When you see questions that demostrate no research effort, you should down vote them. – Raedwald Mar 01 '14 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 Mar 01 '14 at 15:24
  • After fixing this problem, as described, you may use NSIS to create exe for your jar. – baris.aydinoz Mar 01 '14 at 16:28

2 Answers2

2

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>
Community
  • 1
  • 1
helderdarocha
  • 23,209
  • 4
  • 50
  • 65
0

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.

tmarwen
  • 15,750
  • 5
  • 43
  • 62