I have an application that makes use of about 20 external jar files. In order to package everything up into a single file for distribution, I've been unjaring all the externals into a particular directory and then rejaring it all up into one jar. The end result is about 2MB in size. I want to use jarjarlinks to cut down the size of the distribution jar, but I can't find any examples on how to do what I want.
Essentially, I want to only take from the external jars, the classes that I am using and discard all the rest. Can jarjarlinks do that? If not, is there some other utility that anyone would recommend?
My sources so far:
http://code.google.com/p/jarjar/wiki/GettingStarted
http://jonasboner.com/2005/12/09/stay-out-of-jar-hell-with-jar-jar-links.html
http://sixlegs.com/blog/java/dependency-killer.html
You could also use a tool called autojar.
Regarding the manifest file, you can add one with the -e option of the jar command once the jar is packaged:
jar -uvfe myjar.jar full.name.of.class.with.main.method
Related
I read and searched almost everywhere, yet i have no answer.
I have a project with multiple .java files, and i need to export them as a single .jar file.
One of the main questions is based on the fact that i don't have a main class.
What can i do to go through that ? Shall i create a main but never run it? Or there's any way around?
Offtopic ( might be usefull): i want to do this in order to use the jar file in IKVM
Thank you!
You do not need a main method to create a jar. There are many such jars out there such as utilities packages. They don't contain any code that would make sense to execute on it's own since it only contains code that makes your application easier to write.
First compile your code using Netbeans or command line javac utility. This will give you .class files.
Now for creating the jar... You can simply create the jar (which is basically a glorified zip archive) by using the command-line jar tool.
jar -cvf my archive.jar myprogram/
Make sure that the file path within the jar archive match the package name of the classes. It is not uncommon to accidentally get an extra layer of directories.
You probably should have searched stackoverflow first since how-to-create-a-jar-file-in-netbeans seems to cover exactly what you're looking for.
Do you want to use the jar as a external library or a program itself?
If it will be a external library, you don't need a main class. In eclipse you can export as a jar file (select classes-> right click-> export as jar).
If it will be a program itself, then you need a main class.
You really need to create main class file, write code in function main(String args[]) which use your java classes and compile it by javac.
Is there any way I can create Java projects using a simple text editor? Not an IDE like eclipse?
I want to be able to create .jar files without the assistance of an IDE, I have all the JDK commands installed already on my computer (such as javac)
I'd like to know what file structure I need, how I need to arrange my class files, what compilation steps I need to go through etc. to be able to create jar files.
Yes, completely doable (just not much fun once the project gets bigger).
I suggest if it's not a throwaway project, use a build tool like Maven or Gradle to manage your build process, so that you don't need to assemble classpaths and resources yourself, but still retain full control of the build and test lifecycle, without IDEs. This comes at a complexity cost, of course, but once it's set up life becomes easier.
See also How can I create an executable JAR with dependencies using Maven? or the Gradle docs about creating JARs
I'd highly recommend the standard Maven source directory layout too (src/main, src/test etc) as it's both commonplace and makes for easy integration with the above tools.
Follow the below steps to create a jar file
Compile the java source using javac
Create a manifest file (if main exists) to identify main class
Use the below command to create a jar file
jar -cvfm *.class
Yeah. You can create your project structure without an IDE. But it's time consuming and you have to do everything.
To talk about creating JAR, you don't want any extra software. You can use jar utility, which comes with JDK.
Here are steps to create jar:
Compile classes which you want to in jar
Create manifest file (.mf). It's needed if you want to make jar as executable. If you want to bundle classes only together, then no need. (eg. Dependency jar)
Go to command prompt and execute following command "jar cvf MyJarName.jar *.class". Make sure java is set in environment path and you're inside the directory of classes.
cvf means "create a jar; show verbose output; specify the output jar file name.
That's all. If you want to include any folders inside jar then you can use folder name in above command after classes and it must be separated by space.
Example: jar cvf TicTacToe.jar TicTacToe.class audio images
I have a Jar file that has the a few packages.
I want to import the commons library and within Eclipse that's pretty simple.
However when I export the jar file, and run it, it is not able to find the commons library and therefore unable to run. I want to know if I have to move the commons jar files to the exact same directory (within the package)? Or if there is something I can add to the import lines to make it work?
I also want to make this jar file able to be sent to people and used by anyone without having to get them to modify anything on their computer
you need to include jars in your classpath if you are running on command line (Here is an example of this)
if you want it to all be neatly wrapped in one jar consider looking at OneJar. (oneJar example) or JarJar.
I am new to Java and am using Eclipse to write Java code.
I've added lots of library (.jar) files as referenced libraries. I've also exported my project as a JAR file. My question is if I run this file on a computer where the referenced libraries are not at the same place as in my computer, will it run successfully?
I also made a runnable JAR file, whose size was much larger (~29 MB) as compared to the previous file (~24 KB).
My question is if I run this file on a computer where the referenced libraries are not at the same place as in my computer, will it run successfully?
No. You need to package the referenced JARs alongside your program JAR. It would be inconvenient for the end-user to have to download all the libraries that your program depends on. A couple of options, both possible using the Eclipse Export function.
Unpack all your dependent JARs and package them together with your code into one single executable JAR.
Specify in your program JAR manifest the classpath, which will contain relative paths to the location of your dependent JARs. These could be for example in a lib folder. The location of the lib folder relative to your program JAR would need to be the same for all your end-users, so it would make sense to package it all together.
It depends on the way you package jars with the runnable jar app. if you do it inside the jar or outside it then you should make a classpath entry to your manifest.mf.
you can even bundle the reference library in the same jar. So as you reduced the size to that extent i assume you removed those from the jar, which is not good approach in most cases. It is not recommended not to include referenced jar, unless you are 100% (not even 99.9%) sure those library WILL be in the system you would want to run.
EDIT:
to include the referenced jar in eclipse, you need to goto
project-properties>java-build-path>order-and-export
here check whatever libraries you want.
Ok so i wrote a program that makes use of a 3rd party open source library and i want to package it with my program in a single jar. I'm using netbeans 6.8 and everything I've tried java always spit back the error:
java.lang.NoClassDefFoundError: libraryname;
off topic:also i would like to know how to make an executable-jar(exe) through netbeans if it is possible. (ive seen programs that were written in java but were an .exe)
EDIT discovered a plugin for eclipse called FatJar which can do what i want, but i cant find something similar for netbeans, is there such thing?
I'll start off with the obligatory disclaimer: Java executable JARs do not work this way. An executable JAR has a main class defined in the JAR's MANIFEST.MF file, and the MANIFEST also allows the definition of a class path to include libraries that the code in the executable JAR will need. The class path definition in the MANIFEST must enumerate every JAR or folder to put on the class path, relative paths are relative to the location of the executable JAR - not to paths contained inside the executable JAR. Executable JARs are launched with the "-jar" argument to the java executable, and both the java "-cp" flag and the CLASSPATH environment variable are ignored. As for why executable JARs were designed this way, you should be aware of the primary disadvantage of loading classes from JARs contained within JARs, even though the rest of this reply will focus on doing just that.
NOTE: I lost the original sun forum topic that explained it fully, but essentially it is because entries in the top level JAR can be read in a random access manner, but the entire embedded JAR must be read before any entries can be accessed, because the top level JAR might have compressed its entries.
I have used One-Jar successfully in the past, but the structure of the final resulting jar may not be what you expect. Essentially the One-Jar classes are the only non-JARd classes in the final jar; all other code (your code and any dependent library code) is included in the resulting as JAR as JAR files. Your application is JARed as a regular JAR file named "main.jar" in the final JAR's "main" folder. Any libraries your code needs is placed, as JAR files, in the final JAR's "lib" folder. And last but not least the final JAR's MANIFEST.MF file tells One-Jar what your main class is. Execution is a dead simple "java -jar final.jar [args your app uses]". I don't know how to take the next step of converting to an OS-native EXE regarding your off-topic question, but it would probably be best to use a different packaging mechanism than One-Jar anyway. I'm not sure how to go about this with NetBeans, my advice there is to use a build tool to package the final jar. Fortunately One-Jar provides instructions on generating the final jar with Ant, and that should be easily integratable into NetBeans.
I believe the Eclipse FatJar plugin creates a One-Jar executable JAR, so if that plugin seems to do what you want, then One-Jar is the way to do it. Personally, I used a Maven assembly.
There is a caveat - any signed libraries that require (or desire) to take advantage of Java's signed JAR verification may not work this way - Java Cryptographic Extension (JCE) implementations like BouncyCastle are a notable example. I think the reason is that the signature verification runs against the final JAR, not the signed library. Fortunately One-Jar allows the end user to add additional libraries to the classpath, something that is explicitly precluded when running an executable JAR; to workaround this you might be better off delivering the problematic JARs with the final JAR and an OS dependent launch script (.bat, .sh, etc).
I realize that this doesn't achieve exactly what you want, but I'll describe the customary method of distributing a standalone application. If it does meet your needs, you'll find that it's better supported by tools and more readily understood by users, because it follows established conventions.
Put your code in a jar (I'll call it app.jar) along with a META-INF/MANIFEST.MF file with entries like this:
Main-Class: com.y.app.AppMain
Class-path: third-party.jar blort.jar foo.jar
Then, you can throw all of the jars into a directory and run AppMain like this:
java -jar app.jar
If you want, you can put the third-party libraries in a single directory like lib and refer to them in the Class-path attribute using a path relative to the main jar: lib/third-party.jar That helps keep your distribution tidy.
My generic answer to your off-topic question is a (rather lengthy) article: Convert Java to EXE - Why, When, When Not and How. It has lots of links to free and commercial tools, but I have never seen a Netbeans plugin with such functionality, sorry.
To include another jar in your jar, you might find jarjar useful.
Executable jars just have a class defined as 'Main', if I'm not mistaken. This may be useful.
If there's not any concern of repackaging 3rd party jars into your final big jar, then this should be the easiest method.
If there are no licencing issues then the most preffered way is to unjar the actual jar and rejar it with your class files in it, to a new jar.
You can simply use the jar cmd itself for this, no big deal!!
if you use MAVEN, use "maven-shade-plugin" plugin. It will compile jar with all dependencies(3rd party and etc.)