Java: export to an .jar file in eclipse - java

I'm trying to export a program in Eclipse to a jar file.
In my project I have added some pictures and PDF:s. When I'm exporting to jar file, it seems that only the main has been compiled and exported.
My will is to export everything to a jar file if it's possible, because then I want to convert it to an extraditable file, like .exe-file.
But how?

No need for external plugins. In the Export JAR dialog, make sure you select all the necessary resources you want to export. By default, there should be no problem exporting other resource files as well (pictures, configuration files, etc...), see screenshot below.

Go to file->export->JAR file, there you may select "Export generated class files and sources" and make sure that your project is selected, and all folder under there are also! Good luck!

FatJar can help you in this case.
In addition to the"Export as Jar" function which is included to Eclipse the Plug-In bundles all dependent JARs together into one executable jar.
The Plug-In adds the Entry "Build Fat Jar" to the Context-Menu of Java-projects
This is useful if your final exported jar includes other external jars.
If you have Ganymede, the Export Jar dialog is enough to export your resources from your project.
After Ganymede, you have:

One more option is WinRun4J. There is an Eclipse Plugin for WinRun4J that allows you to export your application as a single executable with necessary jars/classes embedded.
(full disclosure: I work on this project)

Related

how to make .jar file in Eclipse MARS

I'm learning Hadoop under VMware now.(win7, 64) Due to some reason, I can't convert a .java file to .jar file under virtual machine, so I tried to create .jar file under Eclipse.
I'm currently using Eclipse MARS(4.5.1). I need to download something about fat jar, but I can't find it online.
My colleague copied his Eclipse software to me. It is already installed, so I copied the whole package. It is Eclipse Kepler. So I am running two Eclipse software on my computer now. will this cause some problem? (I saved projects seperately)
I can do the convert .jar file thing correctly on Eclipse Kepler now. But I still want to use MARS and delete the extra Eclipse.
thank you !
What you're looking for is the "Fat JAR" eclipse plugin.
Fat jar is a jar which contains all the classes used by the project including their referenced dependencies.
The plugin can enable the creation of such a jar.
In your case, however, Eclipse "Mars" & Eclipse "Kepler" include this functionality. The trick is to get to it...
Here's what you do in order to export a Fat JAR:
Right-click on the required Project and select "Export..." --> "Runnable JAR file".
Select the option which extracts libraries and specify the destination JAR file.
Launch configuration is used to execute this JAR with a predefined "Main" class (this is the "Runnable" part...) using "java -jar file.jar".
If you don't plan to - it won't matter.
(Optional) You can also "Save as ANT script" which, well..., generates XML ant script as well.
select project
select Export
Expand the Java node and select JAR file. Click Next
In the JAR File Specification page, select the resources that you want to export in the Select the resources to export field
.
Select the appropriate checkbox to specify whether you want to Export generated class files and resources or Export Java source
or go to http://www.codejava.net/ides/eclipse/how-to-create-jar-file-in-eclipse

How do I change the folder Eclipse exports libraries to?

I have my project, say project Bob. This project needs to be exported.
Project Bob needs libraries, so I need to export those too. I do not, however,
wish to extract the libraries into Bob's jar itself.
When exporting with libraries in a folder next to the jar, the lib folder is called Bob_lib. My question is simply how to make that folder into just lib.
I could rename the folder and update the manifest file, but that's a bit of a
hassle for every time I export.
You can simply use "FileSync" plugin for Eclipse to sync your JARs (or any other files) to the project export folder (or any required folder).
Please refer below URL for "FileSync" plugin for Eclipse:
http://andrei.gmxhome.de/filesync/
I suggest you could check "save as ANT script" in the export dialog, and then edit the script and do the desired replacing.
Notice that all the paths within this script are absolute, in case you want to move/copy it to another folder.

Exporting a Java Project as .jar with including .java source code and preserving external libraries dependecies

I know how to export a Java Project as a .jar including also .java source code (just export it as a plain .jar), and how how to export it as a runnable .jar including only .class files (just select the runnable .jar export style). If the project doesn't have dependecies on external libraries, I noticed that both .jar run correctly by command line.
Problem: if the project contains dependecies on some external library added in the Build Path, it results that the .jar exported as runnable correctly works while a plain .jar doesn't because the external libraries are not anymore found when launching from command line.
Annotation: external libraries are added as .jar in a folder "lib" in the project and from there added to the Build Path.
I want to be able to export the Java project while either including the .java source code and preserving the dependencies on external libraries so that it will run when launching it by command line. Any suggestion? All that I can find out is "just export it as runnable .jar", but this will hide the .java source codes.
EDIT: Can anyone explain why this happens?
If you don't need an automated way you can achieve it with following manual steps:
First export it as runnable jar (a.jar)
Second export as jar with source (b.jar)
Use 7-zip or other archive tool to integrate the sources into the runnable jar. Open both jars and drag the folder containing the sources (first part of package name) from jar (b.jar) into the z-zip window of runnable jar (a.jar)

I'm trying to export a runnable jar in Eclipse, but I need it to include some other files necessary for running my program

Specifically, I am using JDBC libraries to work with databases in my Java program. When I perform "Export Runnable Jar" in Eclipse, it includes the referenced libraries but I am never given an option to include my assets folder (containing the databases) in the runnable jar file. If I try to export a regular jar file via Eclipse, I can add the assets folder, but the referenced libraries are not included.
What should I do here?
Thanks a lot.
Try using Ant scripts or maven script to do this.
Where you can import all the referenced jar and files to your desired location.
Personally i liked ant script, since it was easier and you will surely get lot of results for the same if you google.
Are your asset directories declared as Source Folders in Eclipse?
Right Click on the folder, choose Build Path->Use as Source Folder. Then retry the export.
In the longer term, you should really be looking at a build tool such as maven or ant, they are much more flexible, and they are less dependent on Eclipse, so changes in your project configuration in Eclipse won't affect the final build so much.
It's been some time since I last generated a JAR file with Eclipse, and back in the day I remember the Fat-Jar Eclipse Plug-in to be quite useful.

Exporting individual packages to a jar - IntelliJ IDEA 10.5.2

Does anyone know how I can export individual packages/class files into a jar with IntelliJ IDEA 10.5.2 instead of exporting the whole project?
Thanks.
Create separate module (right click on the project, then New -> Module) for each packages/classes set you want to be a separate jar.
For anyone who comes here for the answer:
You can make the jar using the command line. All you have to do is, run the command in the parent folder of the package you want to export as jar.
e.g You only want to export "mypackage" which is situated in "project/src/mainpackage/anotherpackage/mypackage"
you go into "anotherpackage" and run the following command.
jar cvf myjar.jar mypackage/
for more information on the jar command, take a look at the documentation: https://docs.oracle.com/javase/tutorial/deployment/jar/build.html
NOTE:
if you make a jar of only the source files i.e. .java files inside the src folder, you won't be able to use it as a library in other projects.
Be sure to make a jar of the .class files(i.e. compiled files) for such use.
You can quickly export the jar package you want to export by installing the Handy Export Jar plug-in.

Categories