exported Libraries handling in exported jars java - java

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.

Related

Automate: export runnable jar, by Launch Configuration, Eclipse

I develop Java code using Eclipse. When I want a runnable jar I point and click in the GUI like this:
File->Export->Runnable Jar -> Launch Configuration (select MainTest) --LibraryHandling (extract required libraries into generated JAR)
I want to do this automatically, how can I do this in bash?
Depending on what is in your project and what dependencies you have, this could be pretty complex using the command line tools Java provides. The Java tutorial is pretty good in explaining the usage of the related tools that ship with Java to create jars, but I think most people suggest using a build tool like Maven or Gradle for command line builds.
If you're using the jar tool though, the most basic example for the jar tool with a runnable Main class being specified is:
jar cmf existing-manifest jar-file input-file(s)
where existing-manifest would be your manifest file that specifies your main class.

Exporting a Java project with external dependencies to a jar using Eclipse

Is there an easy way to export a Java project including all of its external dependencies to a standard .jar file?
I have developed an SDK that uses several Apache libraries and I would like to be able to ship this project as a single jar. The answers I have found thus far to this question require the project to be packaged as a Runnable jar, and does not apply to this project which has no main class.
The rest of the answers I have found assume familiarity with Ant or Maven to accomplish this task, and I have no background with either. There are several scripts out there but I don't understand how I would use/modify them, or what I would to run the script and get a result.
Any answer that can provide more background into how exactly a user with no background with Ant or Maven would do these tasks is greatly appreciated.
For more background information I have trying to combine the following libraries into one called JetstreamSDK.jar
commons-codec-1.6.jar
commons-configuration-1.9.jar
commons-io-2.4.jar
commons-lang-2.6.jar
commons-lang3-3.1.jar
commons-logging-1.1.1.jar
httpclient-4.2.5.jar
httpcore-4.2.4.jar
JetstreamSDK-Java.jar
jibx-bind.jar
jibx-extras.jar
jibx-run.jar
xpp3.jar
jar file is like zip compressed file. Usually Eclipse (default) java project will be organized like following directory structure:
some_project
|____ src/ <--- your java source files
|____ bin/ <--- compiled class files
|____ lib/ <--- 3rd party libs e.g. commons*.jar
To package above structure and distribute as a single jar (zip) file- go inside bin/ directory and select everything inside and just right click (assuming you are working on windows) and send to compressed (zipped) folder. Give a suitable name to the generated zip file e.g. your-sdk.jar then right click on lib/ and compress into a another zip together with your-sdk.jar and you are done. This can be accomplished using jar command line tool also- but, you have to read the jar options/help on how to do this.
1.) First compile your project
2.) Right click on your PROJECT -> Export -> Java -> Runnable jar
3.) You will get a Dialog box, there select the class name (in which your main method resides)
4.) Give the export destination
5.) In library handling(radio button): select your desired option(all are self descriptive)

How to create a jar with external libraries included in Eclipse?

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.

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.

Add python module to JAR classpath in Eclipse

I have a Java project that utilizes Jython to interface with a Python module. With my configuration, the program runs fine, however, when I export the project to a JAR file, I get the following error:
Jar export finished with problems. See details for additional information.
Fat Jar Export: Could not find class-path entry for 'C:Projects/this_project/src/com/company/python/'
When browsing through the generated JAR file with an archive manager, the python module is in fact inside of the JAR, but when I check the manifest, only "." is in the classpath. I can overlook this issue by manually dropping the module into the JAR file after creation, but since the main point of this project is automation, I'd rather be able to configure Eclipse to generate properly configured JAR automatically. Any ideas?
*NOTE*I obviously cannot run the program successfully when I do this, but removing the Python source folder from the classpath in "Run Configurations..." makes the error go away.
Figured it out, had to add the source folder with the Python module in it as a class folder in the Build Path project properties. Not sure if this next part is necessary or not, but since the module is not compiled, I added the folder again as "Attached Source" after adding the class folder.
Have a look at the maven-jython-compile-plugin and its demo project at http://mavenjython.sourceforge.net/ . It allows bundling jython with dependencies into a standalone jar.

Categories

Resources