I have a Project in Eclipse and I want to use a library. When I debug this Project, it works. But when I export it, the JAR is not included, and I get a ClassNotFoundException.
How can I export a Project, and include another (JAR-) File. It isn't a runnable JAR file. I already tried to copy the library in my project, doesn't work.
You need to use FatJar plugin in Eclipse to pack all your libraries into one jar file, as your dependencies are present in a seperate jar file.
Eclipse is able to unpack the JAR and pack it into the generated one. Choose one option at in the export frame (File → Export → Runnable JAR File):
:
Related
I have have a java project in eclipse that uses a bunch of jars.I want to export the class as a runnable jar.When i export the project with jar libraries extracted in the jar it gives me an error when running the jar.
(UncaughtExceptionHandler in thread "main")
However when i exract the libraries as a subfolder besides the runnable jar it works fine.
How can i make the project work the the libraries used extracted in the jar and not in a subfolder?
Thank you
Eclipse has an option to package required library's into the generated JAR. If that is selection that should fix your problem.
Correct option
If I understand you correctly, you're having trouble to export your program as a Jar file including the dependencies libraries.
So, the solution is like this:
When you export your project as a 'Runnable jar' (Right mouse on project -> Export -> Runnable jar) you have the option to package all dependencies into the generated jar. It also has two other ways (see screenshot) to export your libraries, be aware of the licences when deciding which packaging method you will use.
launch configuration dropdown is populated with classes containing a public static void main(String[] args) method. The selected class is started when you run the jar.
Exporting as a runnable jar uses the dependencies on your build path (Right mouse on project -> Build Path -> Configure Build Path...). When you export as a 'regular' (non-runnable) jar you can select any file in your project.
If you have the libraries in your project folder you can include them but external dependencies, for example maven, cannot be included.
Also, there another solution using Gradle, which is much easier and robust, which you can manage all your dependencies so easyly and do so much more.
You can read more here.
I have a library called Snakeyaml.jar, and I want to add it to my eclipse project, so it will be included in my jar, when I export my project. So far, I only see ways to add an "External Jar" which only adds a jar library to the buildpath, and does NOT include it in the program when being exported! How can I do it, and do I need any plugins for that? Please help!
Thanks.
Adding the jar to your build path is for compilation and runtime, but from eclipse only. A common misconception is that jar files can be added into other jar files, which will never work. What you probably want is extract your library jar into your exported jar. To achieve this:
File - Export
Expand Java node and select Runnable JAR File
In the library handling section, select Extract required libraries into generated JAR
Reference:
http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fref-export-runnable-jar.htm
While exporting you can chose the File->Export->Runnable Jar Option . Then use the selection like below screen i.e Extract required libraries into generated jar
Okay, so I understand what you are actually asking! You want the Snakeyaml.jar file inside of your exported jar file, and for your program to use its libraries.
To do so, drag and drop the Snakeyaml.jar file into your src folder in eclipse.
Then, go to build path, and instead of looking for external jars for your buildpath, choose to use the jar file that is already in your src folder in eclipse. Once you do that, you should export it and pick to "Extract required libraries into jar file" or something like that, and everything should work well! You will notice upon opening the exported jar file with a tool like Winrar, that the jar you had in your src folder is not there, but the packages of the jar are actually side by side with yours.
I am done with the project which connects to database (MySQL). Now I want to export the project as jar. But I don't know how to include its external dependencies? Is there any way of doing it in Eclipse or should I use any scripts for that?.
When you export your project as a 'Runnable jar' (Right mouse on project -> Export -> Runnable jar) you have the option to package all dependencies into the generated jar. It also has two other ways (see screenshot) to export your libraries, be aware of the licences when deciding which packaging method you will use.
The 'launch configuration' dropdown is populated with classes containing a main(String[]) method. The selected class is started when you 'run' the jar.
Exporting as a runnable jar uses the dependencies on your build path (Right mouse on project -> Build Path -> Configure Build Path...). When you export as a 'regular' (non-runnable) jar you can select any file in your project(s). If you have the libraries in your project folder you can include them but external dependencies, for example maven, cannot be included (for maven projects, search here).
You could use the Export->Java->Runnable Jar to create a jar that includes its dependencies
Alternatively, you could use the fatjar eclipse plugin as well to bundle jars together
You can right-click on the project, click on export, type 'jar', choose 'Runnable JAR File Export'. There you have the option 'Extract required libraries into generated JAR'.
Personally,
None of the answers above worked for me, I still kept getting NoClassDefFound errors (I am using Maven for dependencies). My solution was to build using "mvn clean install" and use the "[project]-jar-with-dependencies.jar" that that command creates. Similarly in Eclipse you can right click the project -> Run As -> Maven Install and it will place the jars in the target folder.
If you want to export all JAR-files of a Java web-project, open the latest generated WAR-file with a ZIP-tool (e.g. 7-Zip), navigate to the /WEB-INF/lib/ folder. Here you will find all JAR-files you need for this project (as listed in "Referenced Libraries").
While exporting your source into a jar, make sure you select runnable jar option from the options. Then select if you want to package all the dependency jars or just include them directly in the jar file. It depends on the project that you are working on.
You then run the jar directly by java -jar example.jar.
To generate jar file in eclipse right click on the project for which you want to generate, Select Export>Java>Runnable Jar File,
Its create jar which includes all the dependencies from Pom.xml, But please make sure license issue if you are using third-party dependency for your application.
If it is a standalone (Main method) java project then Not any specific path put all the jars inside the project not any specific path then right click on the project - > export - > Runnable jar --> Select the lunch configuration and Library handeling then choose the radio button option "Package required libraries into generated jar" -- > Finish.
Or
If you have a web project then put all the jars in web-inf/lib folder and do the same step.
Before exporting, click on Buildpath and configure java buildpath and add external jars inside the library. Then try to export as a runnable jar.
I've a java project that use some external jar library, when i export my project with eclipse (File-Export-Runnable Jar File), i select the option for "copy external libraries into a subfolder next to the main jar" and eclipse automatically create for me a folder named :
myjarname_lib
I want to export the library in a folder named only lib , is it possible ?
I've tried to rename the folder in lib but don't work, because the executable jar don't recognize the library in that folder.
How can i change the name of the external lib ?
Here is a workaround: When you run the Export wizard, there is a checkbox to create an Ant buildfile. Choose a location for the buildfile inside your project somewhere, then finish the export. Open the buildfile that was created (you might need to hit "F5" to refresh the project). Then just replace all "myprojectname_lib" with "lib". From then on, use "Run As -> Ant Build" for your exports instead of the Eclipse built-in.
The drawbacks are that this buildfile is completely independent of your project and it seems to manage copying each dependency individually. If you change the dependencies of your project, you will have to manually update this file to get the correct exports.
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/