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

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

This question is related to java netbeans manifest

The answer is


This is a problem still as of 7.2.1 . Create a library cause you do not know what it will do if you make it an application & you are screwed.

Did find how to fix this though. Edit nbproject/project.properties, change the following line to false as shown:

mkdist.disabled=false

After this you can change the main class in properties and it will be reflected in manifest.


The real problem is how Netbeans JARs its projects. The "Class-Path:" in the Manifest file is unnecessary when actually publishing your program for others to use. If you have an external Library added in Netbeans it acts as a package. I suggest you use a program like WINRAR to view the files within the jar and add your libraries as packages directly into the jar file.

How the inside of the jar file should look:

MyProject.jar

    Manifest.MF
         Main-Class: mainClassFolder.Mainclass

    mainClassFolder
         Mainclass.class

    packageFolder
         IamUselessWithoutMain.class

It is simple.

  • Right click on the project
  • Go to Properties
  • Go to Run in Categories tree
  • Set the Main Class in the right side panel.
  • Build the project

Thats it. Hope this helps.


Don't hesitate but look into your project files after you have built your project for the first time. Look for a manifest file and choose open with notepad.

Add the line:

Main-Class: package.myMainClassName

Where package is your package and myClassName is the class with the main(String[] args) method.


Brother you don't need to set class path just follow these simple steps (I use Apache NetBeans)

Steps:

  1. extract the jar file which you want to add in your project.

  2. only copy those packages (folder) which you need in the project. (do not copy manifest file)

  3. open the main project jar file(dist/file.jar) with WinRAR.

  4. paste that folder or package in the main project jar file.

  5. Those packages work 100% in your project.

warning: Do not make any changes in the manifest file.

Another method:

  • In my case lib folder present outside the dist(main jar file) folder.
  • we need to move lib folder in dist folder.then we set class path from manifest.mf file of main jar file.

    Edit the manifest.mf And ADD this type of line

  • Class-Path: lib\foldername\jarfilename.jar lib\foldername\jarfilename.jar

Warning: lib folder must be inside the dist folder otherwise jar file do not access your lib folder jar files


In 7.3 just enable Properties/Build/Package/Copy Dependent Libraries and main class will be added to manifest when building depending on selected target.


Adding manifest.file=manifest.mf into project.properties and creating manifest.mf file in the project directory works fine in NB 6.9 and should work also in NB 6.8.


I read and read and read to figure out why I was getting a class not found error, it turns out the manifest.mf had an error in the line:

Main-Class: com.example.MainClass

I fixed the error by going to Project Properties dialog (right-click Project Files), then Run and Main Class and corrected whatever Netbeans decided to put here. Netbean inserted the project name instead of the class name. No idea why. Probably inebriated on muratina...


It looks like you are running into a bug in the way NetBeans 6.8 creates the jar for a Java Library Project.

The issue implies that there is a work-around.

I have not been able to verify that with NB 6.8 and/or NetBeans 6.9-dev...

You may want to register with the NetBeans.org website/issue tracker and update the issue and add your 'vote'.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to netbeans

Can't create project on Netbeans 8.2 I'm getting favicon.ico error Cannot find java. Please use the --jdkhome switch Netbeans 8.0.2 The module has not been deployed Error starting Tomcat from NetBeans - '127.0.0.1*' is not recognized as an internal or external command Cannot start GlassFish 4.1 from within Netbeans 8.0.1 Service area javac: invalid target release: 1.8 connecting MySQL server to NetBeans Starting of Tomcat failed from Netbeans Display Records From MySQL Database using JTable in Java

Examples related to manifest

Maven Error: Could not find or load main class Apply a theme to an activity in Android? Reference jars inside a jar Can't execute jar- file: "no main manifest attribute" How do I create/edit a Manifest file? How to setup Main class in manifest file in jar produced by NetBeans project Why has it failed to load main-class manifest attribute from a JAR file? What does MissingManifestResourceException mean and how to fix it? Command line tool to dump Windows DLL version?