Java runnable jar from netbeans 6.9.1

I have a project in NetBeans6.9.1. It works great in the IDE. But when I try to start the jar, this NetBeans is automatically created in the dist directory, I get a NoClassDefFoundError for the classes inside my project. What am I doing wrong? Should I use Ant or something (I don’t know Ant) In eclipse I do "create runnable jar" and jar works without problems. Is there something equivalent in NetBeans?

UPDATE In dist / myJar, I removed the jar, and in the manifest there was no current path and root path of my project. I added them manually and re-created the jar from the command line. And it works. But why doesn't NetBeans add them to the jpath manifest pathpath. I do not understand

UPDATE 2 I found a problem. I think this is a serious NetBeans error. I did refactoring and changed the package names from myPackage.model to mypackage.model . But NetBeans did not do it right. This really changed the package name to mypackage, as shown in the tree navigator, but the package name inside the file remained as myPackage . The program was executed normally inside the IDE, and no errors were reported (although all classes were declared as belonging to my P article), and in the tree they were under my p ackage), but when I tried to run the jar inside the dist directory, I got a class exception which is not found. Today I noticed that the class was listed as my P ackage / model instead of my p ackage / model. I looked into the classes, and refactoring completely fixed everything. I manually changed the package name from within my classes from myPackage to mypackage and fixed all import ones (which imported myPackage). Is this a known NetBeans issue ????

thanks

+4
source share
4 answers

In NetBeans, your Java project will either be based on Maven or Ant. What you want to do is create a "fat" jar, which includes all its dependencies. For Maven, you can add the following lines to your pom so that:

<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>package.and.MainClass</mainClass> </manifest> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>1.2.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> </plugin> 

So, for Maven, this is a two-step thing:

  • You have to tell Maven to create a β€œshaded” (including dependency) jar,
  • and you must specify the main class (the one that contains the static main(..) method to run.

If you use Ant, you may need this blog post .

+6
source

I had the same span, and all I can say is that it seems to crash with NetBeans 6.9.1, as it worked in 6.8 and 6.9.

For convenience, you can open it in WinZip or WinRar and simply modify the manifest file from there without using it yourself. This is what I do.

+1
source

I decided after this post on an external blog :

You need to add two plugins ( maven-assembly-plugin and maven-jar-plugin ) to the build section of your pom.xml file. You will create a section similar to this:

 <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>(select version, ie: 2.3)</version> <executions> <execution> <goals> <goal>attached</goal> </goals> <phase>package</phase> <configuration> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>your.main.Class</mainClass> </manifest> </archive> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>(select version, ie: 2.3.2)</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>your.main.Class</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build> 
+1
source

NetBeans creates its own JAR file for your project when you run the Clean and Build command from a menu or tool button.

Ideally, the class path of the Jar file should not contain the project root folder, NetBeans adds the JARS library if you automatically added them to the project in the Class-Path property in the manifest.mf file.

If you have any user requirements, you can even modify the manifest.mf file, which is part of the built-in JAR. The manifest.mf file is available in the Files panel in the root directory of the project.

To make a JAR executable with the java -jar command, you must specify the main class in the project configuration. (If the project was created using the NetBeans project wizard, this class is already defined, otherwise we must do it manually)

Specify the main class through the menu option as follows:

 File > Project Properties > Run (Category) > Main Class (textbox) 

Please provide details of your project, for example

  • Is the project created from the Create New NetBeans Project Wizard?
  • Is the project based on Ant or Maven?
  • Is a free form project project created from an existing source?

This additional information will help me answer the question in specific details. Hope this helps.

considering tushar

0
source

All Articles