Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java runnable jar from netbeans 6.9.1

I have a project in NetBeans6.9.1. It works fine from inside the IDE. But when I try to run the jar, that NetBeans has automatically created under the dist directory, I get a NoClassDefFoundError for classes inside my project. What am I doing wrong? Should I be using Ant or something (don't know Ant) In eclipse I do a "create runnable jar", and the jar runs without issues. Is there something equivalent in NetBeans?

UPDATE: In the dist/myJar, I extracted the jar, and in the manifest, the current path and the root path of my project were missing. I added them manually, and re-created the jar from command line. And it works. But why doesn't NetBeans add these in the classpath of the jar's manifest.I do not understand

UPDATE 2 I found the problem. I think this is a serious NetBeans bug. I had done refactoring and changed the package names from myPackage.model to mypackage.model. But NetBeans did not do it correctly. It indeed changed the name of the package to mypackage as seen in the tree navigator, but the package name inside the file remained as myPackage. The program executed fine inside the IDE and no errors were reported (although all the classes were declaring as belonging to myPackage and in the tree they were under mypackage), but when I tried to run the jar inside the dist directory I got a class not found exception. Today I noticed that the class was reported as myPackage/model instead of mypackage/model. I looked into the classes and the refactoring completely meshed up everything. I mannually changed the package name from inside my classes from myPackage to mypackage, and corrected all the imports (which were importing myPackage). Is this a known issue of NetBeans????

Thanks

like image 357
Cratylus Avatar asked Feb 26 '23 05:02

Cratylus


2 Answers

With NetBeans your Java project will be either Maven or Ant based. What you want to do is to create a "fat" jar that has all it's dependencies included. For Maven you can add the following lines to your pom to accomlish 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 it's a two-step thing:

  • You must tell Maven to produce a "shaded" (including dependencies) jar,
  • and you have to specify the main class (the one containing the static main(..)method to run.

If you're using Ant you might want have a look a this blog post.

like image 153
Waldheinz Avatar answered Feb 28 '23 18:02

Waldheinz


I've been having this same prolem, and all I can say is that it seems to be a glitch with NetBeans 6.9.1, as it worked in 6.8 and 6.9.

For convenience's sake, you can open it in WinZip or WinRar and just change the manifest file from there without having to jar it yourself. That is what I do.

like image 40
Ky. Avatar answered Feb 28 '23 18:02

Ky.