2012-04-15 5 views
7

자바 프로젝트에 외부 jar를 포함하고 싶습니다. 개미를 사용하고 있습니다. 외부 .jar는 폴더 lib에 있습니다. 내 build.xml은 다음과 유사합니다.개미가 외부 .jar를 포함합니다.

<?xml version="1.0" encoding="UTF-8"?> 
<project> 
    <path id="classpath"> 
     <fileset dir="lib" includes="**/*.jar"/> 
    </path> 

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

    <target name="compile"> 
     <mkdir dir="build"/> 
     <javac srcdir="src" destdir="build" classpathref="classpath" /> 
    </target> 

    <target name="jar"> 
     <mkdir dir="trash"/> 
     <jar destfile="trash/test.jar" basedir="build"> 
      <zipgroupfileset dir="lib" includes="**/*.jar"/> 
      <manifest> 
       <attribute name="Main-Class" value="com.Test"/> 
      </manifest> 
     </jar> 
    </target> 

    <target name="run"> 
     <java jar="trash/test.jar" fork="true"/> 
    </target> 
</project> 

그러나 작동하지 않습니다. 외부 .jar 파일을 가져 오려고 할 때 ant compile 명령 뒤에 오류가 있습니다. com.something 패키지가 없습니다. 제대로 작동하려면 무엇을 편집해야합니까?

정확한 오류 :

Compiling 23 source files to xy/build 
    xy/src/com/Test.java:5: package com.thoughtworks.xstream does not exist 
    import com.thoughtworks.xstream.*; 
^
    1 error 
+0

정확한 오류를 제공하도록 질문을 편집 할 수 있습니까? – Tom

+0

정확한 오류를 추가했습니다. – Pajushka

+0

일반 병에 외부 라이브러리를 쉽게 포함 할 수 없습니다. 이를 달성하려면 원 항아리 같은 특별한 작업이 필요합니다. – oers

답변

4

(가) 속성을 포함하지 않고 당신은 시도해야합니다 : 또한

<zipgroupfileset includes="*.jar" dir="lib"/> 
0

내가 :

<fileset dir="lib" /> 

그리고 항아리 부분에

는이 같은 클래스를 포함을 개미를 사용하여 여러 개의 의존성 JAR을 내 JAR에 포함시킵니다. 내 컴파일 작업은 다음과 같습니다. 아마도 비슷한 것이 당신을 위해 일할 것입니다.

<target name="compile" depends="init"> 
    <javac srcdir="${src}" destdir="${build}" includeantruntime="false"> 
    <classpath> 
     <pathelement path="${classpath}" /> 
     <fileset dir="${deps}"> 
     <include name="**/*.jar"/> 
     </fileset> 
    </classpath> 
    </javac> 
    <copy todir="${build}"> 
    <fileset dir="${src}" excludes="**/*.java"/> 
    </copy> 
</target> 
1

외부 라이브러리를 jar 파일로 넣을 수 없으며 클래스 로더가 해당 jar를 사용할 수 없습니다. 불행히도 이것은 지원되지 않습니다.

one jar과 같은 개미 작업은 필요한 모든 것을 포함하는 jar 파일을 만드는 데 도움이됩니다.

이 비트는 background information of one jar에서입니다 :

Unfortunately this is does not work. The Java Launcher$AppClassLoader does not know how to load classes from a Jar inside a Jar with this kind of Class-Path. Trying to use jar:file:jarname.jar!/commons-logging.jar also leads down a dead-end. This approach will only work if you install (i.e. scatter) the supporting Jar files into the directory where the jarname.jar file is installed.

Another approach is to unpack all dependent Jar files and repack them inside the jarname.jar file. This approach tends to be fragile and slow, and can suffer from duplicate resource issues.

다른 대안 :

  • jarjar : 항아리 항아리 링크 쉽게 자바 라이브러리를 재 포장하고 자신의 유통로를 포함 할 수있는 유틸리티입니다
+0

정확하지는 않지만 Hadoop과 같은 일부 응용 프로그램은 항아리의 lib 디렉토리에 항아리를 넣는 관습을 지원하며 응용 프로그램은 항아리를 클래스 패스에서 가져 오는 것을 처리합니다. –

+1

@DavidParks하지만 Hadoop이 자체 클래스 로더를 사용하기 때문입니다. 그래서 이것은 응용 프로그램 특정 및 공식 항아리 definiton의 일부가 아닙니다. – oers

0

가끔은 단지 jar 내용을 직접 사용할 수 있습니다. 단지 압축을 풉니 다.

 <unzip src="/Developer-Java/mysql-connector-java/mysql-connector-java-5.1.22-bin.jar" dest="${build_dir}" /> 
관련 문제