tags:

views:

1322

answers:

5

I've created a new J2SE project in NetBeans, and I can run it from the IDE, but when I try to run it using Ant on the command line, I get the following problem:

<snip>

run:
     [java] Exception in thread "main" java.lang.NoClassDefFoundError: IndexBuilder
     [java] Java Result: 1

<snip>

Based on the snippet from project.properties below, the class should be found.

run.classpath=\
    ${javac.classpath}:\
    ${build.classes.dir}

How do I go about fixing this?

A: 

Did you try setting the working directory to "build\classes" in the Project Properties -> Run tab?

Rogier
A: 

At least one of the JARs/Libs referenced by your project may not be being copied to the class path of your program. Copy all of the jars/libs that your project uses to the /dist folder of your project (or wherever YourApplication.jar is), then try to run your program. If this fixes it it means your Netbeans project isn't configured quite correctly.

instanceofTom
+1  A: 

The error you're getting means that one of the following is true:

  • The class IndexBuilder cannot be found on the classpath
  • A necessary (for class loading) dependency of IndexBuilder cannot be found on the classpath

That is, when loading the class, it's possible (even likely) that the class can be found but that some critical dependency of the class cannot be found. For example, if IndexBuilder extends another class and that base class cannot be found on the classpath, you'll get this error. Another example is if IndexBuilder uses a class in a static initializer and that class cannot be found.

Check your classpath not just for IndexBuilder but also for anything that IndexBuilder depends on.

See, for example, this discussion of NoClassDefFoundError.

Eddie
A: 

When you are running it from the command line, you are actually invoking Apache Ant. The reason you are getting the ClassNotFound Exception is because ${javac.classpath} and all the other properties are not being properly populated. That is why your code runs from within the Netbeans context. Netbeans is setting those properties for you.

To answer your original question of how do you go about getting it to run from the command line, you need to either set up a properties file that defines those parameters via a property declaration:

<property file="myproject.properties"/>

Another solution is to set the properties as environment variables via a sh script. Or you can use real paths in the build script instead of properties.

See here for more details on how to invoke Ant from the command line.

Steve Levine
A: 

Are you running this on Windows or Unix. If Windows, try changing your property file to:


run.classpath=${javac.classpath};${build.classes.dir}

Please note the semicolon instead of a colon.

Bogdan
This is on Mac OSX, so Unix.
Hank Gay
Can you post a snippet from your Ant build file?
Bogdan