1、最近将程序打包成jar的时候才出现个文件:
我将一个配置文件放在了classpath下,程序获取classpath路径后便使用了commons-io的fileutils.copy方法将该文件拷贝到system32下。
该程序在ide环境中云高兴没有问题,但是当我把程序打包成jar时,程序运行失败。
后来查资料才知道要将打包成jar中的文件拷贝到jar外,必须首先将文件从jar中解压出来,然后才能进行拷贝。
后来在jnative中查看源代码的时候,发现它有一个从jar包中拷贝dll到java-libary-path的代码。
该方法在JNative.java中:
2、加载dll,dll的当前目录变为jre/bin目录。
例如:dll需要加载一个配置ini文件,而这个dll默认读取和该dll的同级目录下ini文件。但是这时当前目录已经变为jre/bin目录,无论你的dll是否是放在system32下或者是其他的地方
我将一个配置文件放在了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下或者是其他的地方