Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NoClassDefFoundError ANT build

I am getting an error regarding the classpath in the run stage. The error is

run:
    [java] java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
    [java]  at java.lang.Class.getDeclaredMethods0(Native Method)
    [java]  at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
    [java]  at java.lang.Class.privateGetMethodRecursive(Unknown Source)
    [java]  at java.lang.Class.getMethod0(Unknown Source)
    [java]  at java.lang.Class.getMethod(Unknown Source)
    [java]  at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
    [java]  at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
    [java] Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.WebDriver
    [java]  at java.net.URLClassLoader.findClass(Unknown Source)
    [java]  at java.lang.ClassLoader.loadClass(Unknown Source)
    [java]  at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    [java]  at java.lang.ClassLoader.loadClass(Unknown Source)
    [java]  ... 7 more
    [java] Error: A JNI error has occurred, please check your installation and try again
    [java] Exception in thread "main" 
    [java] Java Result: 1
BUILD SUCCESSFUL
Total time: 1 second

This is the xml build code I have written. I am referencing the RunningPower jars folder and I believe the correct folder is being referenced as i able am able to compile. My base directory also contains a .classpath and .project file but not sure if they are important.

 <?xml version="1.0" ?>

<project name="SeleniumProjectDataDriven" basedir="." default="run">
    <target name="init">
        <property name="src.dir" value="src" />
        <property name="build.dir" value="build" />
        <property name="classes.dir" value="${build.dir}/class" />
        <property name="lib.dir" value="RunningPowerJars" />
    </target>

    <target name="clean" depends="init">
         <delete dir="build"/>
     </target>

    <target name="compile" description="Compiles the code" depends="clean" >
        <mkdir dir="${classes.dir}" />
        <javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false">
            <classpath>
                <fileset dir="${lib.dir}">
                    <include name="**/*.jar" />
                </fileset>
            </classpath>
        </javac>
    </target>

    <target name="jar" description="Packages the code into jar" depends="compile">
        <mkdir dir="build/jar"/>
            <jar destfile="build/jar/RunningPower.jar" basedir="build/class">
                <manifest>
                    <attribute name="Main-Class" value="RunningPower"/>
                </manifest> 
            </jar> 
    </target>

    <target name="run" description="Run the jar file" depends="jar" >
        <java jar="build/jar/RunningPower.jar" fork="true"/>
    </target>
</project>

In my RunningPowerJars folder, they contain

junit-4.8.1.jar
ojdbc6.jar
poi-3.7-20101029.jar
selenium-java-2.46.0.jar
selenium-server-standalone-2.46.0.jar
testng-6.1.1.jar

Update (7:32 AM PST 8/21/2015)

<target name="run" description="Run the jar file" depends="jar" >
    <java jar="build/jar/RunningPower.jar" fork="true">
        <classpath>
            <fileset dir="${lib.dir}">
            <include name="**/*.jar" /> 
            </fileset> 
        </classpath>
    </java>
</target>

I modified the code but ran into another error.

BUILD FAILED
C:\Users\dt208672\Perforce\depot\ebill\Automation\Selenium_eBill\RunningPower\build.xml:37: Problem: failed to create task or type classpath
Cause: The name is undefined.
Action: Check the spelling.
Action: Check that any custom tasks/types have been declared.
Action: Check that any <presetdef>/<macrodef> declarations have taken place.
like image 702
LinhSaysHi Avatar asked Aug 20 '15 16:08

LinhSaysHi


1 Answers

The classpath is needed also when running the Jar file so that the JVM could find library classes. However, when using the jar attribute in the java task, "all classpath settings are ignored" (see https://ant.apache.org/manual/Tasks/java.html). The simplest way is to specify the main class and add the compiled Jar along with the library Jars located in the RunningPowerJars directory:

<target name="run" description="Run the jar file" depends="jar" >
    <java classname="RunningPower" fork="true">
       <classpath>
            <pathelement location="build/jar/RunningPower.jar"/>
            <fileset dir="${lib.dir}">
                <include name="**/*.jar" />
            </fileset>
        </classpath>
   </java>
</target>

An unrelated note is that you could use the properties defined in init throughout the buildfile. For example, in the jar target you can use ${classes.dir} instead of repeating build/class.

It still doesn't make sense why the build would fail if the classpath element is added inside the java task when using the jar attribute. Although it is ignored, it is weird that it is failing with the "failed to create task or type" error.

like image 117
M A Avatar answered Sep 21 '22 18:09

M A