How to create executable jar in Java?
Following are the step for creating executable jar file in java:
Step: Software environments:
JDK
Eclipse
Step: crate java project in Eclipse
Open the Eclipse
File > New > Java Project
Enter the:
Project name: Jar-Creation-In-Java-TutorialByExample
Select the JRE
JavaSE-1.7
Click on the
Next
Finish.
Step: Add ExecutableJarCreationInJava.java in Jar-Creation-In-Java-TutorialByExample
Right click on the src folder under Jar-Creation-In-Java-TutorialByExample
Click on the
New
Class
Enter the package name: com.tutorialbyexample
Enter the Name: ExecutableJarCreationInJava
Click on the Finish.
Step: ExecutableJarCreationInJava.java
package com.tutorialbyexample;
/*
* Main for for Executable Jar Creation In Java process
*/
public class ExecutableJarCreationInJava {
public void getDetails(Object object) {
printLn(object);
}
/*
* private method for print info on console
*/
private void printLn(Object object) {
System.out.println(object);
}
}
Step: Add MainApp.java in Jar-Creation-In-Java-TutorialByExample
Right click on the src folder under Jar-Creation-In-Java-TutorialByExample
Click on the
New
Class
Enter the package name: com.tutorialbyexample
Enter the Name: MainApp
Click on the Finish.
Step: MainApp.java
package com.tutorialbyexample;
/*
* Main for for Executable Jar Creation In Java process
*/
public class MainApp {
/*
* main method for Main App
*/
public static void main(String[] args) {
ExecutableJarCreationInJava objJarInJava =
new ExecutableJarCreationInJava();
objJarInJava
.getDetails("Jar file creation done in Java by Eclipse!!!");
}
}
Step: Run it in Eclipse for simple output
Right click on the MainApp.java
Run As
Java Application
Console: Jar file creation done in Java by Eclipse!!!
Step: Create the executable jar file mainapp.jar in Eclipse
Right click on Jar-Creation-In-Java-TutorialByExample
Click on the Export
Type the Jar and Click on the Runnable JAR file from wizard
Click on the Next
Select project name under Lunch Configuration: Jar-Creation-In-Java-TutorialByExample
Export destination: C:\mainapp.jar
Let it be rest default setting
Click Finish.
mainapp.jar will create in C:\mainapp.jar.
Step: Directory structure for mainapp.jar
If you have 7-zip, just right click on the mainapp.jar and extract it.
mainaap
--com
-- tutorialbyexample
-- ExecutableJarCreationInJava.class
-- MainApp.class
--META-INF
-- MANIFEST.MF
Step: MANIFEST.MF file details
Manifest-Version: 1.0
Class-Path: .
Main-Class: com.tutorialbyexample.MainApp
Note: With help of this only jar file converted into executable.
Step: Run the executable jar mainapp.jar form command line.
Open the command prompt
Start > Type cmd and press Enter
Command prompt window will appear
Change the dir to c:\ as our mainapp.jar file are located under this dir.
Type the command java and enter
c:\>java
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)
where options include:
-d32 use a 32-bit data model if available
-d64 use a 64-bit data model if available
-client to select the "client" VM
-server to select the "server" VM
-hotspot is a synonym for the "client" VM [deprecated]
The default VM is client.
-cp
-classpath
A ; separated list of directories, JAR archives,
and ZIP archives to search for class files.
-D=
set a system property
-verbose:[class|gc|jni]
enable verbose output
-version print product version and exit
-version:
require the specified version to run
-showversion print product version and continue
-jre-restrict-search | -no-jre-restrict-search
include/exclude user private JREs in the version search
-? -help print this help message
-X print help on non-standard options
-ea[:...|: ]
-enableassertions[:...|: ]
enable assertions with specified granularity
-da[:...|: ]
-disableassertions[:...|: ]
disable assertions with specified granularity
-esa | -enablesystemassertions
enable system assertions
-dsa | -disablesystemassertions
disable system assertions
-agentlib:[= ]
load native agent library, e.g. -agentlib:hprof
see also, -agentlib:jdwp=help and -agentlib:hprof=help
-agentpath:[= ]
load native agent library by full pathname
-javaagent:[= ]
load Java programming language agent, see java.lang.instrument
-splash:
show splash screen with specified image
See http://www.oracle.com/technetwork/java/javase/documentation/index.html for more details.
Indicate that java home has been set if not then set the java home.
Output:
c:\>java -jar mainapp.jar MainApp
Jar file creation done in Java by Eclipse!!!
Thanks for reading, please like and share!!!
Reference:
Java 1.7 by Oracle
Eclipse
Video:
Video:
No comments:
Post a Comment