You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ant.apache.org by Jon McLennan <jm...@clear2pay.com> on 2005/09/22 11:37:51 UTC

NoClassDefFoundError when loading class from -lib

I've taken the source for org.apache.tools.ant.Launcher and modified it 
to call my own version of Main.java. Essentially, the only change I made 
with that class is to allow the user to specify which startup class is 
to be run. (The default is the standard org.apache.tools.ant.Main.) My 
version of Main can create menus from targets in the build file (plus 
some other features). I use log4j for logging debugging messages.

The problem I'm running into is when I call this line:

    Class mainClass = Class.forName(classmain, false, loader); //
    classmain = my Main.java

I get this error:

    java.lang.NoClassDefFoundError: org/apache/log4j/Category
            at java.lang.Class.newInstance0(Native Method)
            at java.lang.Class.newInstance(Class.java:232)
            at com.clear2pay.releng.Launcher.run(Launcher.java:351)
            at com.clear2pay.releng.Launcher.main(Launcher.java:75)

I'm running Ant 1.6.1 (installed at D:\java\ant-1.6.1) and the external 
lib directory (D:\java\lib) is passed to Ant via the -lib command line 
arg. I have log4j-1.2.6.jar in that directory.

This is the command line I use:

    java !ANT_OPTS! -classpath  
        "d:\java\lib\releng.jar;
        d:\java\apache-ant-1.6.1\lib\ant.jar;
        d:\java\apache-ant-1.6.1\lib\ant-launcher.jar;
        d:\java\apache-ant-1.6.1\lib\xml-apis.jar;
        d:\bea\jdk131\jre\lib\rt.jar;
        d:\bea\jdk131\lib\dt.jar;
        d:\bea\jdk131\lib\tools.jar;
        d:\bea\jdk131\jre\lib\i18n.jar;" com.clear2pay.releng.Launcher
    -lib "D:\java\lib"
    -mc com.clear2pay.releng.Main


I put some debugging statements in com.clear2pay.releng.Launcher to see 
if org.apache.log4j.Category has been loaded by the ClassLoader

    Class myClass = loader.loadClass("org.apache.log4j.Category");
    System.out.println("myClass = " + myClass);

which outputs:

    myClass = class org.apache.log4j.Category

I even tested it by listing the methods available from that class.

So what I don't understand is, why is com.clear2pay.releng.Launcher able 
to find org.apache.log4j.Category when I get it from the loader, but it 
can't be found when I load a class that imports it? I can get this to 
work if I put all external jars I need directly in the Classpath, but 
this can cause problems on Windows if the value of Classpath gets too 
long and I'd like to avoid that.

Jon