Difference between extracting and packaging libraries into a jar file explains what happens if you select each option respectively.
What I don't understand is when one option would be recommended above the other.
Extract required libraries into JAR - Extracts the actual .class files from the libraries your app uses and puts those .class files inside the runnable JAR. So, the runnable JAR will not only contain the .class files of your application, but also the .class files of all the libraries your application uses.
If the JARs are kept separate, then the user would only have to download the JAR that contains your application code, instead of a single, massive JAR that contains your application code and all the library code.
if you update your application, then the user will have to download more data to update the application.
1)Package required libraries into JAR - Puts the actual JAR files of the libraries into your runnable JAR. Normally, a JAR file within a JAR file cannot be loaded by the JVM. But Eclipse adds special classes to the runnable JAR to make this possible.
2)Copy required libraries into sub folder next to JAR - Keeps the library JARs completely separate from the runnable JAR, so the runnable JAR will only contain the .class files of your application.
Related
I have made a utility java project which contains XML, DB and other such utility classes. This requires 3rd party dependencies on some of the Apache common libraries, so I have added it in my java project by configuring the build path in eclipse Juno. Now I want to export this java project as jar file. When I am exporting this project as runnable jar file, it is working fine i.e if I include this jar in some other java project, I am able to access the utility classes, but when I am simply exporting the utility project as jar, I am not able to use it. Runnable jar requires a main class, but I don't want to keep a main class in my utility java project. I have compared both the jar files. The difference that I found out was that in runnable jar file, there is no .classpath file but a simple jar file that is in there. In the runnable jar file, all the jar files are mentioned in file named MANIFEST-INF.mf file, but in simple jar file it contains only version of .mf file. Can anyone tell me how can I make a jar file without a main class and use it for my other java projects so that I just have to include the jar file and use it as it is.
I assume it has not been provided in eclipse because extracting external 3rd party library classes in a utility project is something non-standard. It can result in a lot of problems as your project grows, class conflicts being one of them.
Still if you want to continue with your approach, yon can export your project as a normal jar project. Thereafter after open your project as well as the other third party libs in a utility like 7zip and drag-and-drop your third party library contents(except META-INF) into your project jar. That's all.
If you however want to automate it, ant would be your friend.
If you just export the project to jar file, you can package the jar(war) by using the apache ant tool.
Do something like this:
<jar destfile="${dist}/lib/app.jar"
basedir="${build}/classes"
excludes="**/Test.class"
/>`
Kind of a "simple" question here, but it seems complicated to do such a thing using Eclipse.
I have a "utils" project, in which I have developped "common" code like xml parsers, loggers, maths calculations, debug utilities and such.
This library is based on several other external libs (commons-lang-3.1, colt-1.2.0, jdom-2.0.4) to work, and it's a non-runnable JAR file (ie, there is no main(), just utility code to include in other projects).
What I want to do is, when I create the JAR file of my project, extract all external JARs (common-lang, colt, jdom) in the JAR file, in order to automatically use them on other projects.
The reason is that I don't want to re-include common-lang, colt and jdom on every projects based on my lib, but use the ones packed in my lib JAR file (and it's a way to ensure that I'll be using the same version of those libs in the projects based on my lib, too)
I know how to do that with a runnable JAR file.
See for instance : How to create a jar with external libraries included in Eclipse?
But in a non-runnable JAR file :
all my classes are correcly exported in a correct directory structure based on the packages I created (hopefully ;-)),
but external JAR files are exported "as-is", so when I import my JAR in another project, those JARs (common-lang, ...) can't be seen, and I have to re-import them.
Is there a simple way to export this JAR file with a directory structure in which all external JARs files are extracted in the generated JAR file, too ?
Regards
Have you seen JarSplice?
JarSplice merge all your jars and native files into one, easy to use executable jar file via an easy to use GUI.
I have a jar file that references 6 other jars through a manifest file.
I now, however, want to try and compile the jars into one jar file, reasons being: I want them to be cached so the applet does not have a long loading time as one of those jars are required, rather one big waiting time to download all required files and then store it in the cache.
I have tried including the referenced Jar files in the build, unfortunately, all this does is cause a java.lang.NoClassDefFoundError error, so I had to add a manifest file.
What is the best/easiest way to achieve this? I am using eclipse, and not building with ant.
Using eclipse while exporting you can create with the option of
Package required libraries into generate JAR
By doing this eclipse uses its own class loader and reference all the files which are packed in a single jar.
In Processing, I'd like to import a library I've written in Java which wraps around an external library (a .jar file).
Processing appears to import my own library, but when I call the constructor (which references some classes in the external library), I get a java.lang.NoClassDefFoundError in the Processing GUI console.
If instead of including the External .jar in my Eclipse buildpath, I use the library's actual source code and export all of that, Processing does not complain.
How might I be able to package up this project so that I don't have to include all of the library's source code (and instead just include the .jar file) in my Eclipse project?
Update: As per N.L.telaviv 's suggestion, I chose to export as a Runnable JAR. What worked for me was to select the option "Copy required libraries into a sub-folder next to the generated JAR." Here, another folder is created next to where your .jar is output containing the referenced .jar libraries.
Update2: The other option which works is "Extract required libraries into generated JAR." Doing so allows the JAR to access the references internally. It seemed as though the same would have worked with N.L.telaviv 's suggestion, but that does not appear to be the case. Choose this option if you don't want to have any extra files. You'll notice, if you explore the .JAR file, that the external library is in a separate package folder.
Use the Runnable JAR file export option.
Click on your project - export - java- runnable jar file - select the option to pack requires libraries in the jar
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.