Java jar - no main manifest attribute - java

I'm aware that jar files should have a manifest.mf Main-Class: attribute in order to have an entry point and make the jar file runnable. Now, I have a jar file that I've built per below. The classes are all part of the burrito package. My MANIFEST.MF file looks like:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.9.1
Created-By: 1.7.0_51-b13 (Oracle Corporation)
Class-Path:
X-COMMENT: Main-Class will be added automatically by build
Main-Class: burrito.Main
, and when I try to build and execute via the following:
jar -cvf Burrito.jar Customer.class Main.class Server
.class Store.class MANIFEST.MF
*added manifest
adding: Customer.class(in = 2800) (out= 1424)(deflated 49%)
adding: Main.class(in = 1147) (out= 757)(deflated 34%)
adding: Server.class(in = 3954) (out= 2094)(deflated 47%)
adding: Store.class(in = 3950) (out= 2190)(deflated 44%)
adding: MANIFEST.MF(in = 203) (out= 158)(deflated 22%)*
I get:
Burrito.jar
*java -jar Burrito.jar
no main manifest attribute, in Burrito.jar*
I've tried various ways, also trying the -m switch (cvfm). I've attempted the following:
java -cp Burrito.jar burrito.Main
as well as
java -cp Burrito.jar Main
, which both tell me Error: Could not find or load main class
I've been poring over forums, and I can't seem to 'Google' my way to a solution here. Of course the Netbeans jar works, but I need to build my own. I've peeked at the Netbeans jar, and I see that I have two folders, burrito and META-INF. Of course, the manifest file is in the META-INF folder, and the burrito folder contains the class files. I'm not sure exactly to mimic this, and I would be happy to simply get this program to run.
Any pointers/hints?

You want something like this:
jar -cvfm Burrito.jar MANIFEST.MF burrito/Customer.class burrito/Main.class burrito/Server.class burrito/Store.class
The first argument after the options correspond to the f flag (the file to create), and the second argument corresponds to the m flag (the manifest file), and all the other arguments are the files to add to the JAR. You have to ensure the folder structure in the JAR matches the Java package, so if burrito.Main is the class, then you need burrito/Main.class in the JAR, and similarly for the other classes.

Related

Is it possible to have a jar Manifest to use all jars in a folder

