absolute versus relative path names in jar manifest - java

I want to reference a jar file in fixed location for use by multiple executable jars rather than include that jar in each of the executables. I have the following setup which works fine
commons-math3-3.6.1.jar exists in directory testgradle. TestGradle.jar contains the main method and exists in directory testgradle/build/libs
from testgradle/build/libs I run:
java -jar TestGradle.jar
And things work fine. The manifest in TestGradle.jar contains:
Manifest-Version: 1.0
Class-Path: ../../commons-math3-3.6.1.jar
Main-Class: com.spgxyz.test.testg
But I want to address commons-math3-3.6.1.jar with an absolute path so that executable jars such as TestGradle.jar can use it from whichever directory they reside in. However, if I change TestGradle.jar manifest to include the full path:
Manifest-Version: 1.0
Class-Path: C:\Users\Admin\workspace\TestGradle\commons-math3-3.6.1.ja
r
Main-Class: com.spgxyz.test.testg
Then the command:
java -jar TestGradle.jar
run from testgradle/build/libs produces:
Error: Could not find or load main class com.spgxyz.test.testg
Caused by: java.lang.ClassNotFoundException: com.spgxyz.test.testg
I tried various edits to the manifest to try to cure this such as:
Manifest-Version: 1.0
Class-Path: . C:\Users\Admin\workspace\TestGradle\commons-math3-3.6.1.
jar
Main-Class: com.spgxyz.test.testg
Manifest-Version: 1.0
Class-Path: TestGradle.jar C:\Users\Admin\workspace\TestGradle\commons
-math3-3.6.1.jar
Main-Class: com.spgxyz.test.testg
These both produce the same error. If someone could shed some light on what's going on here I'd be very grateful. Running on windows.

Class-Path attribute is interpreted as a list of URLs, so, to use an absolute path (represented with a URL here), it should start with schema and use forward slashes.
Try the following:
Class-Path: file:///C:/Users/Admin/workspace/TestGradle/commons-math3-3.6.1.jar

Related

Problem setting class path without shortcut directory folder in java manifest

Earlier shortcut was allowed in a server so the code shown below worked fine for executing a jar file
Manifest-Version: 1.0
Sealed: true
Main-Class: org.test.MainClass
Class-Path: /PROGRA~1/Testfolder/hibernate3.jar
/PROGRA~1/Testfolder/org.springframework.web-3.1.1.RELEASE.jar
Now the system does not allow shortcut directory folder I have to use the full path and system does not identify such path.
Manifest-Version: 1.0
Sealed: true
Main-Class: org.test.MainClass
Class-Path: /Program Files (x86)/Testfolder/hibernate3.jar
/Program Files (x86)/Testfolder/org.springframework.web-3.1.1.RELEASE.jar
How should I specify such absolute path in a manifest file.
The reason is that you have blank spaces in your addresses. You should either put them in a path without space or replace them with %20. So your Class-Path entry would look like this:
Class-Path: /C:/Program%20Files%20(x86)/Testfolder/hibernate3.jar
I don't have access to a Windows machine now, if it doesn't work it could be due to the way Windows partition name should be presented in URL. You can also try and address that starts with file:///C:/ too (or maybe file:///C/.
Each entry in the Class-Path entry would be considered as a URL so you should escape them the way you escape blank space in URL and they should have a protocol.

executable jar Could not find or load main class

I've been struggling with this common error and just can't resolve it. This application is composed of multiple packages and runs fine within JCreator (at the moment I need to use this IDE rather than Eclipse).
My manifest file is here (there are 2 blank lines at the end):
Manifest-Version: 1.0
Created-By: 1.6.0_45 (Sun Microsystems Inc.)
Main-Class: C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\main\DPMain
I wrote a bat file to create the jar:
jar -cvfm DPlus.jar C:\COMPILE\MyProjects\douwe\classes\MANIFEST.MF
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\main*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\library*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\command*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\file*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\file\display*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\command*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\file*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\file\display*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\gui*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\gui*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\job*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\job*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\types*.class
C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\util*.class
When I try to execute with the command
C:\COMPILE\MyProjects\douwe\classes>java -jar DPlus.jar
I always get the error:
Error: Could not find or load main class C:\COMPILE\MyProjects\douwe\classes\dykstra\dplus\main\DPMain
Can anyone see what I'm doing wrong here?
Usually this error is due to MANIFEST.MF if the'res no application's entry point has been set.
Your manifest file should have this line of code
Main-Class: YourPackage.DPMain
Alternatively, you can do the following.
java -cp .;app.jar YourPackage.DPMain
In my implementation, there are something different from yours, you can refer:
(1) The folder(before compressed) structure
you need to add a META-INF folder and put your MANIFEST.MF in it
(2) The content in your MANIFEST.MF
I think your should use the package format instead of a folder tree format:
Manifest-Version: 1.0
Main-Class: com.loadtest.mgr.LoadTestStarter

