【问题标题】:Ant script giving class not found exception - why?Ant 脚本给类未找到异常 - 为什么?
【发布时间】:2012-12-17 13:01:23
【问题描述】:
<project name="MyProject" default="run" basedir=".">
    <description>
        simple example build file
    </description>
  <!-- set global properties for this build -->
  <property name="src" location="./src"/>
  <property name="build" location="./build"/>
  <property name="lib" location="./lib"/>
  <property name="zmq.dir" location="/usr/local/share/java"/>

  <path id="classpath.compile">
      <fileset dir="${lib}">
          <include name="**/*.jar"/>
      </fileset>
      <pathelement location="${zmq.dir}/zmq.jar"/>
  </path>

  <path id="classpath.run">
     <path refid="classpath.compile"/>
      <fileset dir="${build}/classes">
          <include name="**/*.class"/>
      </fileset>
  </path>

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

  <target name="init" depends="clean">
    <!-- Create the build directory structure used by compile -->
    <mkdir dir="${build}"/>
    <mkdir dir="${build}/classes"/> 
  </target>

  <target name="compile" depends="init"
        description="compile the source " >
    <property name="compile.classpath" refid="classpath.compile"/>
    <echo message="Compiling with classpath ${compile.classpath}"/>
    <javac srcdir="${src}" 
           destdir="${build}/classes">
        <classpath refid="classpath.compile"/>
    </javac>
  </target>

  <target name="run" depends="compile">
    <property name="run.classpath" refid="classpath.run"/>
    <echo message="Running with classpath ${run.classpath}"/>
    <java fork="true"
          dir="${build}/classes"
          classname="jgf.EMDR_Client"
          classpathref="classpath.run"/>
  </target>
</project>

当我运行项目时,会给出java.lang.ClassNotFoundException: jgf.EMDR_Client

我 echo 编译和运行的类路径

编译是:

/Users/JGF/Projects/emdr/lib/json_simple-1.1.jar:/usr/local/share/java/zmq.jar

运行是:

/Users/JGF/Projects/emdr/lib/json_simple-1.1.jar:/usr/local/share/java/zmq.jar:/Users/JGF/Projects/emdr/build/classes/jgf/EMDR_Client.class

我的 JAVA_HOME 是:/Library/Java/Home

ant java dir attribute 有问题吗?

【问题讨论】:

    标签: java ant task classnotfoundexception dir


    【解决方案1】:

    您应该使用 classpath 标记,尽管路径也应该适用于恕我直言。但要正确使用。 不要将类直接添加到路径中,而只添加 jar 或目录:

    <path id="classpath.run">
      <path refid="classpath.compile"/>
      <pathelement location="${build}/classes" />
    </path>
    

    这应该给你一个像这样的路径:

    /Users/JGF/Projects/emdr/lib/json_simple-1.1.jar:/usr/local/share/java/zmq.jar:/Users/JGF/Projects/emdr/build/classes
    

    Java 将首先查看 jar,然后查看目录 /Users/JGF/Projects/emdr/build/classes

    为了找到类jgf.EMDR_Client,Java 将在名为jgf 的子目录中查找名为EMDR_Client.class 的文件。所以它现在应该找到你的类了。

    再次强调:Classpath 元素不是类文件,而是目录或 JAR 文件(它们是压缩目录)

    【讨论】:

    • 我修改了脚本,去掉了对classpath.run的引用,直接引用了classpath.lib。仍然被称为不快乐......没有使用类路径标记 - 而是 java 任务的 classpathref 属性虽然仍然......仍然没有快乐。
    • 修改了java任务并嵌入了classpath标签而不是属性。那也没有用。相同的旧类未发现异常。
    • 我要补充一点,zmq.jar 是一个奇怪的野兽——ZeroMQ 项目的一部分——我相信它只是我用来在我的 Mac 上运行的本机类的 JNI 包装器。这不会是找不到课程的奇怪原因吧? - 从截图中可以看到,它位于 build/classes 目录中
    • 为我的回答添加了一些说明
    • 对 - 这有点道理 - 会试一试..但这让我觉得 Ant 中 java 任务的 dir 属性有什么意义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多