java - How to create executable .jar file with netbeans

23
2014-04
  • landscape

    I'd like to make "double-click" cli application but still don't get how. I know I should propably somehow edit manifest but that is all. I googled ofc. but no success. Thanks for any tips. Here is the output from build, run, and manifest:

    compile:
    Created dir: /home/nick/NetBeansProjects/SemestralWork/dist
    Building jar: /home/nick/NetBeansProjects/SemestralWork/dist/SemestralWork.jar
    Not copying the libraries.
    To run this application from the command line without Ant, try:
    java -jar "/home/nick/NetBeansProjects/SemestralWork/dist/SemestralWork.jar"
    jar:
    BUILD SUCCESSFUL (total time: 1 second)
    
    
    java -jar /home/nick/NetBeansProjects/SemestralWork/dist/SemestralWork.jar
    
    Exception in thread "main" java.lang.NoClassDefFoundError: semestralwork/Main
    Caused by: java.lang.ClassNotFoundException: semestralwork.Main
        at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:319)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:264)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:332)
    Could not find the main class: semestralwork.Main. Program will exit.
    
    MY MANIFEST created with build:
    
    Manifest-Version: 1.0
    
    Ant-Version: Apache Ant 1.7.1
    
    Created-By: 14.0-b08 (Sun Microsystems Inc.)
    
    Main-Class: semestralwork.Main
    
    Class-Path: 
    
    X-COMMENT: Main-Class will be added automatically by build
    
  • Answers
  • bguiz

    These two lines tell you all you need to know:

    Exception in thread "main" java.lang.NoClassDefFoundError: semestralwork/Main
    Caused by: java.lang.ClassNotFoundException: semestralwork.Main
    

    And a further clue is dropped by the manifest output:

    Main-Class: semestralwork.Main
    

    This means that the JAR file is looking for a package named semestralwork and a class named Main inside it. It fails at this point because it cannot find either the semestralwork package or the Main class.

    As you pointed out in your question, the problem is indeed in the manifest file. You can edit this directly in your JAR file if you like, but a better idea would be to do this from Netbeans:

    • Click on `File --> Project Properties (semestralwork)'
    • In the dialog that opens, on the tree on the left select Run
    • Then, on the right, under the field labeled Main class:, enter the fully qualified class name of the class that you want executed when run from the command line.

    In your case, as I see from your comment on @Aaron's answer, if your main class is in a file called encryption.java, and it is in the default package (no package), just enter encryption.

    Once this is done, do a clean and build, then try running it from the command line again.

    HTH

  • Toon Krijthe

    Since I encountered the same problem, I can clarify the solution a little bit.

    You must create main Java class outside your method (for example-default_package folder), and then invoke your method(folder), e.g import your_folder.connected_class; in that main class.

    Hopefully I could help someone with the same problem.

  • user49740

    It's easier to make a .exe from a .jar without netbeans. Here are my suggestions: 1. Use a special application for this(ex: JSmooth, JEXECreator etc) 2. Make a C++ program that starts a JVM (see this tutorial)

  • whatnick

    The default class search path may be the issue. You should try changing directory to the location the jar is and launching with java -jar Semestral.jar. Also you may have misnamed the main class. Please also include your package structure.

  • Aaron Digulla

    Open the JAR file with a ZIP tool (or try less ... if you're on Linux or jar tvf ...). Make sure there is a directory semestralwork in there which contains a file Main.class.

  • user698816

    It's Easy. Download a copy of netbeans. Make a new project in netbeans. Goto your main class in the Projects Explorer. This should be folder "YourProject" As the class YOURPROJECT.JAVA . It is this yourproject.java file that you want to start with. Just write your code into the public static void main area. You can run the program with the green play button in the top toolbar.


  • Related Question

    java - Producing executable jar in NetBeans
  • Dani

    I'm using NetBeans 6.5 and for some reason it won't produce executable jar "out of the box".

    I set my project to be the main project, defined main class in the project properties "run" menu and it works flawlessly when I press F6 to run it.

    I looked at the manifest file and it indeed didn't define the main class there, and also omitted the library dependencies.



    Am I missing something? Is there a way (other than manually altering the manifest file) to produce executable jar files?

    EDIT: yes, I tried clean and rebuild and it produced the jar in the dist folder, still with the same manifest.mf

    Manifest-Version: 1.0
    Ant-Version: Apache Ant 1.7.1
    Created-By: 10.0-b23 (Sun Microsystems Inc.)
    

  • Related Answers
  • LifeH2O

    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 :)

  • 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

  • John Ellinwood
  • Jason Plank

    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.

  • rarchibald

    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.

  • marktucks

    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.

  • ivan_ivanovich_ivanoff

    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?

  • Rogier

    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.

  • James Kingsbery

    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/

  • ivan_ivanovich_ivanoff

    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.

  • java.is.for.desktop

    Interesting information might be here:

    http://wiki.netbeans.org/FaqNoMainClass

  • Alexis Pigeon

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

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

    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.