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
Related
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.
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.
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)
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
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