Exporting individual packages to a jar - IntelliJ IDEA 10.5.2 - java

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.

Related

How do i make a Jar file from an already existing Jar and my own classes?

For example I want to create a Jar file C.jar from A.Jar(like from rt.jar from Jre) and my own classes(Ex.java file).
I am using Eclipse IDE.
If you want to create Jar file using eclipse
then you can follow this link. Put your class files and existing A.jar then perform export operation to create C.jar.
If you want to create Jar file using cmd then use jar cf C.jar *.jar *.class command to create new Jar file with classes as well as existing A.jar.
If you are using maven to build your jar then you can make use of the maven-assembly-plugin. This plugin will build a jar that will contain your code as well as all packages contained in the project's dependencies. See here for the documentation: http://maven.apache.org/plugins/maven-assembly-plugin/

How can i export a jar file with libraries in eclipse? (Not executable jar)

How can i export a jar file in eclipse with libraries? Executable jar wont work for me so that is out of the question. There is no option to do that when i try to export it..
If the jar is not running try to run it using a CLI for example command prompt java -jar <name>.jar and see the errors.
When you export the .jar there are 3 options .
One and two produces the .jar with the libraries inside.
Third option is producing a .jar (executable) with the libraries added
a different folder[ which has the same name as the jar file and it
must be named like it].
Mention that:
You may have serious problems and your executable will not run if your application has duplicate entries. So most of the time the third bullet is the best solution.
Sample image:
If you are trying to create a .jar to add it as a library to other projects then you should follow the below:

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)

Eclipse Java project: can I set eclipse to store .class and .java files in 1 folder?

I just need a simple environment of .class and .java files in a single folder so that I can execute java files later on using "java xx" in command line without adding any extra syntax. I'm not planning to use packages or sub-directories in my project and I won't be executing any files directly from eclipse either.
You could create a executable jar file and run it from command line using:
java -jar MyApplication.jar
Here is the link to create an executable jar file from eclipse:
How to create an Executable jar file
I think you are looking for this:
Create new project->Java Project->In the Project Layout section change to 'User project folder as root for sources and class files' (using Eclipse Kepler, it should be similar in other versions of Eclipse).
But keep in mind that it might become a mess after some time.

java eclipse create executable jar

I used eclipse to create executable jar. It relies external other jars.
In Eclipse, It is simple that you just need to choose Extract required libraries into generated JAR.
You can create an executable jar. It can be executed any places where jre is installed.
But If I use command line to compile jar.
javac -classpath [external jars] *.java
jar cfm [a name].jar manifest *.class [external jars]
It can generate jar. But the jar can only be executed in the directory where it is produced.
If I put it into another directory or machine, it complains NoClassDefFoundError.
So, my question is that how I can generate executable jar using command line as Eclipse.
A jar file cannot have its dependency jars inside. In case of Eclipse, it will unpack all the classes from the dependency jars and will bundle it into your single jar along with your class files. If not in the eclipse way, you need to
1) Create a manifest file which lists all the dependency jars
Manifest-Version: 1.0
Main-Class: Your Main class
Class-Path: dependency1.jar dependency2.jar dependency3.jar
dependency4.jar dependency5.jar
2) Create your jar with your class files using the class path including all the dependency jars and using the above created mainfest file.
3) In this same folder where you created your jar, place all the dependency jars.
Now your folder will look like this,
yourjar.jar (With the manifest file you created above)
dependency1.jar
dependency2.jar
dependency3.jar
dependency4.jar
dependency5.jar
4) Now if you want to share this, you need to share this folder and you can launch your jar from this folder. This is your executable folder and you can run it from anywhere.
Eclipse use Ant to package jar file, you can save the ant script that eclipse use to generate the jar checking the checkbox Save Ant File in the export window :
so, you can generate the Ant Build.xml script and then execute it using ant directly from the command line without using eclipse anymore if you want.
My preferred method for creating an executable jar is to use a utility called one-jar. I have a blog post discussing how to use it in maven and ant: my one jar blog post

Categories