executable jar Could not find or load main class - java

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

Related

How to use a jar file with another jar file [duplicate]

I have jar file with manifest in it
Manifest-Version: 1.0
Build-Jdk: 1.7.0_67
Created-By: Apache Maven 3.2.3
Main-Class: com.company.main.Main
Archiver-Version: Plexus Archiver
And the jar has compile dependency to external library
compile 'org.apache.commons:commons-lang3:3.3.2'
So I want to execute it comandLine I wrote:
java -cp C:\commons-lang3-3.3.2.jar -jar myJar-1.0.0.jar
But
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils
at ...
How to add this file on the class path?
PS. If I use the "bad way" and copy-paste commons-lang3-3.3.2.jar in the ...jre7\lib\ext folder. Everything is working.
If you're not using the -jar option then you need to specify the main class to run, as the manifest will not be interrogated:
java -cp C:\commons-lang3-3.3.2.jar;.\myJar-1.0.0.jar com.company.main.Main
The classpath (-cp) option is ignored if using the -jar option (in that case the manifest should reference any other required jars via its classpath directive).
"Bad Way"..? You should never include your jar files inside the java directories.. How do you expect the users of your application to use your jar when they are using the standard java..?
either you can use the command suggested by #tombola82 or you can include the commons-lang jar in your project itself so that you can refer it.

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

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?

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.

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