views:

201

answers:

3

Hi,

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

+1  A: 

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.

Waldheinz
@Waldheinz: So with NetBeans I MUST have working knowledge of Ant or Maven??? Because I have no knowledge of neither. And why does the classpath in the NetBean's Manifest omit the current directory and the project's root file?
@user384706 Actually I think the Netbeans Maven support is very very good, but indeed some knowledge about it really helps. I think putting the current directory into a .jar Manifest is very fragile and so it's better just not to do it. And if with "project's root file" you mean the class "main" class -- there can be more than one, and instead of guessing wrong NB does not guess at all. The pros and cons of this are debatable, but that's what it's like. :-)
Waldheinz
+1  A: 

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.

Supuhstar
@Supuhstar: I open with WinZip.Open Manifest, and open with text-editor. Change manifest file.I close and get the option to replace with updated archive.I press yes but the new archive has kept the old manifest file under META-INF and has placed my new manifest outside META-INF.Have you encountered this?
No, I haven't... That's quite odd. Are you doing Save or Save As? So long as you just open the manifest, edit it (preferably in Notepad), and save it, it should work.
Supuhstar
A: 

NetBeans creates a self sufficient JAR file for your project when you execute the "Clean and Build" command from Menu or Tool Button.

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

If you have any custom requirement you can even modify the manifest.mf file which gets included in the built JAR. The manifest.mf file is available from the Files panel in the project root directory.

For making the JAR executable with the java -jar command you shall specify the Main class in the project configuration. (If the project was created through NetBeans project wizard then this class is already defined, otherwise we have to do that manually)

Specify the main class through Menu option as follows:

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

Please give details of your project like

  1. Is the project created from NetBeans New Project wizard?
  2. Is the project Ant based or Maven based?
  3. Is the project free form project created from existing source?

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

with regards
Tushar

Tushar Joshi
@Tushar: The project was created from wizard.I have not used Ant nor Maven, unless there is some config parameter that I do not know. It is a swing application. The main class is defined in Manifest. The structure is src/myPackage1/model, src/myPackage1/controller etc. I get no class found error for class in myPackage1.model
If the project is created from wizard it is by default based on Ant and the main class of the project is automatically configured as the Main Class in Run configuration. Do you have specific library structure? Do you have a lib folder separately defined for library JARs?
Tushar Joshi
@Tushar:All my classes belong to packages.I use extra jars, which NetBeans has placed them in a directory /libs.The "class not found exception" is not about these additional jars under the lib directory, but about a class in my packages. e.g. the file structure is MyProject/src/ is my source directory. Under this: ui_swing/models, ui_swing/controller etc. for the coresponding packages. I get a class not found for the ui_swing.models.myModel class under MyProject/src/ui_swing/models. I do not understand what is wrong. It should be able to find the class with the default manifest. Right?
About your UPDATE2: If you can reproduce this bad refactoring behavior then this can be filed as a bug in NetBeans issuezilla.
Tushar Joshi