Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup Main class in manifest file in jar produced by NetBeans project

Tags:

I have the following problem. I have a Java project in my NetBeans IDE 6.8. When I compile it and it produces a .jar file containing everything possible, the META-INF is not right. It doesn't contain the class to be executed - with main() method.

When I click the Run button inside the IDE, everything works. The settings of the project are also set the right way - pointing to a class in my project.

I tried adding a folder META-INF with manifest file but I didn't manage.

Is there a way to do this manually in NetBeans, because I found that if I add the missing Main class in the manifest, everything works.

(I suppose I hit some sort of bug...)

//edit: The result I'm after is that I want the jar that is created with the build of NetBeans to be executable with command:

Quote from Sun Documentation :

When the Main-Class is set in the manifest file, you can run the application from the command line: java -jar app.jar

like image 393
Leni Kirilov Avatar asked May 17 '10 11:05

Leni Kirilov


People also ask

How do you set the main class in a running jar?

We'll use the -cp option (short for classpath) to specify the JAR file that contains the class file we want to execute: java -cp jar-file-name main-class-name [args …] As we can see, in this case, we'll have to include the main class name in the command line, followed by arguments.

How do I specify JAR manifest?

You use the m command-line option to add custom information to the manifest during creation of a JAR file. This section describes the m option. The Jar tool automatically puts a default manifest with the pathname META-INF/MANIFEST. MF into any JAR file you create.

How do I edit a manifest file in Netbeans?

I clicked over to the "Files" tab, double-clicked on "manifest. mf" in the "build" folder, and edited the file to include this.


1 Answers

I'm going to make a summary of the proposed solutions and the one that helped me!

After reading this bug report: bug in the way NetBeans 6.8 creates the jar for a Java Library Project.

  1. Create a manifest.mf file in my project root

  2. Edit manifest.mf. Mine looked something like this:

    Manifest-Version: 1.0 Ant-Version: Apache Ant 1.7.1 Created-By: 16.3-b01 (Sun Microsystems Inc.) Main-Class: com.example.MainClass Class-Path: lib/lib1.jar lib/lib2.jar 
  3. Open file /nbproject/project.properties

  4. Add line

    manifest.file=manifest.mf

  5. Clean + Build of project

Now the .jar is successfully build.

Thank you very much vkraemer

like image 53
Leni Kirilov Avatar answered Oct 03 '22 05:10

Leni Kirilov