在Java中使用ANT提取文件

发布于 2024-12-08 13:23:42 字数 2925 浏览 1 评论 0 原文

我正在尝试使用 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

I'm attempting to use the functionality of Ant to extract the contents of compressed files to a temporary folder. Obviously I'm doing one or more things wrong here, but what they happen to be is escaping me as examples of this type of code on the web seem to be few and outdated:

//...

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();
}
//...

The above is based on: http://www.jajakarta.org/ant/ant-1.6.1/docs/en/manual/antexternal.html

The Expander class:

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);
    }
}

Inside the Expander constructor I get the following at run-time:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人的夜不怕黑 2024-12-15 13:23:42

ant-launcher jar您的 java 程序的类路径中缺少。

我使用 Maven 的搜索网站来查找包含缺失类的 jar:

http://search.maven.org/#advancedsearch|gav

替代解决方案

为什么不使用 Groovy 来代替 Java 中的逻辑呢? ANT 支持已融入语言中:

def ant = new AntBuilder()

ant.unzip(src:"example.zip", dest:"exampleDir")

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:

def ant = new AntBuilder()

ant.unzip(src:"example.zip", dest:"exampleDir")
じее 2024-12-15 13:23:42

测试了这将是新的 Ant 版本:

static public void unzip(String zipFilepath, String destinationDir) {

    final class Expander extends Expand {
        public Expander() {
            Project antp = new Project();
            this.setProject(antp);
            this.getProject().init();
        }
    }
    File source = new File(zipFilepath);
    String folder = source.getName().substring(0,source.getName().indexOf('.'));
    File destFolderFinal = new File(destinationDir+"/"+folder);
    destFolderFinal.mkdirs();

    Expander expander = new Expander();
    expander.setSrc(source);
    expander.setDest(destFolderFinal);
    expander.setOverwrite(false);
    expander.execute();
}

Tested this will new ant version:

static public void unzip(String zipFilepath, String destinationDir) {

    final class Expander extends Expand {
        public Expander() {
            Project antp = new Project();
            this.setProject(antp);
            this.getProject().init();
        }
    }
    File source = new File(zipFilepath);
    String folder = source.getName().substring(0,source.getName().indexOf('.'));
    File destFolderFinal = new File(destinationDir+"/"+folder);
    destFolderFinal.mkdirs();

    Expander expander = new Expander();
    expander.setSrc(source);
    expander.setDest(destFolderFinal);
    expander.setOverwrite(false);
    expander.execute();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文