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();
开发者_Go百科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
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")
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();
}
精彩评论