No main manifest attribute, jar file, Applet - java

When trying to open my exported project (jar file) I get an error message
No main manifest attribute, in file.jar.
My project is basically a simple class extended by Applet, but the file won't open.
My java is up to date and I've tried using the line in cmd
java -jar file.jar
in order to open it, but it still doesn't work.
What is the reason that the main class isn't recognized?
Thanks for the help!

Create a manifest file like this (manifest.mf)
Main-class: start.HelloWorld
Class-Path: /pathToYourExternalJars/XXXXX.jar /pathToYourExternalJars/XXXXX.jar
Here 'Main-class' is the starting point of your application (simply class which contains main method ) & 'Class-path' is for external jars which are included in your application (Optional)

Related

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.

Cannot create executable jar from Java Swing app successfully

I have made a Java Swing application and now I want to export it as an executable jar file. I have created the app in Eclipse, and it has the following structure :
where folder mysqlconnector contains also a jar file. So first I tried following the instructions in this link and created seo.jar, but when I try to execute it from the terminal by java -jar seo.jar I get an error :
Error: Could not find file connectionprops.properties
As the screenshot shows, I tried putting connectionprops.properties in main package, but the same problem remains.
Then I tried making a manifest file named manifest.mf with contents :
Main-Class: bin.main.MainClass //also tried Main-Class: MainClass
as the structure of my project is :
seo --> bin --> main --> MainClass.class
I placed the manifest.mf in folder seo and I gave the following command in the terminal :
jar -cvfm seo.jar manifest.mf *
but again when executing it from the terminal by java -jar seo.jar I get an error :
Error: Could not find or load main class bin.main.MainClass
What am I doing wrong? Should I change something in my project structure? Is there a problem that I have other jar files inside my project? How can I create the executable jar and execute it successfully?
The Manifest Main-Class attribute needs to receive the package path to your Main class. It doesn't matter what the structure of the project looks like, what matters is the fully qualified package name that you have inside the src/ folder. So in this case, main.MainClass is what you should write there.
Also, if you receive other errors when trying to read the connectionprops.properties in your program, try to open the file using
someObject.getClass().getResourceAsStream("connectionprops.properties")
bin.main is not a package name. You can't add this to manifest.mf.
You should add the package name only if you have the main class in the package. If your package is default then put only the main class.
Main-Class: MainClass

Export project as jar file using manifest [duplicate]

This question already has an answer here:
Exporting executable/runnable jar in eclipse
(1 answer)
Closed 7 years ago.
I just wrote Java console project , which i shoud pack as jar file. I googled it alot and found different solutions like (Eclipse) Project -> New Configuration -> e.t.c Then export as jar file using speciefied config. That's nice, but it's not what i need. After making repeatedly - it won't work. I tried to do same as mentioned here: click
Second Solution: create jar using manifest. Well it's much better, because i can specifiy entry point of main.class. But it's won't work due to can;t find out where's annother packages.
Upd: Launcher.class looks like this
package backpack.dao;
public class Launcher{
public static void main(String[] args){
Backpack.start();
}
}
My project structure:
src/backpack/dao/Item.java
/Backpack.java
/Chromo.java
src/backpack/main/launcher/Launcher.java
The Question is: What should i write in manifest instead of this:
Main-Class src/backpack/main/launcher/Launcher to make executable jar
successfully?
P.S Launcher class uses instances from Backpack.java
Please don't downgrade. I'm rookie
Thanks
What is the package of the Launcher.java? Have you included package name to the classname in manifest.txt?
Remember, when Launcher.java is in package "backpack.main.launcher" your classname in manifest.txt will be:
Main-Class src/backpack.main.launcher.Launcher
instead of:
Main-Class src/backpack/main/launcher/Launcher
Also, remember about this:
The text file must end with a new line or carriage return.
To check what exactly is wrong with your jar file run it in the command line using:
java -jar MyJar.jar
and paste here output with error message.
For more help you can check this example from Oracle: link
Edit:
I recreated your project structure and I have found the solution (at least I think I have :) )
Compile your code and go to the parent directory with compiled code (with *.class files, this should be "out" directory or something like this - depending from your IDE). For example: when your project is in some/path/src/backpack/main/launcher go to src directory.
Run command:
jar cvfe MyJar.jar backpack.main.launcher.Launcher -C . .
(Yes, two dots at the end)
This should create jar with name MyJar.jar and manifest file (-e flag) with main class backpack.main.launcher.Launcher. -C flag include all *.class files recursively from directory . (current one), so both from dao and launcher package.
Test jar: java -jar MyJar.jar

Error while creating JAR file and executing it

I have a problem creating and executing a JAR file. I have already made a JAR file, but when I execute it with java -jar, I am getting an error Error: could not find and load main class ... I make a JAR file with jar cvfm, but I execute it from C:\Program Files\Java\jdk1.70\
What's wrong with this?
To create an executable jar file you have to specify the entry point to the jar.Like this:
jar -cvfe "jar file name" "Main Class Name(Ex com.test.MainTest)" "Files to be included in the jar"
If you already have a jar file, you can update the manifest file by creating an "additions" file and running the command to include the main class :
Main-Class: Classname
and run command:
jar ufm "jarfilename" "additions manifest"
Maybe an entry in your manifest is missing? You have to add your MainClass to the MANIFEST.MF - the entry needed is Main-Class: classname
For mor informations see here
Whenever we create the jar file , we pass the main-class parameter in Manifest.mf that is embed in the jar .
you have missed that part and now when you are executing its not able to identify the main class to execute from
http://www.skylit.com/javamethods/faqs/createjar.html might help

Error with an applet in an Executable Jar in eclipse

I want to create an executable jar in eclipse. I tried the option that said create executable jar but it didn't let me. Then I tried creating a normal jar file with a manifest like this:
Main-Class: Game.Game
Class-Path: AppletSource.jar
Here, the executable class is an applet (not JApplet) inside the package Game and a class called Game.class. It uses a package called AppletSource.jar. When I create it, it does not run. It says it failed to load the Main-Class manifest atribute. The AppletSource.jar is in there. Did I miss anything? and is the Main-Class wrote like that if it is inside a package?
PS: I added a main method to the Game class and now it gives me noclassdefounderror. It cant find the AppletSource.jar

Categories

Resources