Jar file with manifest file stopped working - java

A jar file I had created in the very past (xl-importer.jar) stopped loading. The MANIFEST.MF file is the following:
Manifest-Version: 1.0
Created-By: 1.5.0 (Sun Microsystems Inc.)
Main-Class: class/Client
Although there is the Client class inside folder /class/ inside the .jar file, I always get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: class/Client (wrong name: Client)
However when I run java Client inside the /class/ folder, application runs fine.
Jar was created some years ago with a 32 bit compiler. My customer moved to Windows 2003 Server some time ago. Is that the reason? Jar file was loading before that.

In the java tutorials it specifies that Main-Class is specified with :
Main-Class: MyPackage.MyClass
So it could be that you need to replace the "/" with a "."
If the class is in a directory called "class" they need to be put in the class package. "class" is a keyword in java so you can't use it for a package name. If you change the folder name to somehting else, for instance "classes", and add a package statement to your java file, then reference the class using a "." rather than a "/" it should work. It did with mine anyway :) e.g.
Manifest:
Manifest-Version: 1.0
Created-By: 1.5.0 (Sun Microsystems Inc.)
Main-Class: classes.Client
Client.java:
package classes;
public class Client{
//do code...
}

this is what a newer Manifest looks like (java 6)
Manifest-Version: 1.0
Created-By: 1.6.0 (Sun Microsystems Inc.)
Main-Class: MyPackage.MyClass
My guess is that java 5 used a different protocol.
Try changing the main class as shown above.

Related

absolute versus relative path names in jar manifest

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

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?

Reading a Jar's Manifest on OS X

I have the following manifest in a jar file:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.3
Created-By: 1.7.0_67-b01 (Oracle Corporation)
Main-Class: tld.project.Example
Name: tld/project
Specification-Title: Example
Specification-Version: 1.0
Specification-Vendor: Me
Implementation-Title: tld.project
Implementation-Version: 20140913
Implementation-Vendor: Me
I use the following to read some attributes from the manifest at runtime:
// Try and load the Jar manifest as a resource stream.
InputStream manInputStream = Example.class.getResourceAsStream("/META-INF/MANIFEST.MF");
if (manInputStream != null) {
// Try and extract a version string from the Manifest.
Manifest manifest = new Manifest(manInputStream);
Attributes attributes = manifest.getAttributes("tld/project");
Why does the above code set attributes to null, but only on OS X? (1.7.51) If I check manifest.getEntries().size() it also returns zero, but again, this works as expected on Windows and Linux. Any ideas?
Example.class.getResourceAsStream won't necessarily look in the same jar file that contains Example.class, it'll search through all the JARs available on the class loader and give you the first matching resource it finds. This isn't Mac OS specific, it's the same rule on all platforms, but presumably your classpath is in a different order when you run on mac compared to on windows or Linux and you're getting the manifest from a different jar.
You can use getResource to see which manifest you're actually loading.
If you specifically want the jar that contains Example.class you can find it with
Example.class.getProtectionDomain().getCodeSource().getLocation()

Failed to load Main-class Manifest Attribute

I am getting following error while executing compiled jar file. I have re installed my java but my problem is not solved yet.
Failed to load Main-class Manifest Attribute from
D:\Tools\Lodable_Creation\dist\Lodable_Creation.jar
Currently by MANIFEST.MF file looks like.
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.1
Created-By: 1.6.0-b105 (Sun Microsystems Inc.)
Main-Class: main
X-COMMENT: Main-Class will be added automatically by build
I am using Netbeans 6.9.1 IDE.
Use a package for your class. Make sure your class looks something like this (notice the package and the public class):
package com.foo;
public class Main {
public static void main(String[] args) {
}
}
After which you can specify Main-Class as so:
Main-Class: com.foo.Main
As adarshr suggested, JVM is not able to find the class because it requires the fully-qualified name in the Main-Class attribute of Manifest file.
Actually, it is not really necessary to specify the main file. You can just give your JAR file as your classpath, and give the fully-qualified name of the class to run it using java.
Say your JAR is myJar.jar and the fully-qualified main file is com.user.Main. Then from the command line, go to the directory which has your JAR File and give :-
java -classpath myJar.jar com.user.Main
And this will run the Main class. You would also need to give the classes (or JARs) in the classpath which are used (imported) in your Main class.
See this link for the details.
I have encountered this error when I have developed projects with a JDK(1.7 in my case) and the installed JRE was an older version(1.6). Try to update your JRE or change the JDK used, if possible, to match your JRE version.

Categories

Resources