I'm trying to set a jar manifest so it loads all the libraries (jars) within a folder next to the jar.
The manifest looks like this:
Manifest-Version: 1.0
Class-Path: libs/
Main-Class: path.to.my.class.Main
The layout is as follows:
- MyJar.jar
- libs/
-----lib1.jar
-----lib2.jar
And I'm trying to run like this:
java -jar MyJar.jar
And I get NoClassDefinition errors about classes in the jar within the folder.
In case someone is curious, this folder might contain optional jars that are processed during class loading. That' swhy I can't use the hardcoded or autogenerated ones.
Any idea?
Update
Rephrased the question as this is not currently possible from the manifest itself. The answer was the only really viable without the need of extracting the jars, although it also works.
So as a summary the answer is no, this can't be done from manifest file alone if you have unespecified dependencies.
You should define your Manifest classpath as
Class-Path: libs/lib1.jar libs/lib2.jar
See Oracle documentation for more details https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html
Try extracting these jars. It looks like you cannot add all jars from directory but you can add all classes. You lose obviously all configuration in manifest, however, if you are interested in jars' code content only, it might work.
I tested that with these simple classes
import pkg.B;
public class A {
public static void main(String[] args) {
System.out.println(B.class.getName());
}
}
package pkg;
public class B {}
now I try to separate the classes. I have jarred them into
$ jar tf libA.jar
META-INF/
META-INF/MANIFEST.MF
A.class
$ jar tf libB.jar
META-INF/
META-INF/MANIFEST.MF
pkg/B.class
no Class-Path in any manifest. I can run A with java -cp libB.jar:libA.jar A. Now I create another jar with Class-Path set to lib/
$ cat manifest
Class-Path: lib/
$ jar cfm empty.jar manifest
my directory tree look like
$ ls -R
.:
A.java empty.jar lib lib.jar manifest pkg
./lib:
libA.jar libB.jar
./pkg:
B.java
Now I try jar
$ java -jar empty.jar
Error: Could not find or load main class A
Hopeless, right? Then I extracted libA.jar and libB.jar into lib (same as [this guy][2]). Now all is fine
$ java -jar empty.jar
pkg.B
java does not know the jar files in the libs directory.
If you are using java 1.6+, You can run program as
java -cp lib/* -jar MyJar.jar

Creating Executable jar from CMD prompt

I have following class files,jar and manifest in a directory
Dot.class
Bridge.class
jsch.jar
MANIFEST.MD
Manifest file contains following two lines
Manifest-Version: 1.0
Main-Class:Dot
command used for creating jar file
jar cfm dot.jar MANIFEST.MD *
While executing generated jar it throws error saying no main manifest attribute
While seeing inside the generated jar META-INF folder contains auto generated manifest file, it doesn't have content for my main class.
I couldn't find successful steps ,Please correct me.
Had the same issue a few days ago and couldn't resolve it with the manifest file so I put the main class as a build parameter like this:
jar cfe Main.jar MainClass *.class
Add a space after ':' as in
Main-Class: Dot
Add a new line after the last line, in your case after Main-Class entry:
Manifest-Version: 1.0
Main-Class: Dot
The reason for 2. ist documented in https://docs.oracle.com/javase/tutorial/deployment/jar/modman.html in detail.
I tried below command and it works with the jar produced which i post.
java -cp "jsch.jar;." Dot

How exactly do I compile my Java into a JAR file?

So I'm trying to compile my game for my Object-Oriented Programming class into a jar file so it can be ran with java -jar javacoffeeadventure.jar.
My folder structure for a folder with java files removed looks like this:
audiomanager/
commandinterpreter/
gamemanager/
io/
logic/
META-INF/
player/
resources/
rooms/
main.class
Everything is packaged under javacoffeeadventure. For example, the main file is javacoffeeadventure.main. The META-INF folder contains one MANIFEST.MF file that I tried to edit and make the jar invoke main.class's main() method:
Manifest-Version: 1.0
Created-By: 1.8.0_60 (Oracle Corporation)
Main-Class: javacoffeeadventure.main
I know I use jar to compile into a jar file, but how would I use that command to create a jar file that is able to begin with javacoffeeadventure.main? Is my manifest wrong?
as a slight by-the-way, jar puns are funny to me if you guys have any. :)
In my experience, the following has worked, but if you utilize it, you may need to adapt your directory structure.
First, your folder structure needs to be of the form 'containing_folder.com.example.package', where your classes are in the 'package' folder. You need to then place your manifest.mf file in the uppermost directory ('folder'). The manifest file should be as follows:
Manifest-Version: 1.0
Main-Class: com/example/package/javacoffeeadventure
including a carriage return after the second line.
From the initial folder compile with the following command:
jar cmvf manifest.mf javacoffeeadventure.jar com/example/package/*.class
making sure that beforehand you've compiled the classes in your package (use *.java)
Hope this helps.

How should my Java JAR main class path look like?

So I have Test.jar. It's directories look like:
META-INF/MANIFEST.MF
Test/src/test/Test.java
/MainFrame.java
/MainPanel.java
/image.png
And my mainfest file looks like:
Manifest-Version: 1.0
Created-By: 1.7.0_13 (Oracle Corporation)
Main-Class: test.Test
When launching from command line (java -jar Test.jar) i get such error: could not find or load main class test.Test. How to solve it? I know it's problem with Main-Class line in manifest but I dont know how should path look like..
thats because your jar apparently contains java source files and not compiled java class files.
your jar layout should be
META-INF/MANIFEST.MF
/test/Test.class
/MainFrame.class
/MainPanel.class
/image.png
your manifest is fine. you should compile your source code files (*.java) to produce *.class files and package those into your jar.

Setting the Classpath in (Eclipse's) Jar File

I have a program.jar file which includes some external libraries. Additionally I wan't to start some classes with the program.jar.
The classes (TestKlass.class,...) are located in <path>/bin/data/test + sometimes there will be new classes added here.
How can I set this location for the classpath so I can use this command in the jar file:
cStart = Class.forName("data.test.TestKlass.class");
This is how my current MANIFEST.MF looks like:
Manifest-Version: 1.0
Rsrc-Class-Path: ./ junit-4.10.jar selenium-java-2.20.0.jar WinRegistr
y-4.4.jar selenium-server.jar
Class-Path: .
Rsrc-Main-Class: data.Testworks
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
(Everything works fine within eclipse)
You can't reference external jars classes in a runnable jar file. Everything must be contained inside the jar.

Categories