Adding External jar's in Eclipse - java

I have created a program to connect to MySQL. I add Connector/j using eclipse add external jar option. Program works fine in eclipse. But when I created the executable jar using eclipse and when i run it, it always give ClassNotFoundException. Please tell me how to add external jars to my jar. Or is there any other error? Please can anyone help me.

The simplest solution is to export your project as a 'Runnable Jar file' (Right-click on project->Export...->Runnable Jar file) that will place all dependencies in one jar file.
Otherwise you will need to include a classpath to the additional jars either in the manifest.mf file or on the command line with the -cp option.
java -cp .;myjar.jar;mysql.jar my.package.classname

You need to create a jar that includes the files from all the dependent jars. The classloader won't be able to find the classes if you simply include the jar files themselves inside the executable jar. There is an eclipse plugin called FatJar that does this.
http://fjep.sourceforge.net/

You can simply add the class-path element to your jar MANIFEST and list your external jar inside the MANIFEST
like this:
Manifest-version:1.0
Class-Path: class0.jar
class1.jar
class2.jar
class3.jar
class4.jar
...
One jar per line.

Related

Java export jar including libraries

I have been looking around for some time now, but didn't a find way how to export a JAR (not runnable jar) that contains in it's build path the referenced libraries.
Using Eclipse, I have included the lib folder which contains the jars of the referenced libraries in the export process.
Importing that JAR to another project and calling some method results in a ClassNotFoundException.
Looking at the MANIFEST, I didn't see any reference to those jars in the classpath, though the jars are indeed included in the jar.
So my questions are:
1. Is there any way to accomplish the packaging of the non-executable JAR so it will include libraries?
2. Is there any best practice for building and deploying a jar that include other jars libraries?
I tried it too but it doesn't work for me. I added the final .jar file but it doesn't work.
So, I did a workaround.
Extract the .jar file that you want as a dependency.
Copy that content and put it all inside your .jar file.
Add your .jar file as dependency inside an eclipse project.
Run it and see if everything is ok.

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