在Java中使用ANT提取文件
我正在尝试使用 Ant 的功能将压缩文件的内容提取到临时文件夹。显然,我在这里做错了一件事或多件事,但它们碰巧是我无法理解的,因为网络上此类代码的示例似乎很少且过时:
//...
public static void extract(File src, File dest){
String path = src.getPath();
String ext = path.substring(path.lastIndexOf('.') + 1).toLowerCase();
Expander expander = null;
if(ext.equals("tar")) {
expander = new Expander("untar","untar");
} else if(ext.equals("jar")) {
expander = new Expander("unzip","unzip");
} else if(ext.equals("zip")) {
expander = new Expander("unzip","unzip");
} else {
// ...
}
expander.setSrc(src);
expander.setDest(dest);
expander.execute();
}
//...
以上基于: http://www.jajakarta.org/ant/ant-1.6.1/docs/en/manual/antexternal.html
Expander 类:
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.taskdefs.Expand;
class Expander extends Expand{
public Expander(String taskType, String taskName){
this.setProject(new Project());
this.getProject().init();
this.setOwningTarget(new Target());
this.setTaskType(taskType);
this.setTaskName(taskName);
}
}
在 Expander 构造函数内,我得到以下内容运行时:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/tools/ant/launch/AntMain
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.apache.tools.ant.Project.initProperties(Project.java:313)
at org.apache.tools.ant.Project.init(Project.java:300)
at Expander.<init>(Expander.java:8)
at Main.extract(Main.java:151)
at Main.getDeliverableFiles(Main.java:130)
at Main.main(Main.java:36)
caused by: java.lang.ClassNotFoundException: org.apache.tools.ant.launch.AntMain
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 18 more
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ant-launcher jar您的 java 程序的类路径中缺少。
我使用 Maven 的搜索网站来查找包含缺失类的 jar:
http://search.maven.org/#advancedsearch|gav
替代解决方案
为什么不使用 Groovy 来代替 Java 中的逻辑呢? ANT 支持已融入语言中:
The ant-launcher jar is missing from your java program's classpath.
I use Maven's search website to find the jars containing missing classes:
http://search.maven.org/#advancedsearch|gav
Alternative solution
Instead of wring this logic in Java, why not use Groovy? ANT support is baked into the language:
测试了这将是新的 Ant 版本:
Tested this will new ant version: