Java编写存储位置,如何编写可以提取JAR文件并将其数据存储在指定目录(位置)的Java程序?...

I have created a JAR file. Now, I created another Java program. I want to unpack that JAR file in some other directory, meaning I want to do something like unzip.

If I run jar -xf filename.jar this causes some error:

Exception in thread "main" java.io.IOException: Cannot run program "jar":

java.io.IOException: error=2, No such file or directory

at java.lang.ProcessBuilder.start(ProcessBuilder.java:459)

at java.lang.Runtime.exec(Runtime.java:593)`

解决方案

Or try this code:

Extract the Contents of ZIP/JAR Files Programmatically

Suppose jarFile is the jar/zip file to be extracted. destDir is the path where it will be extracted:

java.util.jar.JarFile jar = new java.util.jar.JarFile(jarFile);

java.util.Enumeration enumEntries = jar.entries();

while (enumEntries.hasMoreElements()) {

java.util.jar.JarEntry file = (java.util.jar.JarEntry) enumEntries.nextElement();

java.io.File f = new java.io.File(destDir + java.io.File.separator + file.getName());

if (file.isDirectory()) { // if its a directory, create it

f.mkdir();

continue;

}

java.io.InputStream is = jar.getInputStream(file); // get the input stream

java.io.FileOutputStream fos = new java.io.FileOutputStream(f);

while (is.available() > 0) { // write contents of 'is' to 'fos'

fos.write(is.read());

}

fos.close();

is.close();

}

jar.close();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值