找不到Ant的主类错误

时间:2012-04-30 20:57:22

标签: java ant build noclassdeffounderror build-script

这是我第一次使用Ant,并且在尝试运行时遇到java.lang.NoClassDefFoundError异常。这是我的Ant构建脚本,

<project name="LearnKirtan" basedir="." default="main">

    <property name="src.dir" value="src" />
    <property name="lib.dir" value="lib" />

    <property name="build.dir" value="build" />
    <property name="classes.dir" value="${build.dir}/classes" />
    <property name="jar.dir" value="${build.dir}/jar" />

    <path id="classpath">
        <fileset dir="${lib.dir}" includes="**/*.jar" />
    </path>

    <property name="main-class" value="gsingh.learnkirtan.Main" />

    <target name="clean">
        <delete dir="${build.dir}" />
    </target>

    <target name="compile">
        <mkdir dir="${classes.dir}" />
        <javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false" classpathref="classpath" />
    </target>

    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}" />
        <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
            <manifest>
                <attribute name="Main-Class" value="${main-class}" />
            </manifest>
        </jar>
    </target>

    <target name="run" depends="jar">
        <java classname="${main-class}" fork="true" />
        <classpath>
            <path refid="classpath" />
            <path location="${jar.dir}/${ant.project.name}.jar" />
        </classpath>
    </target>

    <target name="clean-build" depends="clean,jar" />

    <target name="main" depends="clean,run" />

</project>

构建没有错误。运行目标给出了异常。我已经检查了jar文件中生成的清单,它看起来像这样,

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.6.0_30-b12 (Sun Microsystems Inc.)
Main-Class: gsingh.learnkirtan.Main

4 个答案:

答案 0 :(得分:4)

您的运行目标看起来有点不稳定,<java classname="${main-class}" fork="true" />是自动关闭的,因此永远不会使用classpath元素

<target name="run" depends="jar">
    <java classname="${main-class}" fork="true" >
        <classpath>
            <path refid="classpath" />
            <path location="${jar.dir}/${ant.project.name}.jar" />
        </classpath>
    </java>
</target>

可能适合你。

答案 1 :(得分:1)

问题是您没有正确指定java任务的类路径。

试试这个:

<java classname="${main-class}" fork="true">
    <classpath>
        <path refid="classpath" />
        <path location="${jar.dir}/${ant.project.name}.jar" />
    </classpath>
</java>

答案 2 :(得分:0)

我有可能的问题。我的脚本如下:

<java classname="SchedulerManager" fork="true">
          <jvmarg value="-Ddb.username=${db.user}"/>
          <jvmarg value="-Ddb.password=${db.password}"/>
          <jvmarg value="-Ddb.url=${db.url}"/>

          <classpath>
             <pathelement location="${tomcat.home}/webapps/${project.war}-admin/WEB-INF/lib/${jar_name}"/>
             <pathelement path="com.${company}.${project}.scheduler.SchedulerManager"/>
          </classpath>
      </java>

当我跑步时,我收到以下错误:

 [java] Exception in thread "main" java.lang.NoClassDefFoundError: com/<company>/<project>/scheduler/SchedulerManager
 [java] Caused by: java.lang.ClassNotFoundException: com.<company>.<project>.scheduler.SchedulerManager
 [java]     at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
 [java]     at java.security.AccessController.doPrivileged(Native Method)
 [java]     at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
 [java]     at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
 [java]     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
 [java]     at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
 [java] Could not find the main class: com.<company>.<project>.scheduler.SchedulerManager.  Program will exit.
 [java] Java Result: 1

但是,如果我CD到文件夹“${tomcat.home}/webapps/${project.war}-admin/WEB-INF/lib/”并运行:

java -cp "*" -Ddb.username=${db.user} -Ddb.password=${db.password} -Ddb.url=${db.url} com.<company>.<project>.scheduler.SchedulerManager (of course I gave real parameters)

有效!

答案 3 :(得分:0)

最后决定已经找到了!我们用以下方式重写了脚本:

 <path id="master-classpath">
    <fileset dir="${tomcat.home}/webapps/${project.war}-admin/WEB-INF/lib">
       <include name="*.jar"/>
    </fileset>

    <fileset dir="${workspace.path}/artifacts">
       <include name="${jar_name}"/>
    </fileset>

    <fileset dir="${workspace.path}/deploy_scripts/libs/app">
       <include name="${jdbc_jar_name}"/>
    </fileset>
  </path>
  <java classname="com.${company}.${project}.scheduler.SchedulerManager" fork="true">
      <jvmarg value="-Ddb.username=${db.user}"/>
      <jvmarg value="-Ddb.password=${db.password}"/>
      <jvmarg value="-Ddb.url=${db.url}"/>

      <classpath refid="master-classpath"/>
  </java>