Why doesn't an executable jar acknowledge jars inside it? - java

I made an executable jar that depends on other jars with the command prompt using the format
jar cvfm MyJarName.jar manifest.txt *.class dependentJar1.jar dependentJar2.jar
The jar was made properly and everything seemed fine... But when run, it gets runtime errors because it can't find the class files that my project refers to. Exploring the created jar, the other 2 jars that it depends on are in there just as they should be and they are listed in the manifest.mf class path, but for some reason java is dumb and doesn't actually look inside those jars.
When those 2 jars are in the same directory as the owner jar, java is able to find them and it works fine. But I don't want this; I want those jars to actually be INSIDE it. What's the deal? How can I make an executable jar with other jars inside it work?

You can use something like OneJar or jarjar (or any of several others) to pack up everything inside of a single jar.
If you're building with Maven you could use the Maven Shade Plugin.
You could use a custom classloader like JarClassLoader that will allow precisely what you want.

I don't think you could. Maybe it's somehow possible with customized classloader, but not from-the-box. Use maven shade plugin.

If you use an Ant Builder in Eclipse and put the referenced jars in a library accessible to your project, Eclipse will extract the necessary classes and include them in your distributable jar file.

Related

Java export jar including libraries

I have been looking around for some time now, but didn't a find way how to export a JAR (not runnable jar) that contains in it's build path the referenced libraries.
Using Eclipse, I have included the lib folder which contains the jars of the referenced libraries in the export process.
Importing that JAR to another project and calling some method results in a ClassNotFoundException.
Looking at the MANIFEST, I didn't see any reference to those jars in the classpath, though the jars are indeed included in the jar.
So my questions are:
1. Is there any way to accomplish the packaging of the non-executable JAR so it will include libraries?
2. Is there any best practice for building and deploying a jar that include other jars libraries?
I tried it too but it doesn't work for me. I added the final .jar file but it doesn't work.
So, I did a workaround.
Extract the .jar file that you want as a dependency.
Copy that content and put it all inside your .jar file.
Add your .jar file as dependency inside an eclipse project.
Run it and see if everything is ok.

Importing jar files in neabeans

I've been used to simply importing jar files into my netbeans projects and then being able to import them in my projects. For example, I included the guava.jar. I saw it under libraries and when I expanded it, I saw things like com.google.common.. So, I just said import com.google.common. in my own classes and I was able to use their methods. Similarly for commons-math3-3.0.jar. But now I similarly included joptimizer-3.2.0-sources.jar (http://www.joptimizer.com/usage.html). When I expand it, I see folders like "src.main.com.joptimizer.functions" However, this time when I go to my projects and try to say import src.main..., it doesn't work they are not recognized. Why did simply importing the jars work before, but not in this case? And what do I need to do in this case to be able to use the methods in the joptimizer jar?
You included sources to you project. Sources means jar with not compiled *.java files.
You should add jar with compiled java classes (*.class files).
Looks like there is no compiled library on http://www.joptimizer.com/usage.html site
and you should compile it manually.
Install maven tool, unpack joptimizer-3.2.0-sources.jar and execute
mvn package
in the root directory (directory with pom.xml)
You will find compiled library in target directory

Using JAR inside a jar

I created a JAR file from my java project.
Using Eclipse, I added a JAR as a referenced library in my own project.
However, now when I try to run my program's JAR using java -jar myProgram.jar, I get an exception stating that my referenced jar is not available.
So how can I create a JAR consisting a reference to a different JAR and make it work?
Right, an executable JAR cannot contain its own JAR dependencies.
You have to have the main class and classpath set in the executable JAR manifest, then package all your JAR dependencies along with the executable JAR in a relative directory structure that matches the manifest CLASSPATH. Reading this might help.
You need to use Eclipse's runnable JAR exporter. Since Eclipse 3.5 you've the following options when you rightclick project, choose Export > Runnable JAR file:
Either way, Eclipse should take care that you'll be able to run the JAR the way you want on the exported location.
See jarjar project. It is exactly what you are looking for. http://code.google.com/p/jarjar/

Netbeans (Java): 3rd library works but how could I compile it into jar?

I'm using 3rd libraries (substance, trident) and I added them as libraries (I added the .jar's) to my project in Netbeans. This works also but if I use the builded jar outside the project directory it doesn't work like it should (the choosen UI don't show), I get the error:
java.lang.ClassNotFoundException:
org.pushingpixels.substance.api.skin.SubstanceOfficeBlue2007LookAndFeel
I set that UI/LookAndFeel like that in my code:
UIManager.setLookAndFeel("org.pushingpixels.substance.api.skin.SubstanceOfficeBlue2007LookAndFeel");
How could I make this work/run?
You've got 2 choices:
Put the library jar on the
classpath.
Assemble\Build the library jar with
the regular jar.
For option 1, you most likely need the jar located "near" the main jar on the file system; though, this is not necessarily a requirement. When you run the jar you then include the library jar on the classpath.
For option 2, you use some type of tool like maven's assembly plugin or the fatjar plugin in Eclipse (sorry, I don't know what the analog is in NB).
I hope this helps.

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