Having problems with Manifest file when creating a JAR file in Netbeans - java

I want to create a jar file in Netbeans using the clean and build options. The jar file is created, but the manifest file is not updating; this means the main class is not set in the manifest file, so when I run the jar file, it has the "could not load main class" error.
How do I handle this and how do I add main class in project properties?

Right click on netbeans project and select "Properties"
Then select "Run". There is option for selecting main class. Select main class from there.Then click "ok".
After doing this try run your project at least once before building your jar file.

Hope You did not set up main class in project setup.
So do right click on project.
then go to properties.
On that screen you can see Run configuration.
There is place to configure main class of you jar file.
So configure your main class at there and press ok.
Then clean and rebuild the project.
After that manifest file should update.

May be you are running your project using shift+F6 key. You should run your project once by pressing F6 key only then the netbeans gives option to select main class then after that clean and build.

Related

Build Artifact with IntelliJ

I am trying to deploy a JAR into Docker.
The problem I am running into is building the JAR. (with IntelliJ)
I clicked File -> Project Structure then went to 'Artifacts' and clicked the 'Create Archive' button.
I provided a name for the JAR ("SimpleService.jar")
I clicked CREATE MANIFEST and then the following have been done
Manifest File location is selected
Main class is set to com.oneorangetree.Main
Class Path is "C:\Program Files\Java\jdk-17.0.1\lib*.jar"
This is the first time I am trying to deploy a Java application. I am not sure what #2 and #3 should be set to.

JAR files created by IntelliJ IDEA do not run

I wrote a program that spans a few classes in IntelliJ and it works fine when I was testing it in the IDE. However, whenever I follow tutorials to make my project into a .jar executable, it does not run.
The file in the out folder does not run when double-click on it and says "The Java JAR file "projectName.jar" could not be launched.
When opening it from the terminal, I get the error "Could not find or load main class". I used "$ java -jar projectName.jar "
I followed these steps:
Open Project Structure, Artifacts and click the (+).
Under JAR, from modules with dependencies.
Point to the class with my main method and click OK.
Click apply and OK to exit.
Then I close project structure and open Build and then Build Artifacts...
Then I click Build.
Navigate to ~/IdeaProjects/projectName/out/artifacts/projectName_jar/projectName.jar
Then I would try to run it but it does not run. My project has both a GUI and can be run from command lines. When I compiled the main method in Terminal, it worked fine, but I need a JAR file. I'm not sure what I should include in the question, since I'm sure that my code is unrelated. I also utilize Maven, not sure if that is related. Are there other things that I should be doing or adding to my project before I build the JAR?
This can be a result of the Manifest being created in the wrong place.
First make sure it is in src/main/resources/META-INF/MANIFEST.MF
(Intellij sometimes incorrectly places it in /src/main/java/META-INF/MANIFEST.MF)
Then Make sure the jar is looking for META-INF in the src dir NOT in /java OR /resources. This means:
When you set up File>Project Structure>Artifacts>+>JAR>From modules with dependencies... then your Directory for META-INF/MANIFEST.MF should end at src. Example:
ONLY C:\Users\User\StudioProjects\Project\src\
NOT THE PREPOPULATED C:\Users\User\StudioProjects\Project\src\main\java
If your Project Structure jar is already set up change the Manifest File path to:
C:\Users\User\StudioProjects\Project\src\META-INF\MANIFEST.MF
INSTEAD OF C:\Users\User\StudioProjects\Project\src\main\java\META-INF\MANIFEST.MF
You need to make sure that the Main-Class attribute is set. One way to check this is by opening the Jar with a Zip utility and looking for META-MF/MANIFEST.MF extract or open that file with a notepad, notepad++ or any document reader. If "Main-Class" is not in their or set to the correct main class you will received the error you have received. Seeing the IntelliJ tutorial it has a Main-Class field when setting up artifact publishing. I have included the link below to the IntelliJ page.
IntelliJ Creating an Artifact Tutorial

How to create jar from many java class using netbeans?

