我想将外部jar包含到我的java项目中。我用的是蚂蚁。外部.jar在文件夹lib中。我的build.xml看起来像这样:
<?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
后出现错误: package 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
发布于 2012-04-15 18:37:15
您应该尝试不使用includes属性:
<fileset dir="lib" />
在jar部分中,您可以像这样包含类:
<zipgroupfileset includes="*.jar" dir="lib"/>
发布于 2012-04-16 06:16:46
您不能将外部库放入jar中,并期望类加载器使用这些jar。不幸的是,这不受支持。
像one jar这样的ant任务可以帮助您创建包含所需内容的jar文件。
该位来自background information of one jar
不幸的是,这是不工作的。Java启动器$AppClassLoader不知道如何使用这种Class-Path从Jar内的Jar加载类。尝试使用jar:file:jarname.jar!/commons-logging.jar也会走入死胡同。只有当您将支持的Jar文件安装(即分散)到安装jarname.jar文件的目录中时,这种方法才有效。
另一种方法是解压缩所有相关的Jar文件,并将它们重新打包到jarname.jar文件中。这种方法往往是脆弱和缓慢的,并且可能会受到重复资源问题的影响。
其他替代方案:
Java链接是一个实用程序,它可以轻松地重新打包
中
发布于 2012-04-16 04:01:33
我还使用ant在我的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>
https://stackoverflow.com/questions/10164822
复制