java eclipse create executable jar - java

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

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/

Eclipse Exporting to jar ignores Manifest class path: directive

I am trying to use the Eclipse export function to create a jar file to be deployed in an Axis2 deployment in Tomcat. When I have source code projects, I can export this jar, rename it to aar, and it works fine. All the classes from all the projects are present inside that exported jar.
Now I convert a few of those projects to jar files (they form a library). The primary project now points to these library jars as external jars in the build path. It builds fine. Now I want to export the same type of jar file from this primary project that no longer has access to the projects containing the library source code.
SOO...I followed these instructions to create my own Manifest.txt file that would point to these external jars using the Class-Path directive. The line appears as follows:
Class-Path: file1.jar ../../libraryJars/file2.jar
So I follow the usual export to jar (not runnable of course; there is NO main here!) with the option to use my Manifest file and the Class-Path directive in that manifest is ignored. I look inside the created jar and the only classes I find are from the primary project. All those classes in the external jars were NOT loaded.
How do I get the classes inside the external jars to be exported with the classes in the primary project when creating this jar? I understood that using the Manifest.txt approach was the way to do this. Perhaps it only works when making a runnable jar (which I cannot do)?
I do NOT want to use something as messy as ANT. If I have to resort to script files to accomplish this task I will just do the copies with a bat file.

jar file creation - library dependecy

i want to create a jar file of my java web application.
This web application has so many jar files related to this. so all am including in the build path. And when the time of jar file creation from eclipse,even if i had checked the checkbox for exporting the class files and resources option, the generated jar file doesn't have those library files. So when am trying to execute the jar file from command line option by using
java -jar myJarfile.jar
it will throw error like NoClassDefFoundError , can anyone help me to solve the issue.?
How can i generate the jar with all dependencies.?
You shouldn't really be including external dependencies in your jar. Just bundle up your code into your jar and specify the dependencies at runtime.
java -jar myJarfile.jar -cp someDependency.jar

Using external Jar dependency in Eclipse with Non-Runnable Jar

I am making a Jar file in Eclipse to run an Applet, and the program uses an external jar file (jxl.jar). When I run the applet, it tells me it can't find the jxl files I am trying to use.
When I make the jar file, I right click the project and select Export and then JAR file. I don't see an option in the dialog that comes up to include dependencies. How can I ensure that it is including my external jar file, which is added to the external Java Build Path as an External Jar file in my Project Properties.
There are two ways to do this:
Specify the dependent JAR in the "archive" parameter; e.g.
ARCHIVE="myapplet.jar,jxl.jar"
Specify the dependent JAR in the manifest of your applet JAR; e.g. in the manifest.mf file add this line:
Class-Path: jxl.jar

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