How to make an executable JAR file?
Asked Answered
S

6

105

I have a program which consists of two simple Java Swing files.

How do I make an executable JAR file for my program?

Shamrock answered 10/3, 2011 at 10:10 Comment(1)
Related: superuser.com/questions/912955/…Canova
K
105

A jar file is simply a file containing a collection of java files. To make a jar file executable, you need to specify where the main Class is in the jar file. Example code would be as follows.

public class JarExample {

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                // your logic here
            }
        });
    }
}

Compile your classes. To make a jar, you also need to create a Manifest File (MANIFEST.MF). For example,

Manifest-Version: 1.0
Main-Class: JarExample

Place the compiled output class files (JarExample.class,JarExample$1.class) and the manifest file in the same folder. In the command prompt, go to the folder where your files placed, and create the jar using jar command. For example (if you name your manifest file as jexample.mf)

jar cfm jarexample.jar jexample.mf *.class

It will create executable jarexample.jar.

Knucklebone answered 10/3, 2011 at 10:25 Comment(4)
Just my two cents: You do not necessarily have to create a manifest file. For the jar utility, if you specify the flag e instead of m, then you just need to specify the entry point (that is, the main class) of the application, rather than a manifest file. Example: jar cvfe jarexample.jar com.example.jar.JarExample *.classRaffaello
If you create the MANIFEST.MF file, don't forget to finish the last line with a line break. On Windows just add an empty line at the end. Otherwise the last attribute will not make it in the jar file. In this special case the Main-Class attribute will be missing.Krall
Does the jar command require a separate path variable? I am able to use the java and javac commands without issue, but the jar command is giving me an 'jar' is not recognized as an internal or external command error message.Recognize
@Recognize No it does not. Use command which on linux or get-command in powershell to see where those java and javac are actually located on your machine and if there is a jar program there as well.Labarbera
Q
40

In Eclipse you can do it simply as follows :

Right click on your Java Project and select Export.

Select Java -> Runnable JAR file -> Next.

Select the Launch Configuration and choose project file as your Main class

Select the Destination folder where you would like to save it and click Finish.

Quilt answered 23/3, 2013 at 14:2 Comment(2)
Launch Configuration: <MainClasssName> - <ProjectName>Gamecock
Eclipse creates a bunch of classes in a new package org\eclipse\jdt\internal\jarinjarloader (and the JAR is not compatible with OpenJDK). Why is eclipse complicating this?Vizza
C
31

Here it is in one line:

jar cvfe myjar.jar package.MainClass *.class

where MainClass is the class with your main method, and package is MainClass's package.

Note you have to compile your .java files to .class files before doing this.

c  create new archive
v  generate verbose output on standard output
f  specify archive file name
e  specify application entry point for stand-alone application bundled into an executable jar file

This answer inspired by Powerslave's comment on another answer.

Cody answered 9/11, 2017 at 19:16 Comment(2)
I am getting an error, Error: Could not find or load main class mtx.testClass Caused by: java.lang.ClassNotFoundException: mtx.testClassGraziano
Is testClass defined in one of your .class files? Make sure you have compiled your source (.java) files before running this command. Also, usually your class should be UpperCamelCase, you might try renaming it to TestClass instead.Cody
S
8

If you use maven, add the following to your pom.xml file:

<plugin>
    <!-- Build an executable JAR -->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <archive>
            <manifest>
                <mainClass>com.path.to.YourMainClass</mainClass>
            </manifest>
        </archive>
    </configuration>
</plugin>

Then you can run mvn package. The jar file will be located under in the target directory.

Stylistic answered 29/11, 2019 at 19:15 Comment(0)
S
5

It's too late to answer for this question. But if someone is searching for this answer now I've made it to run with no errors.

First of all make sure to download and add maven to path. [ mvn --version ] will give you version specifications of it if you have added to the path correctly.

Now , add following code to the maven project [ pom.xml ] , in the following code replace with your own main file entry point for eg [ com.example.test.Test ].

      <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                    <mainClass>
            your_package_to_class_that_contains_main_file .MainFileName</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

Now go to the command line [CMD] in your project and type mvn package and it will generate a jar file as something like ProjectName-0.0.1-SNAPSHOT.jar under target directory.

Now navigate to the target directory by cd target.

Finally type java -jar jar-file-name.jar and yes this should work successfully if you don't have any errors in your program.

Samarskite answered 18/7, 2020 at 8:52 Comment(1)
This is a Maven specific solution and obviously doesn't work for someone not using Maven as their build system.Cline
E
0

Creating a jar file with out any dependent jars will work as per the below steps.

1.Compile Test.Java file javac Test.java --- write your code here

2.Create a manifest file test.mf Main-Class: Test

  1. Create the executable Jar file jar -cvmf test.mf test.jar Test.class

4.Run the Jar file java -jar test.jar

Evangelistic answered 18/12, 2023 at 22:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.