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
- First make sure that your file name and your class name are same. "client.java" where class is client
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
make sure that your both file (.java and manifest.txt) on the same directory
- Open command prompt
- Go to that folder where you saved .java and manifest.txt file. Using command
cd your_path
Now compile your .java file using command
javac client.java
Now you have client.class file on the same folder
Now to make .jar file use this command
jar cfn client.jar manifest.txt client.class
Now your client.jar file is created on the same directory
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:
- From the menu bar's File menu, select Export.
- Expand the Java node and select Runnable JAR file. Click Next.
- In the Runnable JAR File
Specification page, select a 'Java Application' launch configuration
to use to create a runnable JAR. - In the Export destination field, either type or click Browse to
select a location for the JAR file. - Select an appropriate library handling strategy.
- 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
What Is "String Args[]"? Parameter in Main Method Java
How to "Decompile" Java Class Files
How to Handle Cors Using Jax-Rs With Jersey
What's Causing My Java.Net.Socketexception: Connection Reset
Finding the Max/Min Value in an Array of Primitives Using Java
Do Subclasses Inherit Private Fields
Unicode Equivalents For \W and \B in Java Regular Expressions
Syntax For Creating a Two-Dimensional Array in Java
Difference Between a Static and a Non-Static Initialization Code Block
What Does Servletcontext.Getrealpath("/") Mean and When Should I Use It
Difference Between Applicationcontext.Xml and Spring-Servlet.Xml in Spring Framework
How to Get the Separate Digits of an Int Number
How Does a Preparedstatement Avoid or Prevent SQL Injection
How Are Ssl Certificate Server Names Resolved/How to Add Alternative Names Using Keytool