Failed to generate JAR file using Intellij - java

I have created a jar in path xxx/IdeaProjects/xxx/out/artifacts/abc_jar.
When I run it using java -jar, I get
Could not find or load main class ...
I have moved the mainfest file to xxx/IdeaProjects/xxx/src/main/resources/META-INF/MANIFEST.MF
and main class is com.rh.xxx.Application, but still getting
Could not find or load main class...

Set the Start-Class attribute value in MANIFEST.MF file with the fully qualified java class. Verify the same in the generated jar file after the jar is created.
Please refer below, here starter class is the one has main method.
Content of, META-INF/MANIFEST.MF
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.mycompany.project.MyApplication
Refer https://docs.spring.io/spring-boot/docs/current/reference/html/executable-jar.html#executable-jar-launcher-manifest for more info.

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.

No main manifest attribute, jar file, Applet

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)

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

Setting a path for the main-class in the manifest file

Say for example my manifest.txt file is saved in a parent directory of my bin directory where by classes are saved. Now, I want to be able to refer to my main-class in the manifest header so that I may create my JAR file from the parent directory directly.
So for example:
my manifest.txt is saved in the directory called project so:
-project(parent directory) contains:
bin(this contains my classes that are needed to run the application)
,src(folder)
and manifest.txt
So in a perfect world I would imagine my manifest.txt to look something like this:
Main-Class: bin/mainclass
but for some reason this doesn't work. Any ideas?
The Main-Class attribute must contain the fully qualified name of the main class. That has nothing to do with how your project is laid out. If the class is named Foo and is in package com.bar, the main class is com.bar.Foo.
If the class Foo is in the default package, which is a bad practice, then its fully qualified name is Foo. But the first thing you should do in that case is moving the class to a package.
When you execute java -jar path/to/myjar.jar, from whatever directory, java will inspect the jar, read the manifest file, look for the Main-Class, and load it from the jar.

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