When writing my projects, I use a number of apis and libraries. The projects compile - I have added the .jars to my classpath - however, the relevant .class files are not added to the final jar. Is there a way to force this? I'm not using an IDE like Eclipse (and would honestly prefer not to).
No, the classes aren't meant to be added to your jar file. Instead, the idea is that you supply the 3rd party jar files alongside your own jar file. If you really need to only have one jar file, you'd need to merge all the jar files together - but I'd strongly recommend that you don't do this unless it's absolutely vital. (You'll need to work out how to merge the manifests etc).
Related
I have read numerous posts regarding this, and I was still not able to find a clear-cut answer.
We have the need to use a proprietary SDK in our maven project and this SDK contains ~315 jar files that are needed for around 30 lines of code (SAP product). Every answer I read dealt with adding individual jars to your local maven repo. That is fine and I understand that, but is it possible to add an entire directory of libraries. These libraries are only needed for compiling the project since they are already on the classpath of the target server (They would all be scoped as provided in a pom).
I've tagged Netbeans 8 since that is the IDE I am using, so if anyone knows a hack to get a maven project in netbeans compiled using libraries on Netbeans classpath that would be a good solution as well...
JAR's are just java .class organized in folders and Zipped. Extract all those 315 JARs to somewhere, thus merging all of their content, and then Zip it again to one single fat JAR file. Add this fat JAR to your local repository as you have read elsewhere.
This other question can help you with the JAR merging thing: How to combine two Jar files
Although there are many messy workarounds for this, the ideal would be to let the compilation fail and search for the missing compile jars using a search utility like agent ransack you can search within the jars in that directory for the missing classes referenced in the compiler errors. As you find the jars you need, add them as dependencies with the scope of provided.
A less clean option would be to zip all of the jars, use the dependency plugin to unpack them to a folder and add that folder to the classpath of the build, then remove them or exclude them from the final package.
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 project with several third-party JAR files in several directories. Currently, the project uses some ant tricks to recursively include all jar files into the classpath. I want to build a deployment for another site which will include JAR'ing my own code into a single file and somehow including the other JARs that I need. Oracle claims that wildcards on the commandline will not recursively include jars. I want the deployment to work in Windows or Linux.
It seems like I have the following options:
Include ant with my JAR and run the existing script.
Somehow re-organize the jars to be in a single directory so I can use a wildcard in my classpath. Hopefully it won't break the third-party libraries.
Manually create a big, ugly classpath.
Does anyone know of an easier way? I'm inclined to go with #1 for now.
I'd go with #2. When you build your distribution, copy all the jars to a "lib" directory, then include them all using wildcards. I've never known a third party library to break when doing such a thing.
There shouldn't be much trickery to it using ant: use copy with flatten="true" and include the fileset(s) indicating the directories/jars to recurse through.
option 4: The goal is to make you app startable simply with
java -jar your.jar
The main class and the classpath are set in the MANIFEST.MF of your.jar. Use ant to create the classpath at build time. This can be either a big, ugly, nested tree or a big, ugly flattened tree in lib.
See here, here and here for examples.
You can use JarJar and put everything in a single JAR file. You'll need to verify third-party licensing and distribution terms to ensure you can repackage their libraries.
I have multiple JAR files, which I have to add to classpath in Eclipse.
Is it possible to combine 30 files in one file and include that file?
You can, but I don't think it would necessarily be a good idea to do so. Three possible reasons, and no doubt there are more:
It makes it harder to see where any one constituent file comes from.
It makes it harder to replace just one library
If there are files which are contained in multiple jar files, which should "win"? For example, all the jar files will have their own manifest... you may be able to merge them all, but not necessarily... and for other files there may well simply not be a sensible way of merging them.
My vote is to keep the jar files separate.
Your may want to have a look at jarjar.
If you use an ant task you can also go for zipgroupfileset:
<zip destfile="jarlibrary.jar">
<zipgroupfileset dir="lib" includes="*.jar"/>
</zip>
There are a number of existing tools for this:
Uberjar
Megajar
Onejar
This question seems probable duplicate. Please try to find answer from similar article in this forum
Clean way to combine multiple jars
The jarjar project is what you need.
There is another thread here that explains how to package jars for releases, including using Ant, jarjar and eclipse plugins.
First, you can. JAR is just a ZIP file. You can extract all stuff into one directory, then create zip file (even manually using WinZip or similar tool), call the result *.jar (if you wish) and add it into the classpath.
The only problem that will probably happen is if several jar files contain resources with the same name (and path). for example, manfest.mf, or other descriptors. In most cases it will not cause any problem but sometimes if application code uses these resources you can have trouble.
There are some automatic tools that do this. Take a look on JarJar.
BUT the more important question: why do you want to do this? If you do not use maven put all library jars to one directory named lib under your project. Then add all these jars to your classpath in eclipse using 2-3 clicks. That's it.
if you are using maven, include all your direct dependences into your pom.xml, compile the project, then say mvn eclipse:eclipse. This will create .classspath and .project for you. Now just use it.
Jar files are just ZIP files, so you can try to unzip all jars, put all files together and then ZIP them again.
So figure we have a Jar file with our Java application, and it has inside of it the Jar files for the libraries it depends of, a the jdbc or any other third party jar.
How do you proceed to obfuscate it with free software? I mean, obfuscate your code and leave the Jars untouched.
I tried with Proguard, to no avail. Even the author told me at the sourceforge forums it wasn't possible.
But you can do it manually with an ugly hack involving the renaming of the jar to zip and mangling with the inner data, so, why wouldn't a software be able to do it?
You can create a single jar from multiple jars using Jar Jar Links.
Then you process the result with ProGuard.
Presumably, you have some sort of build script in place to compile your source, package it with the other jars (One-Jar/Fat Jar/etc.), and build your jar.
You should be able to insert the obfuscation task between the compilation and packaging.
If your obfuscation tool of choice will only take a jar input, you should be able to compile, jar, and obfuscate your code independently. After that, have your script unjar the obfuscated file and do your packaging.
Don't waste your time. Anyone can decompile your obfuscated code, the only thing you're going to do is frustrate your legitimate users who want to debug problems with your software.
Obfuscation is a technical solution (and a poor one at that) to a legal problem.