【问题标题】:Import jar file in ant build file在ant构建文件中导入jar文件
【发布时间】:2013-04-01 08:10:06
【问题描述】:

我正在尝试编译一个从 jar 导入类的项目,但它不工作。这是我的主要课程:

package sample.calendar;
import org.jasypt.util.password.*;
public class OutlookToGmailCalendarSync {
  public static void main(String[] args) {
    System.out.println("hi");
  }
}

这是我的 build.xml:

<project name="MyCalendarSample" 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="dist"  location="dist"/>

  <path id="path.class">
    <pathelement location="build"/>
    <fileset dir="lib">
       <include name="*.jar"/>
    </fileset>
  </path>

  <!--Run-->
  <target name="run" depends="compile"
   description="Runs the complied project">
    <java fork="true" classname="sample.calendar.OutlookToGmailCalendarSync">
      <classpath>
        <path refid="path.class"/>
      </classpath>
    </java>
  </target>

  <!--Compile-->
  <target name="compile" depends="init"
        description="compile the source " >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" destdir="${build}"/>
  </target>

  <!--Init-->
  <target name="init">
    <!-- Create the time stamp -->
    <tstamp/>
    <mkdir dir="${build}"/>
  </target>

  <!--Clean-->
  <target name="clean"
        description="clean up" >
    <!-- Delete the ${build} directory trees -->
    <delete dir="${build}"/>
  </target>

</project>

这是我运行 ant 时的错误:

compile:
    [javac] C:\java\my\build.xml:31: warning: 'includeantruntime' was not set, d
efaulting to build.sysclasspath=last; set to false for repeatable builds
    [javac] Compiling 28 source files to C:\java\my\build
    [javac] C:\java\my\src\sample\calendar\OutlookToGmailCalendarSync.java:2: er
ror: package org.jasypt.util.password does not exist
    [javac] import org.jasypt.util.password.*;
    [javac] ^
    [javac] 1 error

org.jasypt.util.password 位于 lib 文件夹中的 jar 文件中,为什么会出现此错误?

【问题讨论】:

  • 看起来“lib”目录中缺少 jasypt.jar 文件

标签: java ant build jar


【解决方案1】:

设置compile command的类路径以包含jasypt.jar

【讨论】:

  • 我将编译部分中的行更改为&lt;javac srcdir="${src}" destdir="${build}" classpath="path.class"/&gt;,但仍然出现相同的错误提示package org.jasypt.util.password does not exist
  • 我把它改成了classpathref而不是classpath,它现在可以工作了
  • 但是现在,当我尝试使用 jar 文件中的任何内容时,我遇到了一个新问题。当我将行 CalendarService myService = new CalendarService("korshyadoo-MyOutlookSync-0.1"); 添加到 main 方法时,我得到 [java] Exception in thread "main" java.lang.NoClassDefFoundError: com/google/gdata/client/calendar/CalendarService
  • 您的类路径需要包含您正在使用的所有库的 jar,用冒号分隔
  • 您的意思是在&lt;fileset dir="lib"&gt; 下为每个jar 文件添加一个包含吗? *.jar 不应该把它们都包含进去吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-23
  • 2020-04-08
  • 2013-07-15
  • 2011-04-15
  • 2012-04-06
  • 2016-03-13
  • 1970-01-01
相关资源
最近更新 更多