Excelsior error: classpath does not contain main classes

I try to make an exe file of a jar file. I get a error like this:
"classpath does not contain main classes"
This error is from the program Excelsior.
Your manifest file should be like-
Manifest-Version: 1.0
Created-By: 1.7.0_67 (Oracle Corporation)
Main-Class: MyMainClass
Class-Path: .
You must have missed "Main-Class" or "Class-Path" attribute.
You can manually add these entries by editing manifest file.
Also, if you are looking for other alternatives, this will help you-
How can I convert my Java program to an .exe file?

Class not found despite of classpath in MANIFEST

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.

Cannot find class even when jar file is in working directory

I am struggling to get my Java program to run on AIX. I used Eclipse on Windows to create a runnable Jar file, jRams.jar below. I kept on getting a class not found error, until finally I put all the external libraries in the same directory.
$ ls
JAXB2_20081030.jar
JAXB2_20110601.jar
activation.jar
asjava.jar
commons-beanutils-1.8.3.jar
commons-beanutils-bean-collections-1.8.3.jar
commons-beanutils-core-1.8.3.jar
commons-codec-1.5.jar
commons-collections-3.2.1.jar
commons-configuration-1.6.jar
commons-digester-2.1.jar
commons-jxpath-1.3.jar
commons-lang-2.6.jar
commons-logging-1.1.1.jar
commons-logging-adapters-1.1.1.jar
commons-logging-api-1.1.1.jar
jRams.jar
jaxb-api.jar
jaxb-impl.jar
jaxb-xjc.jar
jaxb1-impl.jar
jremote.jar
jsr173_1.0_api.jar
log4j-1.2.16.jar
netty-3.2.4.Final.jar
$
Still, I get the class not found error.
$ java -jar jRams.jar
The java class is not found: com.jbase.jremote.JRemoteException
jremote.jar definitely contains JRemoteException. Why isn't this working?
UPDATE
Thank you for your straight-to-the-point answers. I now understand the nature of a java application and a manifest file far better.
Turns out my ftp client was transferring in ASCII mode and not Binary, so the jar files were corrupt. I have learned a great deal, nonetheless.
When using the -jar option, you need to specify which jar-files should be on your class path in the manifest file. Just having the required jar files in the same directory won't do it.
Add a line in your manifest that says:
Class-Path: JAXB2_20081030.jar:JAXB2_20110601.jar:....:netty-3.2.4.Final.jar
or skip the -jar option and launch using
java -cp JAXB2_20081030.jar:....:netty-3.2.4.Final.jar:jRams.jar pkg.JRamsMain
and it should work fine.
(Note that on *nix systems, as opposed to Windows machines, the jar files in the class paths should be separated using : instead of ;.)
Further reading:
The Java Tutorials: Adding Classes to the JAR File's Classpath
You need to add all those JARs to the runtime CLASSPATH by adding the -classpath parameter. AIX requires you to separate the JARs using :
You will have to specify the full path(if libraries not in the same directory as jRams) or just the names of the jar file in a manifest file (If all dependency jars are in the same folder). Alternative specify the path to all the dependent jars using -cp argument.
Example (This assume every dependency is in the same directory you are executing java command from):
java -cp commons-collections-3.2.1.jar; jaxb-impl.jar; etc.. ;jRams.jar package_to_class.MyMainClass.java
Where package_to_class is example: com.myproj.example.
EDITED.
Follow these steps to add "Class-Path" to existing jar file -
Create "newmanifest" file with following entry
Class-Path: additional/jars
Update existing jar file e.g. "classes.jar"
jar --update --manifest=newmanifest --file classes.jar
Inflate jar file
jar -xvf classes.jar
created: META-INF/
inflated: META-INF/MANIFEST.MF
Verify "Class-Path" is added to MANIFEST.MF
cat META-INF/MANIFEST.MF
Manifest-Version: 1.0
main-class: CLASSNAME
Created-By: 15.0.2 (Oracle Corporation)
Class-Path: additional/jars

Categories

Resources