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.
Related
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.
I'm trying to run a project from the command line (Ubuntu 14.04). The main class is called Demo, and I have a Demo.java, a Demo.class, and a Demo$1.class all in the same directory (I know it would be better to seperate them).
I wrote my own Manifest file, MANIFEST.MF, which just looks like this:
Main-Class:Demo
I made sure to end it with a newline.
Next, I want to create my .jar file. I did so with this command:
jar -cfm example.jar MANIFEST.MF *.class
Then, I try to run my project like so:
java -Djava.library.path=/path/to/dependencies -jar example.jar
I seem to get the following error no matter what I try:
Error: Could not find or load main class Demo
I've actually never compiled/run a Java project from the command line before, it's possible I'm making a stupid mistake and just can't figure it out. Any help is appreciated!
EDIT: Here are the contents of example.jar, according to vim:
" zip.vim version v27
" Browsing zipfile /home/ellen/bendersexample2/src/bendersexample/example.jar
" Select a file with cursor and press ENTER
META-INF/
META-INF/MANIFEST.MF
AnnotatedBenders.class
Demo$1.class
Demo.class
Demo$ModelType.class
ManualBenders$1.class
ManualBenders$BendersCallback.class
ManualBenders.class
Model.class
Problem.class
Solution.class
Solution$Verbosity.class
StandardModel.class
Here are the contents of the META-INF/MANIFEST.MF which is in the jar:
Manifest-Version: 1.0
Created-By: 1.8.0_161 (Oracle Corporation)
Main-Class: Demo
UPDATE: Here are the interesting parts of Demo.java:
package bendersexample;
public final class Demo {
/* Some functions */
public static void main(final String[] args) {
/* Some code */
}
}
I changed my MANIFEST.MF to the following:
Main-Class:bendersexample.Demo
And re-generated the example.jar file. I still get the following:
Error: Could not find or load main class bendersexample.Demo
Could there be an issue with how I generate my class files?
To generate the class files initially, I did the following:
javac -classpath .:/opt/ibm/ILOG/CPLEX_Studio_Community128/cplex/lib/cplex.jar *.java
Please let me know what else I should try! Thank you
The problem was just that had the Manifest in the /bendersexample folder and the was creating the .jar in this folder as well! I just needed to move that stuff into the parent directory and everything worked fine!
The final Manifest file used bendersexample.Demo as the Main-Class, and the jar was created and run from /bendersexample 's parent directory.
If anyone runs into this problem I would recommend taking a look at your project's structure before trying anything else, because this turned out to be a very easy fix!
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
I just dropped the JUnit folder into /System/Library/Java/Extensions/. I'm able to get JUnit to run, but it can't find my test class.
I'm running this command (plus a few variants) from the containing folder of the package (/containing_folder/package_name/):
java org.junit.runner.JUnitCore package_name.ClassTest
but it tells me:
JUnit version 4.10
Could not find class: package_name.ClassTest
My system info:
MacOS 10.7.2 • Java 1.6.0_26 • JUnit 4.10
Addendum:
I've moved JUnit per suggestion and tried running JUnit on my test class with the following bash script, but I'm still getting the same error message.
#!/bin/bash
export CLASSPATH=/Users/myname/Desktop/Programming/Java/junit4.10/junit-4.10.jar:/Users/myname/Desktop/Programming/Java/:/Users/myname/Desktop/Programming/Java/package_name.jar
java org.junit.runner.JUnitCore package_name.ClassTest
Any *nix or mac users see what I'm doing wrong?
Thanks.
P.S. No, "myname", "ClassTest", and "package_name" are not the real names used on my system - they've been anonymized.
You can use -cp option.
java -cp class_path_or_jar_separated_by_comma class_to_run
Goood Morning,
the trick is (like Dave Newton already said) to set the Classpath right. In a batch file it would look like this:
set CLASSPATH=D:\_zip\lib\junit.jar;D:\containing_folder
java org.junit.runner.JUnitCore package_name.ClassTest
The important thing here is to have the following things in your Classpath:
JUnit class files
Your class files, including your JUnit test classes
Libraries your class files depend on
This means you have to set the containing folder of your class files (in my example it's D:\_zip) in the classpath. So you can set the full classname (including packages) as an argument for the JUnitCore.
A good FAQ is here: http://junit.sourceforge.net/doc/faq/faq.htm#running_1
Update: I'm not sure how the bash script und MacOS works, but if you have your classes packed in a JAR file, it would be enough, to set the CLASSPATH inside the JAR files' manifest:
Manifest-Version: 1.0
Created-By: 1.6.0_26-b03 (Sun Microsystems Inc.)
Built-By: Gruber ^^
Implementation-Vendor: Company
Implementation-Title: Title
Implementation-Version: 1.0
Main-Class: package_name.ClassTest
Class-Path: /Users/myname/Desktop/Programming/Java/junit4.10/junit-4.10.jar
The Main-Class is not really important for the JUnit tests, but i left it here. The manifest file is set inside the JAR:
package_name.jar/META-INF/MANIFEST.MF
You can set it manually or Eclipse will set it for you (export to jar, etc), if you use it ^^.
With this Manifest set, the script should look like this (called from the same directory as the jar file):
set CLASSPATH=package_name.jar
java org.junit.runner.JUnitCore package_name.ClassTest
I am trying to configure the classpath of a JAR so that my ResourceBundle can pick up property files from it.
If I run it from the .class files and specify the -cp flag it works fine, and System.err.println(System.getProperty("java.class.path")); will print the path specified in the -cp flag.
If I try and create a jar file for it, System.err.println(System.getProperty("java.class.path")); always prints the path of the jar file, and the property files aren't picked up.
It seems if you are running it as a jar file you can't specify the -cp flag (which was what I was hoping, as it's common to switch which property files are being used). I've tried specifying it in the jar manifest instead, but it's still not working.
Here is the code and manifest from a test jar that doesn't seem to work:
public final class Test {
public static void main(final String[] args) {
System.err.println(System.getProperty("java.class.path"));
}
}
Manifest-Version: 1.0
Created-By: 1.6.0_20 (Sun Microsystems Inc.)
Main-Class: Test
Class-Path: /home/ajanuary/Projects/test/
edit
The original path was rather meaningless so I changed it. I want to point to a directory which the ResourceBundle can find the property files in.
If you use -jar, -cp is ignored:
-jar
Execute a program encapsulated in a JAR file. The first argument is the
name of a JAR file instead of a
startup class name. In order for this
option to work, the manifest of the
JAR file must contain a line of the
form Main-Class: classname. Here,
classname identifies the class having
the public static void main(String[]
args) method that serves as your
application's starting point. See the
Jar tool reference page and the Jar
trail of the Java Tutorial for
information about working with Jar
files and Jar-file manifests. When you
use this option, the JAR file is the
source of all user classes, and other
user class path settings are ignored.
Source: java - the Java application launcher
I would instead read a property in my Java application (that property could indicate from where resources should be loaded).
Example of how to execute the application would then be:
java -Dkey=value -jar application.jar
You can't use classpath wildcards in the manifest.
Take a look at Setting the classpath for more information on how classpath works:
class path wildcards are not honored
in the Class-Path jar-manifest header.
Yes, and for the shell, ~ means $HOME, but for java, it doesn't mean anything.
The problem was that I also had an index file. If you have an index file, the Class-Path will be ignored.