Unable to execute jar file due to main manifest attribute missing - java

I have below project structure:
->bin
->lib
->resources
->src
->DemoFramework
->FirstDemo.java
In lib folder, I have an external jar that I need in my application. Its name is ext.jar. In resources, I have Manifest.txt file, whose content is given below.
Manifest-Version: 1.0
Class-Path: . lib/ext.jar
Main-Class: DemoFramework.FirstDemo
I am using below command to generate jar:
javac -cp ".;./lib/ext.jar" src/DemoFramework/*.java -d bin
Basically I am putting all class files into bin folder so that in final jar file, source code is not visible.
Then I am issuing below command:
jar cmf resources/Manifest.txt project.jar bin lib
The jar file is successfully created but when I run it, it says:
no main manifest attribute, in project.jar
I am confused about this error, no idea why it is happening.
Can you guys help me to sort it out?
Thanks.

Related

Error: Could not find or load main class when trying to run jar file

I'm trying to create a JAR file of my Java code. There are four classes - A.java, B.java, C.java, and D.java. A.java has the main method. These are in a folder called src, and my manifest file is outside of src, and all of my .class files are in bin which is inside src. I created a MANIFEST.MF with the following contents:
Manifest-Version: 1.0
Main-Class: src/A
I then use the command jar cmf MANIFEST.MF A.jar src/bin/*.class src/GraphFile.txt src/ProblemA.txt src/ProblemB.txt (there are a few text files I need to include as well in my JAR file). Then, when I run java -jar A.jar ProblemA.txt GraphFile.txt (note that ProblemA.txt and GraphFile.txt are input files that my program accepts and uses), I get the following error:
Error: Could not find or load main class src.A
Caused by: java.lang.ClassNotFoundException: src.A
Can anyone explain what I am doing wrong or any steps I am missing? Unfortunately, I cannot post the actual code because this is for a class project, but I just need to get it compiled into a JAR file for submission! Thanks for any help.

"Could find or load main class" while running executable jar file in Java code

After converting my java program to executable jar file using commands in command prompt in windows 10,while executing jar file I am getting error:
Could find or load main class Combine.class" caused
by:java.lang.ClassNotFoundException:Combine.class
My jdk-11.0.1 has javamail api and excelapi.While executing I have set my classpath as:
classpath=%classpath%;C:\Program Files\Java\jdk-11.0.1\javamail_api\javax.mail-1.6.2.jar;C:\Program Files\Java\jdk-11.0.1\javamail_api\activation.jar;C:\Program Files\Java\jdk-11.0.1\jexcelapi\jxl.jar;.;
It was compiling and executing properly but after converting to executable jar file it is not running and giving above error.
Any help would be appreciated.
Thank you
The clue is in the exception message. It is trying to load a class with the name Combine.class. But the classes real name is Combine.
You have created the JAR file incorrectly.
echo Main-Class: Combine.class > manifest.txt
jar cmf manifest.txt FinalExecutable.jar Combine.class
If Combine is in the default package (i.e. it doesn't have a package statement) then the above should be:
echo Main-Class: Combine > manifest.txt
jar cmf manifest.txt FinalExecutable.jar Combine.class
If Combine is declared in package foo.bar, then the above should be.
echo Main-Class: foo.bar.Combine > manifest.txt
jar cmf manifest.txt FinalExecutable.jar foo/bar/Combine.class
and you need to be in the directory above the foo directory.
NB: the "Main-Class" attribute in the manifest must be a Java fully qualified class name, NOT a filename or file pathname.
It also should be noted that the CLASSPATH environment variable and the -cp argument will be ignored when you run a JAR using java -jar .... If your executable JAR depends on other JAR files, you should either combine them (to create a shaded JAR) or you should add a "Class-Path" attribute to the manifest; see https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
Finally, my advice would be to use a build tool (e.g. Maven) to compile your code, create the executable JAR file, etc rather than doing it by hand.

Can't figure out how to run my jar file "could not find or load main class"

I'm trying to create a jar file and run it using java -cp main.jar com.test.Foo.Main but I keep getting:
Error: Could not find or load main class com.test.Foo.Main
Caused by: java.lang.ClassNotFoundException: com.test.Foo.Main
This is my file structure. So I'm thinking the line in my Main.java should be package com.test.Foo correct?
I'm compiling my Main.java with javac Main.java which outputs a Main.class file. Afterward, I create a jar file using jar cfm main.jar META-INF/MANIFEST.MF Main.class and finally while I'm in the same directory as the jar file <root>/src/com/test/Foo/ I run java -cp main.jar com.test.Foo.Main and that's when I run into the above error. Any idea how I can run this file like this (and yes I need it to run with this command specifically)?
Main.java
package com.test.Foo;
public class Main {
public static void main (String args[]) {
System.out.println("I am com.test.Foo.Main");
}
}
META-INF/MANIFEST.MF
Manifest-Version: 1.0
Main-Class: com.test.Foo.Main
I tried using some of the options given in this popular SO question and nothing helped.
The picture you're showing in your question is your project structure not your jar structure.
When you create a jar file, the structure for that jar file might be
different with your source code folder structure.
Every IDE (such as eclipse, netbeans, IntelliJ) has a mechanism for creating JAR files. In your case when you open the created jar file (using zip apps like winrar) you should see something like this :
com
|
test
|
Foo
|
Main
META-INF
|
MANIFEST.MF
This should be the ordering of your files and folders, otherwise Java can not find your main class from MANIFEST.MF
Now to solve this problem:
Open your jar file using a zip application like winrar
check the folder structure residing inside your jar file as I draw
Fix it right away within the winrar or try to correct your project structure to produce the structure I mentioned.
The class is called com.test.Foo.Main you need to specify the full name in the command:
java -cp main.jar com.test.Foo.Main
or you can use the simpler
java -jar main.jar
Check your META-INF/MANIFEST.MF for the attribute of Manifest-Version: 1.0
This attribute must be there.
Edit:
You need to move to the source root src/ and issue below command to create a valid jar.
javac com/test/Foo/*.java
and, create the jar using,
jar cmf com/test/Foo/MANIFEST.MF main.jar com/test/Foo/*.class
The thing is, package structure should match with the folder structure apparently.

Creating a JAR File from the Command Line Doesn't Work

I've edited this question, and now it is exactly what I did:
I want to create a .jar file from some .class files in the command line.
I worked on Eclipse, and created:
myProject project, and in it: myPackage package, and in it: myClass class.
Then I wrote in the command line:
jar -cfv myJar.jar myPackage\myClass.class
And I got this:
added manifest
adding: myPackage/myClass.class(in = 745) (out= 473)(deflated 36%)
This really created the myJar.jar file in my current directory. Now, I wanted to check if the process was done successfully, so I extracted the class from the jar thus:
jar xfv myJar.jar
And I got this:
created: META-INF/
inflated: META-INF/MANIFEST.MF
inflated: myPackage/myClass.class
And this created for me just the META-INF folder, with the MANIFEST.MF in it, but I don't see any .class file here!!
It seems like something in the packing to jar process is incorrect.
Anybody has an idea??
Any answer is appreciated!
According to the output you gave, there is no myClass.class file in the directory where you execute
jar -cf myJar.jar myClass.class
So obviously, the command can't add it to the jar: it doesn't exist. If you want to add the myClass directory, recursively, to the jar file, then use
jar -cf myJar.jar myClass
EDIT:
Just look at the output:
inflated: myPackage/myClass.class
The myClass.class file is there in the jar file. There is no problem at all.

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