I have a java package with many java class and many main. i wish to make jar from these package ? can you help me please ?
(i work with netbeans )
If the project is under netbeans IDE, you should follow these steps:
Right click on the project and then click on Properties
Click on Packaging
Click on Compress JAR File
Accept changes, click OK
Right click on the project and then click on Clean and Build
Go to the directory where your project is stored
Look in the dist folder for the JAR file
To select the main class that will be executed
Click on Run
Click on Browse near to Main Class label
EDIT :
If you are using netbeans under windows, and if the jar file does not generate any output, try the following command in cmd :
start /b <jar file>
Then an error should appear
So you can know what is the problem
Right click on the the project in net beans project window and choose Project Properties -> run , there you can setup your main class
clean and build your project and go to your netbeans project -> your project -> dist there is the executable jar file.
selected main class will be executed when you execute the generated jar file
I found this site useful... Hope it helps
deploying jar

Beginner issues with Eclipse and running an imported .jar file?

I've never used Eclipse before am trying to import a .jar file and then run it without any command-line arguments.
I think the .jar file has been imported correctly, it is sitting in a created lib folder and I can see it in referenced libraries.
The .jar will not run because of the error "selection does not contain a main type".
I can see there is a main class inside the .jar when I extract it, but the manifest file does not include a 'main-class' specification. Because this is an assignment though, I cannot alter the .jar directory. Is there a way that I can run the file within Eclipse regardless of this?
Within the jar file all classes are part of a package. Would this alter the way I could try and run it?
To ensure, you imported the JAR correctly: In package explorer, right click your project, choose Properties, Java Build Path and tab Libraries. If your library doesn't appear, click button Add JARs or Add External JARs and choose your JAR file.
Then right click your project and choose Run As / Run Configurations.
In the dialog window click button New launch configuration. Then on the right side under Main class, input the full qualified class name of the main class which is contained in your JAR file. E.g. com.foo.Bar for class Bar in package com.foo. You can use the button Search to search for a main class.
Then click Apply and Run.
Write a class that will call the main class inside the jar.
Or simply ask your instructor.

Eclipse cannot create runnable jar - No resources selected

When I try to export my project as a runnable jar Eclise gives me the following error:
No resources selected.
The project runs fine within Eclipse, I already tried to clean the project before exporting, but that didnĀ“t work.
Any ideas on how to fix this.
The problem is most likely with your run configuration.
Go to your Class that contains the main(String[] args) method you wish to automatically run
when the jar is called from the command line
Right-click->Run As...->Application
Make sure it runs to your satisfaction
Now go through the export process as before, selecting the newly created launch configuration.
The last step is key.
I found another solution.
I found that there's a bug in JDK 9.
I solved it by downgrade to JDK 8(1.8.1)
I fixed the problem, I exported the project as an archive file. Then I opened up a new workspace and imported the the archived project. After that I tried to export the project as a runnable jar, after two tries the jar runs as is it should.
I still don't know what the exact cause was but I hope someone how has run into the same problem finds this helpful.
Doing a (project->clean) and creating a new run configuration (with correct project and class name with main() method, selected) solved the problem for me.You just have to select the new run configuration while exporting runnable jar :)
Try this, this is the way to create a jar or runnable jar in eclipse, all your external libraries in the project will be included
File -> Export-> Java ->Runnbale JAR file
Launch configuration : your Class containing the public static void main(String[] args)
Export destination : Target place
Library Handling:
Package required libraries into generated JAR
FINISH
When you click this option in Eclipse you should get a popup that lets you choose what files to export, you need to select the src folder.
I was having this same problem, and I was able to resolve it. Maybe you should cross verify the main class, that you selected in this project. It is possible that some other project's main class may have the same name as the main class you selected. Make sure the main class you selected is the one from the project that you selected.
The problem is your package. You probably renamed it, and in the export option, the program was confused, and it was using the first name of your package.
This is basically happen when you change package name under which you made your program. This will create confusion regarding package, as per program export, main method still written in old package.
To resolve it, Just copy it and paste it in same package with different name and then try to export it. Check for log too.
I was in the same situation. For me deleting existing eclipse installation(not not the project workspace) and opening the existing eclipse workspace with new installation of eclipse was the only thing that worked.
Just copy the project as a new one. Configure the build path by creating new Run/Debug Settings.this would resolve the problem.
I continued to get the "no resources selected" error. I then did the procedure I am quoting and it gave me a MANIFEST file. Then I was able to create an executable JAR.
Quoting: "I exported the project as an archive file. Then I opened up a new workspace and imported the the archived project. After that I tried to export the project as a runnable jar, after two tries the jar runs as is it should."
I also faced the same issue, in my case, I just closed the project and reopened it, now it's wokring
Check that your project is correct at the time when you select the main class.
If the class non exist, it's the standard error of Eclipse.

Categories

Resources