creating jar file for java application - java

i have created a java application which uses data from its config folder and , it also uses third party jar files those are located in lib folder, could anyone tell me how to create jar file for this project with the content stored in config file and lib folder.
i tried creating jar using eclipse export functionality. when i run this jar file, it says it can not find the third party libraries that i have used for this project and configuration file.
thanks in advance for any help

You can create a Runnable JAR in Eclipse 3.4+ in the Export wizard selection dialog (right click on a project and go to Export...) using an existing launch configuration which will incorporate the libraries or repack them. Config files should be readable from the same directory as the runnable jar is located. If you need any help with loading these in, just ask :)
(source: eclipse.org)

You have two options
include the stuff in the third-party jars in your jar
provide access to the jars on the classpath when you run your jar.
Both have their benefits and their drawbacks.

Java does not support putting JAR files inside executable JAR files, so you can't just put your third-party library JAR files inside your own JAR - Java won't be able to find them.
If you don't want to distribute your application as a whole bunch of JAR files, you can use a look such as One-JAR which will build a JAR file for you that contains your own classes plus the classes of the third-party libraries that you're using.
To learn more about how to package a program in an executable JAR file, see Packaging Programs in JAR Files in Sun's Java Tutorials.

If you use netbeans just by click on "build" a jar file will show up in the "dist" file in your project directory

Related

How to make jar file with external jar libraries in netbeans 8.0.1

I am using NetBeans 8.0.1 compress jar option in properties to make a jar file of my project.
I want the jar file to include some external jar files in the libraries. However, my exported jar file does not have external libraries, which I want to include. How to add external libraries to a jar file?
edit [Wrong]
I'm not really sure what you did, but usually libraries have JAR-files which you add using the Properties->Libraries->Add Jar/Folder button.
When coding, you're able to use the library. When you build your project the JAR-libraries will be included in your new JAR. I've only used external libraries once, for reference.

Add jar library to eclipse project for exporting

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.

How to create a jar file of a java project which internall uses some external jars

I want to create a jar file of my project which is internally using some other external jars.
Basically what I am expecting is a single jar file which has all the class files and external files in it.
can someone help me in this
If your classes use external jar files then you need to enclose those jars into your jar file.
Right click on the project in eclipse then :
Click on 'export'->'Runnable Jar`
Here: Make sure you select 'Extract required libraries into generated JAR'
This will extract all the libraries present on the classpath of your project. into the exported jar file

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.

How do I include external JARs in my own Project JAR

I have a Java application and created a JAR file and deployed it.
The App uses external JARs such as the Log4J JAR. When creating my JAR file, how do I include all external dependent JARs into my archive?
In order to get my App working, I'm having to copy the Log4J JAR into the same directory as my own JAR which kinda defeats the purpose of the jar. Wouldn't it be more elegant to have 1 single JAR file to deploy?
If you use Eclipse, You can extract all included files into one runnable jar like this:
Right click on your project name from Package Explorer and select Export.
In Export screen, select Java -> Runnable JAR file and Next.
Fill in the Runnable JAR File Spec screen and Finish.
You can choose whether to package dependency jars as individual jar files or extract them into the generated JAR.
You could use something like One-JAR to package your Java application together with its dependency into a single executable Jar file (One-JAR uses a custom classloader to make JARs nesting possible).
You have to expand the library jars into the same place where your compiled classes go, then make a jar from that. Depending on how your build process is set up, there may be multiple ways to achieve this. It's not rocket science - a jar is just a zip archive with a META-INF directory at the root level.
Keeping JAR separate is better as it is easy to upgrade only the specific JARs to its new versions without touching any other configuration. As of your issue of having to copy each file to same location as of your JAR, you can always use Java CLASSPATH and include any JAR to your application's class path.
A JAR is not itself capable of nesting other JARs, as you discovered.
Traditionally, one would distribute a ZIP archive or other installer that would unwind the application JAR (yours) as well as any support JARs in the appropriate location for classpath access. Frequently, then, the application was invoked through a script that invoked the primary JAR and created a classpath that listed the support JARs.
As other posters have noted, you have some options to create a super-JAR if that's what you want.
You can use Maven + assembly plugin (http://maven.apache.org/plugins/maven-assembly-plugin/)
BTW, probably that's not the easiest way, if you did not work with maven.

Categories

Resources