How to run my a Java program as an executable .jar file? - java

I was wondering how can I turn my program (which I am currently running from Eclipse) into an executable file?
This will make it more feasible as people with JRE can just access it by clicking on a single file.
Any help would be appreciated.
Thank you.

Compile your java code
Create a manifest.txt file.
Specifiy the main class in the manifest file like: Main-Class: com.example.Main
Create a jar by adding the manifest.txt and required .class files: jar cvfm myJar.jar manifest.txt *.class
Note: make sure to add .class files, not .java files.

You may create this as a maven project and then simply run mvn clean package and the jar will be created in target directory.

Related

java Jar containing jars => java.lang.NoClassDefFoundError -> How to include .jar files

jar file
when I run java -jar main.jar I get the following error:
The main class of the .jar file second.jar which is one of several libraries inside is not accessible.
I cannot rebuild the project since I just have the main.jar file.
How can I write a run.bat file or a mainifest.mf file that all .jar files that are contained in the main.jar file are accessible.
Here is the mainifest.mf file inside the main.jar:
There are following probable options for you to solve.
Add all the required jar files in the Manifest.MF as given below.
Manifest-Version: 1.0
Main-Class: com.comapany.product.MainClass
Class-Path: a.jar b.jar c.jar
You can run manually using the following command
java -cp a.jar;b.jar;c.jar com.comapany.product.MainClass
It is highly recommended to create a fat jar using Maven Shade plugin in case of Maven or Shadow plugin in case of Gradle.

Build jar depending on a lib like org.json without ide

I build a project without any IDE and want to compile it. I dont want to use Maven or Ant. So i have several classes and a jar file (org.json) "json.jar".
javac -cp json.jar *.java
java -cp .;json.jar App
Now i want to create another jar file for my project but dont know how to include/link the lib json.jar. I created a manifest.mf
Main-Class: App
Class-Path: json.jar
and tried
jar -cmf manifest.mf App.jar *.class json.jar
that creates a jar file but i cant run it. Thank you
according to jar man page :
Be sure that any pre-existing manifest file that you use ends with a
new line. The last line of a manifest file will not be parsed if it
doesn't end with a new line character.

Add a jar file to another jar file

In my project I have this code that tell class loader to load Driver.class like so:
Class.forName(org.gjt.mm.mysql.Driver);
In Eclipse it runs with no problems and I have created the Jar file of the project. But I don't know how to insert the
mysql-connector-java-5.1.7-bin.jar
into a Jar file of my project. The folder structure look like this:
MANIFEST file:
Manifest-Version: 1.0
Main-Class: server.MultiServer
I am assuming that you finally just want to run your code as
java -jar myjar.jar
There are two options.
Keep mysql-connector-java-5.1.7-bin.jar next to your jar in the same folder and add classpath: mysql-connector-java-5.1.7-bin.jar to the manifest.
Copy all the classes in mysql-connector-java-5.1.7-bin.jar to your jar. Do not copy the jar but the classes in the jar. This is called a fat jar or uber jar. You can automate the same using maven shade plugin.
When you invoke your application jar add the -cp or -classpath option and provide the path to the dependent libraries, here the mysql-connector-java-5.1.7-bin.jar.
for example refer the below example
java -jar -classpath C:\myproject\lib\mysql-connector-java-5.1.7-bin.jar myproject.jar

Unable to find the keystore created by a jar file

I have created a program that creates a keystore. When I run this code on IDE, it runs perfectly. However, when I run the jar file - I get no errors but cannot find the keystore created.
Some additional info - The jar file has been created in the cmd prompt using the following commands:
javac -XDignore.symbol.file Keygenerate.java
echo Main-Class: Keygenerate >manifest.txt
jar cvfm myjar.jar manifest.txt *.class
The reason i use -XDignore.symbol.file option is because I am using some deprecated methods in my java file. My file runs perfectly fine on Netbeans but does not clean and build. This is the reason I am using command line to compile and create the jar.
You may need to add the manifest file to the META-INF folder of myjar.jar
Can you show the project and jar folder structures?

How to create a single jar with different main classes?

I have 2 main classes in the project and i want to merge them into a single jar. because i want to use this jar in another project. so can you please suggest a solution for this.
You would go about this the same you would creating a JAR file from 2 classes which both do not have a main method:
jar -cvf your_jar.jar MainClass1.java MainClass2.java
If you want to choose one of the main methods to be the one which will execute by default when the JAR is run, then you can create a manifest file with the following contents:
Main-Class: MainClass1 (Manifest.txt)
Then you can run the jar tool like this:
jar -cvfm jarfile.jar Manifest.txt MainClass1.java MainClass2.java

Categories

Resources