Add a jar file to another jar file - java

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

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.

java.lang.NoClassDefFoundError after building jar file in Intellij

The structure of my first app is simple:
libs
opencsv-3.8.jar
yamlbeans-1.0.jar
out
artifacts
...
production
...
src
META-INF
MANIFEST.MF
pl.krzysiu
App.java
CsvReplacer.java
Everything is fine during the compile and running the program. After building artifact jar file in the default out\artifacts directory, I get
java.lang.NoClassDefFoundError: net/sourceforge/yamlbeans/YamlException
when I try to run it by java -jar CsvReplacer.jar command
The libraries are included inside the jar file (they are there after unpacking it) - they are added to Libraries section in Project Structure (separately - one file per one lib), the whole libs dir is included in the Dependencies tab of Modules section (with export checkbox checked) and the libs dir is added in Output Layout of Artifacts section similarily.
The manifest file contains:
Manifest-Version: 1.0
Class-Path: libs\yamlbeans-1.0.jar libs\opencsv-3.8.jar
Main-Class: pl.krzysiu.App
Why the libs aren't visible for the App? If I copy this dir manually to the CsvReplacer.jar file's location - everything works fine.
The structure inside CsvReplacer.jar file looks like:
libs
opencsv-3.8.jar
yamlbeans-1.0.jar
META-INF
MANIFEST.MF
pl
krzysiu
App.java
CsvReplacer.java
IDE: Intellij IDEA 2016.3
The standard Java classloaders cannot find a JAR file embedded inside another JAR file.
You have two choices when making an executable JAR with dependencies.
Create a so-called uberJAR file by merging the contents of the dependent JARs into your main JAR.
References:
IntelliJ IDEA export Runnable program as Uber Jar
https://blog.jetbrains.com/idea/2010/08/quickly-create-jar-artifact/
Give your JAR a "Class-Path" manifest attribute to tell it where the (external!) dependent JARs are located.
You can't give a -cp and a -jar option together. But another alternative would be to get rid of the -jar option and use a -cp argument to specify the classpath.
Alternatively, you could implement a custom classloader that can load from a jar inside a jar, or use something like one-jar or Spring Boot.

Creating jar which includes classpath

I am trying to send my jar file to other people without the need to send multiple jar files to a person for them to run my application. Is there a way to include a secondary jar file inside of my jar and have it as my Class-path instead of it being in my working directory?
This is my manifest for when i create the Jar.
Manifest-Version: 1.0
Main-Class: MultipleLinearRegression
Class-Path: Jama.jar
This is how I am creating my jar file
jar cvfm MyApplication.jar manifest.txt Jama.jar *.class
With Maven and the Shade plugin you can create an Uber JAR, containing all dependend JARs in one JAR file. There is also a Shade Plugin for Gradle. (If you don't use Maven or Gradle, you should really consider to do so.)
There is an interesting article here: Creating Executable Uber Jar’s and Native Applications with Java 8 and Maven
There are other similar tools like One-JAR.
for creating a jar file, we can follow these steps,
1) Class-Path: <lib1.jar> <lib2.jar> <path/lib3.jar>
2) Main-Class: <classname>
3) Place a new line at the end of this file

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