I have a jar that uses a manifest for some of its dependencies which are packed up into a jar using maven.
Is it possible to run the jar with both the internal packaged up jars, which are described in the manifest, and jars that I pass using -cp at application launch?
Or is it one or the other?
Related
I have created a JavaFX application using NetBeans IDE and below is my folder structure.
I want to a build a single jar file including all dependencies for this jar to work properly.
This jar requires testplanner and batch folder from project root directory and files inside dist folder to work properly.
How can I package all this to a single jar file?
Theoretically JAR files cannot contain dependencies within, as java does not support it out of the box. WAR and EAR archives can. What You want to do is not standard, but is named fat jar. Fat jars are used i.e. by spring-boot maven plugin, but you could try this:
https://dzone.com/articles/how-build-fat-jar-using
And some more explanation:
NetBeans - deploying all in one jar
Use tecreations Deploy. Put all your sources into a path declared as Tecreations.getProjectPath(), run BuildAll to create your corresponding classes, put your jars in projectpath/jars and select the appropriate settings, whether to include jars, sources or classes. Select your main class and click Deploy. Unsigned and signed output jars are produced in user/Documents.
Download: https://tecreations.ca/java/downloads/release.
Let's say we have a simple Java project compiles into a simple.jar with its POM depends on log4j.jar. When I open the simple-1.0.jar, inside there is no log4j.jar. And then we upload this simple-1.0.jar onto Nexus.
Now when deploy this simple-1.0.jar to the target server, how do we deploy the log4j.jar?
Thanks
Jirong
You can either package the dependencies with your application (i.e. create an uber-jar, WAR, or EAR file) or you can deploy the dependent jars to a location on the server and then set that location as the classpath when you run the jar.
Packaging an application as an Uber-JAR
One way of creating an uber-jar with Maven would be to use the maven-shade-plugin
Deploying the dependent jars and setting classpath
In this scenario you would create a "libs" folder somewhere on the target machine and copy all of the dependent jars into this folder.
Then when you launch your application you would set the classpath like so:
java -classpath /{libs directory} -jar simple-1.0.jar
Multiple classpath entries can be specified by separating them with a : like also:
java -classpath /{directory1}:/{directory2} -jar simple-1.0.jar
You can have Maven list the resolved dependencies using the dependency plugin:
mvn dependency:list
I am trying to build my application jar (non executable jar) with all it's dependenices jar files also to unpack.
Simply to explain my issue, in eclipse, to export runnable/executable jar, there is an option
"Extract required libraries into generated Jar"
Please help me the same option for non-runnable jar also.
Thanks
No there isn't.
Just as background: Packaging dependencies within a JAR file (sometimes called a fat jar or app jar) is not a native Java mechanism but is grafted on using custom ClassLoaders or start scripts packaged in the JAR.
If you create a runnable JAR file with Eclipse it uses a wrapper to run your application which can read the packaged libraries. You can see this if you open the exported JAR file and look at it's Manifest:
Manifest-Version: 1.0
Rsrc-Class-Path: ./ [removed]
Class-Path: .
Rsrc-Main-Class: com.example.Test
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
The JarRsrcLoader sits between your applications Main-Class and the Java Runtime and can then load the packaged dependencies before executing your code. In a library there is no such hook that Eclipse can hook into since the loading is done by someone using your library and you can't control the loading process.
There are mechanisms to combine JAR files into a single one by unpacking the dependencies and then repackaging them into your JAR. Look here for example: How to combine two Jar files
Note though that by repackaging a 3rd party JAR you might run into issues with signed classes or conflicting Metadata.
If you want to publish a library and/or manage dependencies properly I recommend looking into build systems like Maven (http://maven.apache.org/) or Gradle (http://www.gradle.org/). I personally much prefer Gradle.
Edit: This project seems promising: https://github.com/puniverse/capsule
Use Capsule. It does everything you want including set JVM configuration right in the manifest.
With capsule you have the option of packing all of your JARs and native libs right into the capsule JAR, or you can specify your Maven dependencies in the Manifest, and let them be downloaded the first time the capsule is launched.
I have a Java project in Eclipse with class MainClass having main method in package :
com.nik.mypackage.
The project also references two external libraries, which I copied in the lib folder in Eclipse and then added to build path using ADD JAR function. The libraries being one.jar and two.jar
This library is in lib folder in eclipse and added to the build path.
I want to create a executable JAR of the application using ant script. So that user can access my application using command:
c:>java -jar MyProject-20111126.jar
I know about the Eclipse plugin which directly exports a java application as runnable JAR. But I want to learn ant and the build process so manually want to create the build.xm.
You have two options from your build.xml. You can either unjar the library jars and then bundle their contents with the code compiled for your application. Or, you can put the library jars on the filesystem and supply a ClassPath entry in the manifest file of the MyProject-2011126.jar file.
If you set the classpath in the manifest remember that the path you supply is relative to the MyProject-2011126.jar.
one alternative:
Instead of having only a jar, you build mutiple jars (your jar + libs) +batch file.
So, your built package can be like this structure:
-/package/bin/app.bat
/package/lib/my.jar
/package/lib/one.jar
/package/lib/two.jar
In app.bat you just have the same as your code
java -jar MyProject-20111126.jar
PS: if you want to start learning built tools, ANT may be a bit tool old. I suggest http://maven.apache.org/
Please try one-jar. It helps to redistribute everything packaged as single jar and comes with ant-task . See Easiest way to merge a release into one JAR file.
I created a JAR file from my java project.
Using Eclipse, I added a JAR as a referenced library in my own project.
However, now when I try to run my program's JAR using java -jar myProgram.jar, I get an exception stating that my referenced jar is not available.
So how can I create a JAR consisting a reference to a different JAR and make it work?
Right, an executable JAR cannot contain its own JAR dependencies.
You have to have the main class and classpath set in the executable JAR manifest, then package all your JAR dependencies along with the executable JAR in a relative directory structure that matches the manifest CLASSPATH. Reading this might help.
You need to use Eclipse's runnable JAR exporter. Since Eclipse 3.5 you've the following options when you rightclick project, choose Export > Runnable JAR file:
Either way, Eclipse should take care that you'll be able to run the JAR the way you want on the exported location.
See jarjar project. It is exactly what you are looking for. http://code.google.com/p/jarjar/