Producing executable jar in NetBeans

可紊 提交于 2019-11-27 04:37:39

I just had the same problem in NetBeans 7.2.1 with a Maven Java Application project. Modify the pom.xml file to include the maven assembly plugin with one tweak to myrho's answer (needs to reference the predefined descriptor "jar-with-dependencies"):

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.4</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <mainClass>your.app.MainClass</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

An alternate method is to build a Java Application project in NetBeans that doesn't use Maven. Select File -> Project Properties -> Build -> Packaging and check the "Copy Dependent Libraries" checkbox.

myrho

If you are using the maven assembly plugin and want to build an executable jar with dependencies, you need to add this portion to the configuration of the maven-assembly-plugin section in your pom.xml:

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <configuration>
    <!-- ... -->
    <archive>
      <manifest>
        <mainClass>your.app.SampleClass</mainClass>
      </manifest>
    </archive>
  </configuration>
</plugin>

Source: Maven Assembly Plugin Usage

kv.

Try this:

  1. Right click your project on the "Projects" panel, select "Properties"

  2. Click on "Run" in the new window.

  3. Edit the "Main Class:" field (click on Browse).

This way you will select the main class which is the entry point to your application and the Manifest will be created correctly.

Very simple, The .jar and .jad files are there in dist folder

I was also searching for answer and got this one from http://forums.netbeans.org/ptopic3465.html

Solved :)

If you clean and build your project it should create the jar in the "dist" directory.

It should create the manifest.mf at the top level of your project directory.

I actually came across this page after running into the same issue, I edited the manifest.mf file but the changes weren't showing up in the jar MANIFEST.MF file. I found the issue that I was having and thought I would pass along the information just in case it's relevant.

What I was doing wrong was I didn't have the project I was working on set as the main project, so while I was editing the the correct manifest, I was compiling the wrong project.

So I suppose the short of the story did you check and see if you have the correct project selected for the manifest.mf file you are editing?

Maybe you created Java Project with Existing Sources instead of Java Application?

I had similar problem and creating new Java Application and then manually copying src files solved the problem - everything worked "out of the box".

I had the experience, that the build process is different, depending on the project type.

I suppose, you've created "Java Class Library" project.

So just create a "Java Application" project, then merge all classes to it.

Then, Netbeans will not override, but enhance the "manifest.mf" into the JAR.

Of course, it's a stupid Netbeans bug. Of course it should be possible to add main classes afterwards.

EDIT: Please see my other answer. It's easier.

Strange indeed, it should do it out of the box including the classpath.

Did you upgrade from a previous version? When upgrading, NB will upgrade the project files but sometimes this migration is not done well and this kind of scenario's pop up. Just close the project, rename the nbproject dir to nbproject_old and do new project -> Java project with existing sources. Set the main class again and add dependencies and try again.

Another approach entirely is to develop, run unit tests, etc. in NetBeans but then use a maven or ant script to do your "real" build. It seems like these kinds of build tools give you more control over how things are built, and it also lets you automate builds.

For maven, you can add entries to the manifest easily using the maven jar plugin:

http://maven.apache.org/plugins/maven-jar-plugin/

A better solution:

Edit nbproject/project.properties and add the entry:

manifest.file=manifest.mf

manifest.mf should be in project's root and contain just:

Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build

Works. Tested.

Interesting information might be here:

http://wiki.netbeans.org/FaqNoMainClass

l_39217_l

Goto the Files view. Create a manifest.mf in the root directory.

/project_folder
    build.xml
    manifest.mf(edit here)
    /dist
    /src
    /test
    /build

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

To manually force package creation with NetBeans IDE 8.0.2 ...

  1. In Project navigator, expand your <project> and right click Project Files / pom.xml
  2. Click Run Maven / Goals
  3. Type "package" in Goals field
  4. Click OK

The output window will then show (on Mac) ...

Building jar: /Users/<username>/NetBeansProjects/<project>/target/<project>-1.0-SNAPSHOT.jar

Ok with neatbean you can do following steps below: 1. Right click name project choose item Set Configuration and Choose Customize
2. Choose Run and notice Main Class
3. Choose Browse and set class if you want to run. After you click ok.
4. Finish.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!