I am in the final step of my application release and so creating jar, called App.jar. I have other jars on which App.jar is dependent, so I rather included it within App.jar. When trying to run, it works if App.jar is put into the folder of the main project. However, when put outside it (for example on the dekstop), it does not work completely, meaning that some GUi components are not displaying. My question is:
Besides other jars on which my App.jar is dependent, can it be dependent on some other files or under some other conditions?
EDIT:
I am working in Eclipse, to build jar I just exported it and chose the option "packaged required libraries into generated JAR", which put all the jars I need to run into App.jar. My MANIFEST.MF then looks like this:
Manifest-Version: 1.0
Rsrc-Class-Path: ./ JTattoo.jar plugin.jar docking-frames-common-1.1.1
-SNAPSHOT.jar jl1.0.jar swt-3.7M5-win32-win32-x86.jar worldwind.jar a
ccess-bridge.jar sunpkcs11.jar looks-2.0.1.jar tools.jar OfficeLnFs_2
.7.jar jce.jar jna_WindowUtils.jar dom4j-2.0.0-ALPHA-2.jar junit-4.5.
jar rt.jar mysql-connector-java-5.1.21-bin.jar sunmscapi.jar glasslib
.jar resources.jar jxl.jar dnsns.jar sunec.jar jogl.jar mp3spi1.9.4.j
ar docking-frames-core-1.1.1-SNAPSHOT.jar DJNativeSwing.jar tritonus_
share.jar dom4j-2.0.0-ALPHA-2-sources.jar zipfs.jar MozillaInterfaces
-1.8.1.3.jar jfr.jar DJNativeSwing-SWT.jar gluegen-rt.jar jaxen-1.1.1
.jar gdal.jar localedata.jar jna-3.2.4.jar charsets.jar sunjce_provid
er.jar jsse.jar jaccess.jar
Class-Path: .
Rsrc-Main-Class: org.cnstar.wiki.app.GreatPlaces
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
SplashScreen-Image: images/splash_screen.png
You should set the classpath properly to include all the jars required.
Yes there can be more jars dependency due to transitive dependency. Your App.jar may be dependent on a jar, which may be dependent on another jar. Using Maven you can be away from all such worries as it automatically downloads all the dependencies.
Related
I am having an executable JAR. Ofcouser I have JDK installed at my end I am giving following command to run my exe JAR from command prompt.
1 Using JRE :-
C:\Users\userName\Desktop\Utility\latest>"C:\Program Files\Java\jre1.8.0_161\bin\java.exe" -jar Utility.jar
2 Using JDK
C:\Users\userName\Desktop\Utility\latest>"C:\Program Files\Java\jdk1.8.0_161\bin\javaw.exe" -jar Utility.jar
Both are working on my desktop but if I tries #1 to run the executable JAR on different machine which has only JRE Version (1.8 onwards) it is not getting opened up.
I tried following links but some links are sayin to download few installers but all I do not want to get that. Is there any way. Or issue with my executable JAR ?
How can I make my executable JAR not need JDK to run
Run a JAR file using a specific JRE
Manifest-Version: 1.0
Rsrc-Class-Path: ./ commons-collections4-4.3.jar poi-3.17.jar poi-ooxm
l-3.17.jar xmlbeans-3.0.1.jar curvesapi-1.06.jar poi-ooxml-schemas-3.
17.jar poi-examples-3.17.jar poi-excelant-3.17.jar poi-scratchpad-3.1
7.jar commons-codec-1.10.jar commons-collections4-4.1.jar commons-log
ging-1.2.jar curvesapi-1.04.jar junit-4.12.jar log4j-1.2.17.jar xmlbe
ans-2.6.0.jar ooxml-schemas-1.3.jar
Class-Path: ./ commons-collections4-4.3.jar poi-3.17.jar poi-ooxml-3.17.jar
xmlbeans-3.0.1.jar curvesapi-1.06.jar poi-ooxml-schemas-3.17.jar poi-examples-3.17.
jar poi-excelant-3.17.jar poi-scratchpad-3.17.jar commons-codec-1.10.jar
commons-collections4-4.1.jar commons-logging-1.2.jar curvesapi-1.04.jar
junit-4.12.jar log4j-1.2.17.jar xmlbeans-2.6.0.jar ooxml-schemas-1.3.jar
Rsrc-Main-Class: DataProcessor.DataProcessor.App
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoade
It sounds like the issue your are having is more than not having the JDK or knowing where the JRE on the target system is located, it's that you also didn't include the dependencies that your code has.
The jar file you have includes details in the manifest file that tells the JVM the classpath and the main class to load. If you look at the Rsrc-Class-Path, it is add the jars from the current directory. This is fine if you are sending the entire folder structure that includes all the jars in the expected location. But it doesn't work with just the jar.
In order to make a single jar that would run without any additional jars, you need to repackage the jars. There are two common ways to do this
UberJar - where the class of your project is combined with the classes extracted form all of your dependencies into a single jar
JarInJar - where your jar and all the dependecy jars are put into a jar and a custom classloader is used to load the classes from the jars inside the jar.
I'm not sure what build tool you're using, but for Maven the Shade Plugin will create an UberJar.
I personally recommend using the JarInJar option for this reason. The Spring Boot Maven Plugin is what I believe to the easiest
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.
My java project uses javax.mail.jar. So, in netbeans, I created a new folder Libraries, copied the jar file there and added this in project properties. I added this folder to git and this runs fine on netbeans. But when I build the project with ant on another machine and run the project jar with
java -jar myproject.jar
I get the error
Exception in thread "main" java.lang.NoClassDefFoundError:
javax/mail/MessagingException
I can see that myproject.jar contains the dependent library
$jar tf TakServer.jar
Libraries/
Libraries/javax.mail.jar
...
I'm not sure what is going wrong. Any idea how to get it to run?
Solution: As #Gimby pointed out, the jar files from Netbeans and ant build are different. Netbeans includes a lib/ folder containing all dependencies in dist/ folder and its MANIFEST.ML adds lib/ to classpath. The Libraries folder inside the main jar is not used at all.
The ClassPath in the manifest points to the filesystem outside the jar.
It it not possible to use jars inside jars
https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
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.
Is it possible to create a JAR file that requires external dependencies without including those dependencies in the JAR file?
My google-fu has failed to give me an answer; everything that I have found shows how to include them in the JAR file, but not what to put in the manifest file to say "I haven't got them, look in the user's classpath". I would assume that the dependencies are properly installed and configured on the user's classpath.
In my case, my dependencies are Apache Commons CLI and Math.
Edit:
Inside my JAR file, I have Main.class.
My manifest file looks like:
Manifest-Version: 1.0
Created-By: 1.6.0 (Sun Microsystems Inc.)
Main-Class: Main
My CLASSPATH looks like
.;C:\Program Files\Java\jre1.6.0_06\lib\ext\QTJava.zip;C:\java_lib\commons-cli-1.2.jar;C:\java_lib\commons-math-2.0\commons-math-2.0.jar
If I include the dependencies in the JAR in /lib and add the line Class-Path: lib/commons-math-2.0.jar lib/commons-cli-1.2.jar to the manifest, then it does work.
I've tried adding Class-Path: commons-math-2.0.jar commons-cli-1.2.jar to the manifest without including the files in the JAR just to see if that would work, but it didn't.
Use the Class-Path entry in the META-INF/MANIFEST.MF to tell where to look for dependencies relatively to your JAR. For example:
Class-Path: servlet.jar ../foo/bar.jar acme/beans.jar
It certainly is possible. One way to think about it is that every time you create a jar, you are depending on the classes in the jre, and it is not necessary to include them in your jar. The jre will automatically look for them in the classpath. If they are not found you will see a NoClassDefFoundError.