I have the "SomeJarA.jar" with the followiung structure:
SomeJarA
|
|--lib/SomeJarB.jar
|--com/.../SomeClass.class
Im trying to execute the "SomeJarB.jar" in "SomeClass.java" using:
Runtime.getRuntime().exec("java -jar SomeJarB.jar");
Is this possible?
As far as I know, if there's a way to achieve this, it is going to be a tricky and highly unsupported arrangement. I'll look around for some supporting documentation, but I'm inclined to say "No."
This related link might help, though.
EDIT: Found the reference I wanted. It's here. Specifically, there's a note which says, "The Class-Path header points to classes or JAR files on the local network, not JAR files within the JAR file or classes accessible over internet protocols."
This is from user #Tezar and Question Classpath including JAR within a JAR
You can use "Class-Path" in your manifest file.
For example:
Create manifest file MANIFEST.MF
Manifest-Version: 1.0
Created-By: Bundle
Class-Path: ./custom_lib.jar
Main-Class: YourMainClass
Compile all your classes and run
jar cfm Testing.jar MANIFEST.MF *.class custom_lib.jar
c stands for create archive f indicates that you want to specify file v
is for verbose input m means that we will pass custom manifest file
Be sure that you included lib in jar package. You should be able to run
jar in the normal way.
based on: http://www.ibm.com/developerworks/library/j-5things6/
See the linked Question. It seems like the sanest method for jars in jars and avoids third-party class loaders. If you feel like upvoting, go to Tezar's answer and upvote that entry, not this one, as this is just a copy.
Related
Hi Guys I have included the Webcam-Capture API in my project.
When I run it in Netbeans everything works fine. But when i compile everything to a runnable jar i get this message trying to run it by cmd line.
can anyone of you help me?
i already tried to unbound and rebound all jars and changing jdks but its not working
add -classpath flag in the command line ,pointing to the path where Webcam-Capture API exists in your file system, unless you want to create a single package executable.In your case It should be something like below
java -classpath YOURJAR.jar;folder_of_dependant_jar/*;. com.awesome.pagackage.Starter
Where YOURJAR.jar contains the com.awesome.pagackage.Starter.main(String args[])
You also mentioned that your jar is a runnable jar it also means that while exporting/building you can do one of the following way.( NOTE , this feature is in eclipse , but you would get the idea ).Each of the following options you see in the library handling does specific things.
The first option: Extracts the dependent jar into your target jar as java packaging.This means if your package is com.awesome.package and the dependent jar has package logic.package; , after the runnable jar is build you could find both these package exists in your jar file.
The second option: I think it is more on eclipse specific since eclipse adds few classes of its own , of runnable generation, so I am not explaining it here.
The third option : is the most interesting one. it creates folder stucture like below
ndon_lib\external.jar ( external jar file )
ndon.jar ( your jar file )
This time the manifest.mf file contains something like below.
Class-Path: . ndon_lib/external.jar
Main-Class: com.awesome.pagackage.Starter
You should set the classpath
java -cp "your.jar" "yourclass"
We are trying to make a jar out of our class files but somehow it does not work. The creation of the jar-file works fine with
jar -cvfm client.jar Mainfest.txt /home/pi/Desktop/Client/*.class
But when we try to run it comes with a classnodefferror. Our application is using 2 property files - one for database and one for log4j.
The directory and the one subdirectory included looks like this:
See link - image nr. 1 and 2
When we try to execute the jar file it shows this error:
(See link image 3)
Normally when we run it, we type (see link image 4)
And the manifest-file looks like this (see link image 5)
We have tried different solutions like changing paths, leaving out environment (-D) etc. and we really can't figure out what we do wrong.
There are several problems here:
It is better to explicitly mention each included JAR file in the manifest - wildcards like * isnt working too well
You can't use absolute paths in a JAR file - use relative paths instead
So, 1) change Manifest.txt to
Main-Class: Client
Class-Path: . includes/log4j-1.2.17.jar includes/mysql-connector-java-5.1.33-bin.jar includes/sqlitejdbc-v056.jar includes/RXTXcomm-2.2pre2.jar includes/..... (repeat for all pi4j JAR files)
And, 2) copy ALL the JAR files you need (including RXTXcomm-2.2pre2.jar and the pi4j JARs) into the include/ subdirectory, and change command to
jar -cvfm client.jar Manifest.txt *.class *.properties includes/*
I have a jar whose content looks as shown below,
Below is my manifest file
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.3
Created-By: 1.7.0_06-b24 (Oracle Corporation)
Main-Class: org.sai.com.DerbyDemo
Class-Path: derby.jar derbyclient.jar derbynet.jar derbytools.jar
When i try to run the jar, it has thrown a ClassNotFoundExcception meaning it isn't referencing the jars inside the outer jar.
In the Class-Path attribute, how can I reference jars (derby.jar, etc) inside the actual jar?
You will need a custom class loader for this, have a look at One Jar.
One-JAR lets you package a Java application together with its dependency Jars into a single executable Jar file.
It has an ant task which can simplify the building of it as well.
REFERENCE (from background)
Most developers reasonably assume that putting a dependency Jar file into their own Jar file, and adding a Class-Path attribute to the META-INF/MANIFEST will do the trick:
jarname.jar
| /META-INF
| | MANIFEST.MF
| | Main-Class: com.mydomain.mypackage.Main
| | Class-Path: commons-logging.jar
| /com/mydomain/mypackage
| | Main.class
| commons-logging.jar
Unfortunately this is does not work. The Java Launcher$AppClassLoader does not know how to load classes from a Jar inside a Jar with this kind of Class-Path. Trying to use jar:file:jarname.jar!/commons-logging.jar also leads down a dead-end. This approach will only work if you install (i.e. scatter) the supporting Jar files into the directory where the jarname.jar file is installed.
You can't. From the official tutorial:
By using the Class-Path header in the manifest, you can avoid having
to specify a long -classpath flag when invoking Java to run the your
application.
Note: The Class-Path header points to classes or JAR files on the
local network, not JAR files within the JAR file or classes accessible
over internet protocols. To load classes in JAR files within a JAR
file into the class path, you must write custom code to load those
classes. For example, if MyJar.jar contains another JAR file called
MyUtils.jar, you cannot use the Class-Path header in MyJar.jar's
manifest to load classes in MyUtils.jar into the class path.
In Eclipse you have option to export executable jar.
You have an option to package all project related jars into generated jar and in this way eclipse add custom class loader which will refer to you integrated jars within new jar.
Default implementations of the classloader cannot load from a jar-within-a-jar: in order to do so, the entire 'sub-jar' would have to be loaded into memory, which defeats the random-access benefits of the jar format (reference pending - I'll make an edit once I find the documentation supporting this).
I recommend using a program such as JarSplice to bundle everything for you into one clean executable jar.
Edit: Couldn't find the source reference, but here's an un-resolved RFE off the Sun website describing this exact 'problem': http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4648386
Also, you could 'test' that your program works by placing the library jar files in a \lib sub-directory of your classes directory, then running from the command line. In other words, with the following directory structure:
classes/org/sai/com/DerbyDemo.class
classes/org/sai/com/OtherClassFiles.class
classes/lib/derby.jar
classes/lib/derbyclient.jar
From the command line, navigate to the above-mentioned 'classes' directory, and type:
java -cp .:lib/* org.sai.com.DerbyDemo
if you do not want to create a custom class loader. You can read the jar file stream. And transfer it to a File object. Then you can get the url of the File. Send it to the URLClassLoader, you can load the jar file as you want.
sample:
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("example"+ ".jar");
final File tempFile = File.createTempFile("temp", ".jar");
tempFile.deleteOnExit(); // you can delete the temp file or not
try (FileOutputStream out = new FileOutputStream(tempFile)) {
IOUtils.copy(resourceAsStream, out);
}
IOUtils.closeQuietly(resourceAsStream);
URL url = tempFile.toURI().toURL();
URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{url});
urlClassLoader.loadClass()
...
Add the jar files to your library(if using netbeans) and modify your manifest's file classpath as follows:
Class-Path: lib/derby.jar lib/derbyclient.jar lib/derbynet.jar lib/derbytools.jar
a similar answer exists here
in eclipse, right click project, select RunAs -> Run Configuration and save your run configuration, this will be used when you next export as Runnable JARs
I have this jar:
/mybundle.jar/
de/mybundle/myclass.class
lib/mysql.jar
META-INF/MANIFEST.MF
With the following MANIFEST.MF
Manifest-Version: 1.0
Class-Path: lib/mysql.jar
Main-Class: de.mybundle.myclass
It all seems perfectly correct for me, but when I run
java -jar mybundle.jar
I get a NoClassDefFoundException when the class tries to instantiate one of the MySQL-Library classes.
What did I do wrong?
You can't bundle jar files in other jar files. The paths specified in the Manifest are relative to the location of the jar file you're calling, so in your case relative to the location of mybundle.jar.
You have two options:
Either put the MySQL jar in the lib directory outside of your mybundle.jar.
Create a fat jar, which contains all classes from the required jar files in addition to your own classes. This is available from within Eclipse or Maven.
If your mybundle.jar is in c:/foo, then your mysql.jar has be in c:/foo/lib. The Class-Path in the manifest is relative to the executable JAR the way you've written it.
I get: Could not find the main class: org.dav.kin.Tester. Program will exit. when I attempt to run my jar file via java -jar tester.jar or java -classpath tester.jar org.dav.kin.Tester Does anyone know what is wrong and how to fix it? Below are additional details. Thanks.
Manifest File:
Manifest-Version: 1.0
Created-By: DKin
Class-Path: .
Main-Class: org.dav.kin.Tester
jar tf tester.jar
org/
org/dav/
org/dav/kin/
org/dav/kin/Tester.class
org/dav/kin/TesterCellRenderer.class
...
...
META-INF/
META-INF/MANIFEST.MF
UPDATE:
Jar file runs if I specify the system classpath, which contains the groovy-all-{version}.jar, like so: java -classpath tester.jar;"%CLASSPATH%" org.dav.kin.Tester Anyone know why I have to explicitly re-state the classpath (or more precisely, the groovy jar)?
Your jar file lacks a file with this name
/org/dav/kin/Tester.class
or you have special characters in your MANIFEST.MF file
MANIFEST.MF files have a particular syntax. It's best to use other tools to generate them; however some of the details I've encountered which increases the success of hand written files include:
Always make sure the lines are less than 72 characters long.
Always use \r\n (windows newline), even on non-windows systems.
Verify that all whitespace characters are spaces.
Verify that there are no nonprintable characters (htab, etc).
Sometimes a blank line at the end of the file helps.
Is Tester.class' package declaration org.dav.kin?
You have indicated that the you are using Groovy. Groovy does compile down to Java class files but it still requires the groovy runtime libraries. You need to make sure groovy is on the classpath as well as your classes. Try this:
java -classpath tester.jar;groovy-all-1.8.0.jar org.dav.kin.Tester
Just in case. I just solve exactly the same problem.
Instead of
Class-Path: .
in MANIFEST.MF
one should enumerate (with space) jars which are required in runtime, so it should be something like this:
Class-Path: groovy-all-2.4.5.jar relative/my-dependent-project-artifact.jar