拷贝jar包的文件

1、最近将程序打包成jar的时候才出现个文件:

我将一个配置文件放在了classpath下,程序获取classpath路径后便使用了commons-io的fileutils.copy方法将该文件拷贝到system32下。

该程序在ide环境中云高兴没有问题,但是当我把程序打包成jar时,程序运行失败。

后来查资料才知道要将打包成jar中的文件拷贝到jar外,必须首先将文件从jar中解压出来,然后才能进行拷贝。

后来在jnative中查看源代码的时候,发现它有一个从jar包中拷贝dll到java-libary-path的代码。

该方法在JNative.java中:
private static void loadFromJar() throws UnsatisfiedLinkError
{
File tempDir = new File(System.getProperty("user.dir"));
File dllFile = new File(tempDir, DLL_NAME);
// Thubby: This returns null for me!
//InputStream in = JNative.class.getResourceAsStream("../../../lib-bin/" + DLL_NAME);
InputStream in = JNative.class.getResourceAsStream("/lib-bin/" + DLL_NAME);
if (in == null)
{
if (!dllFile.exists())
{
throw new UnsatisfiedLinkError(DLL_NAME + " : unable to find in " + tempDir);
}
}
else
{
if (dllFile.exists() && dllFile.canWrite())
{
dllFile.delete();
}
if (!dllFile.exists())
{
byte[] buffer = new byte[512];
BufferedOutputStream out = null;
try
{
try
{
out = new BufferedOutputStream(new FileOutputStream(dllFile));
while (true)
{
int readed = in.read(buffer);
if (readed > -1)
{
out.write(buffer, 0, readed);
}
else
{
break;
}
}
}
finally
{
if (out != null)
{
out.close();
}
}
}
catch (IOException e)
{
throw new UnsatisfiedLinkError("Can't write library in " + dllFile);
}
}
System.load(dllFile.toString());
}
}

2、加载dll,dll的当前目录变为jre/bin目录。
例如:dll需要加载一个配置ini文件,而这个dll默认读取和该dll的同级目录下ini文件。但是这时当前目录已经变为jre/bin目录,无论你的dll是否是放在system32下或者是其他的地方
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值