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

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.

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.

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 run my a Java program as an executable .jar file?

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.

How to attach libraries to JAR file?

It works fine when compiling project, but after exporting it to a runnable jar and launching, it can't find external files and throws an error. What should I do?
Add external libraries to the manifest.mf:
Class-Path: . MyApp_lib/extlib.jar MyApp_lib/extlib2.jar ...
You could attempt building a fat jar that includes all the jars. It contains a custom class loader to load the jars referenced externally by your project.
Try using http://fjep.sourceforge.net/ plugin to build a fat jar.
You can export a java project containing jars using the File -> Export -> Other -> One Jar Exporter.
The jar thus exported works fine.
You have to keep all required jars in the classpath to run your jar. Run your jar like :
java -cp extlib/* -jar yourjar.jar OR java -cp lib1.jar:lib2.jar:.. -jar yourjar.jar
Make sure that while building the jar, you include all the used libraries(include everything from class path). This issue will happen when you refer a external jar.
You can include a classpath variable in the jar's manifest file.
JAR file classpath

Categories

Resources