Importing Jar files in another directory within your jar file - java

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.

Related

Convert .java files to single .jar

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.

How to add an external .jar to the build path via code

I am working on a program that will allow for modular addons to be installed by simply dropping the correct .jar file into a folder. I was not quite sure how to add a file like this to the build path and use a method from that file. I know how to do this if I know exactly what file I will be using, but in this case I do not. I will need to be able to run a method from the external jar without telling eclipse ahead of time that this is the file I will be using.
You can load the jar using method described on this answer: How to load a jar file at runtime
I would assume you also need an interface to act as a binding contract so you know what to cast your class to

How to package libraries into my jar using Ant

My application relies on two external libraries, I have both in jar format and have added them to my classpath, making it possible to run my application within NetBeans.
However, I would like to package my application in an easy to use jar file. When I tried the automatic method of jar creation provided by NetBeans (where it auto generates a jarfile in dist/) and ran it on another computer, I got lots of ClassNotFound (or similar) exceptions for classes that I could tell were supposed to be provided by my other libraries.
Is there a way I can include the other jarfiles I have into my own jar? I've never created an application which relies on other libraries before so this is a first for me.
You can add a "Class-path" line to your jar's manifest. The drawback is that you have to hard code the paths to a file system (not jar) location in the manifest. If you put them all in the same directory or a consistent relative directory, it should be manageable.
See: http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
Alternately you can try something line One-JAR: http://one-jar.sourceforge.net/

Using jarjarlinks to include only used classes in a jar

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

Modifying a jar file

I have a jar file which is used in html file as applet. I want to modify the content of the jar file and to rebuild the jar file so that the html will work fine with the new jar file. How can i do this??
I already tried unzipping using 7zip nad modified the source and created the new jar. But when i use it in html it shows some java.lang.Classnotfound error
You can unjar or rejar the classes and source files as you wish.
unjar
jar -xvf abc.jar
jar
jar cf abc.jar input-files
http://docs.oracle.com/javase/tutorial/deployment/jar/build.html
Make the changes in the code (.java files), recompile to get the .class files. Then simply replace the old .class files in the jar with the new ones. I usually use WinZip, but you can use whatever app that can handle .Zip files. It should just work.
I've faced cases where the launcher of the app uses some sort of verification and checks for this kind of changes. I had to use a new launch script. This doesn't seem to be your case though.
This is surely possible from the command line. Use the u option for jar
From the Java Tutorials:
jar uf jar-file input-file(s)
"Any files already in the archive having the same pathname as a file being added will be overwritten."
See Updating a JAR File
A brief test shows this quickly updates changes apart from trying to delete the file.
I haven't seen this answer on other threads about modifying jar files, and many, marked as duplicates, suggest there is no alternative but to remake the jar completely. Please correct if wrong.
JARs are just ZIP files, use whatever utility you like and edit away!
Disclaimer: When reverse engineering any code be sure that you are staying within the limits of the law and adhering to the license of that code.
Follow the instructions above to unpack the JAR.
Find the original source of the JAR (perhaps its on SourceForge) and download the source, modify the source, and rebuild your own JAR.
You can also decompile the class files in the JAR. This is a rather advanced process and has a lot of "gotchas".

Categories

Resources