How to Make an Executable Jar File

How to make an executable JAR file?

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.

Making an executable jar file

Making .jar file from .java file

  1. First make sure that your file name and your class name are same. "client.java" where class is client
  2. Now create a manifest.txt file. and write this

    Main-Class: Your_Main_Class_name

    In your case it is

    Main-Class: client

    Now save the file

  3. make sure that your both file (.java and manifest.txt) on the same directory

  4. Open command prompt
  5. Go to that folder where you saved .java and manifest.txt file. Using command

    cd your_path

  6. Now compile your .java file using command

    javac client.java

  7. Now you have client.class file on the same folder

  8. Now to make .jar file use this command

    jar cfn client.jar manifest.txt client.class

  9. Now your client.jar file is created on the same directory

  10. to run .jar file use this command

    java -jar client.jar

Create executable Jar file under Eclipse

To create a new runnable JAR file in the workbench:

  1. From the menu bar's File menu, select Export.
  2. Expand the Java node and select Runnable JAR file. Click Next.
  3. In the Runnable JAR File
    Specification page, select a 'Java Application' launch configuration
    to use to create a runnable JAR.
  4. In the Export destination field, either type or click Browse to
    select a location for the JAR file.
  5. Select an appropriate library handling strategy.
  6. Optionally, you can also create an ANT script to quickly regenerate
    a previously created runnable JAR file.

how to make an executable jar which includes a java and a jar file

Step 1. create manifest.txt file as
Main-Class: Main-Class-Name

Step2. compile all classes.

Step 3. run JDK's jar.exe utility like:

jar cvfm JarName.jar manifest.txt .class
cvfm is create a jar; show verbose output; specify the output jar file name; specify the manifest file name. (
.class means all class files in the current directory.)

Step 4. specify jar dependency on classpath.

For detail refer http://www.skylit.com/javamethods/faqs/createjar.html

Build to executable jar

Take a look at jpackage, a new standard tool in Java 14 for packaging self-contained Java applications. It looks like this is exactly what you need.

Command to build (roughly):

jpackage --name <name here> --input lib --main-jar NameJar.jar --main-class <main class here> --type app-image


Related Topics



Leave a reply



Submit