java.lang.NoClassDefFoundError while starting tomc

2019-04-26 14:43发布

问题:

My goal is to start tomcat using Ant. Here is my script:

<target name="tomcat-start">
    <java jar="${tomcat.home}/bin/bootstrap.jar" fork="true" dir="${tomcat.home}">
        <classpath>
            <fileset dir="${tomcat.home}/bin">
                <include name="bootstrap.jar"/>
                <include name="tomcat-juli.jar"/>
            </fileset>
        </classpath>

        <jvmarg value="-Dcatalina.home=${tomcat.home}"/>

    </java>
</target>

After script execution I receive this output:

java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory
at org.apache.catalina.startup.Bootstrap.<clinit>(Bootstrap.java:60)
Caused by: java.lang.ClassNotFoundException: org.apache.juli.logging.LogFactory
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 1 more
Exception in thread "main" 
Java Result: 1

I've checked: org.apache.juli.logging.LogFactory class is presented in tomcat-juli.jar!

What could be wrong?

回答1:

For some unknown weird reasons tomcat doesn't start in my system even when I launch it from command line using java -jar command.

However, I managed to start it using java -cp "bin\bootstrap.jar;bin\tomcat-juli.jar" org.apache.catalina.startup.Bootstrap command, executed from tomcat.home directory.

The code in ant which does the same:

<exec executable="java" dir="${tomcat.home}">
    <arg line="-cp bin\bootstrap.jar;bin\tomcat-juli.jar"/>
    <arg value="org.apache.catalina.startup.Bootstrap"/>
</exec>


回答2:

If you want to stick with using catalina, the proper method is to add \setenv.bat with contents like:

set CLASSPATH=D:\tomcat\apache-tomcat-7.0.42\bin\bootstrap.jar;D:\tomcat\apache-tomcat-7.0.42\bin\tomcat-juli.jar

I discovered that by reading catalina.bat. Someone might want to update this with the proper Tomcat doc



回答3:

Check out the startup.sh batch file or shell script of tomcat. It has bunch of other files apart from bootstrap.jar. check setclasspath.sh. It has all jars are being set in class path.

Other more clean approach would be to invoke startup.sh script from ant. This will do everything for what it takes to start Tomcat Server.



标签: java tomcat ant