Can't find dependent libraries in jar - java

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

Related

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.

NoClassDefFoundError Jar Error for org/apache/poi/ss/usermodel/Sheet

I have seen this question asked a lot, but I still can't figure out a solution to it. Well a solution that works for me. I have a project that is using Apache POI, and I made sure to include all the external JARs. The project compiles and runs fine in eclipse but when I run the jar with "java -jar Test.jar" I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Sheet
I'm not sure if this is useful information, but I created a lib folder for my project and put the poi library in there. This means that the dependcies are in the JAR file when I create it, I figured I should mention this because I saw a few solutions about just having your external jars right next to your executable jar. I also tried setting my classpath to the directory of the project.
What do I seem to be doing wrong?
The Apache POI JAR file is not on your runtime classpath. Rebuild Test.jar with the following manifest entry in the MANIFEST.MF file
Class-Path: poi-3.9-20121203.jar
When the java -jar [filename].jar command fails, it's almost always because of one of two things:
Your MANIFEST.MF is messed up and doesn't list dependencies properly. Make sure all jar dependencies in your manifest file point to jars, relative to your jar's parent directory.
You are missing .class files, either in your specific jar or in one you depend on. Ensure your jar contains org/apache/poi/ss/usermodel/Sheet.class or that your manifest hierarchy points to a jar that contains that class.
You will need to provide classpath in Jar file's manifest file. See this official doc
An Example
We want to load classes in MyUtils.jar into the class path for use in MyJar.jar. These two JAR files are in the same directory.
We first create a text file named Manifest.txt with the following contents:
Class-Path: MyUtils.jar
First check if your target jar (Test.jar) is a fat jar containing all the required dependencies.
$ jar tf Test.jar
You should see your lib/ folder there containing all the required dependencies (including Apache POI). If that's the case do what others suggested, add Apache POI to your MANIFEST.MF.
About having your jar dependencies in a separate folder, as you suggested, that's also possible. Imagine your dependencies where stored in a lib/ folder outside your Test.jar. You could run your code with this command:
$ java -cp ".:lib/*" org.Test.Main
It simply adds all the .jars within lib/ folder to your classpath. In this case you also need to specify the name of the main class (full name).
I also got this problem and tried to google it..
i have found out that i have to read the error log
I cant save my file to .xls so
in my case after reading the error i found that a jar file is missing
i just added the jar file poi-3.7-20101029 located in the ext folder of my ireport
ex.YourIReportFolder/ireport/modules/ext-poi.x.x-xxxx
hope this helps :)

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

How to build a distributable jar with Ant for a java project having external jar dependencies

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.

Using JAR inside a jar

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/

Categories

Resources