user3062233
user3062233

Reputation: 179

Running java program in windows

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.

Upvotes: 0

Views: 141

Answers (2)

tmarwen
tmarwen

Reputation: 16354

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.

Upvotes: 0

helderdarocha
helderdarocha

Reputation: 23637

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>

Upvotes: 2

Related Questions