让Jacob从当前路径读取dll文件及相关打包方法
独立观察员2013.08.12
Jacob LibraryLoader.class 修改版代码
功能:让jacob可在当前路径下的dll文件夹内读取相关依赖的dll文件,免去复制到系统文件夹的麻烦。
Bug:好像就不会从系统路径读取了,难道catch内代码不执行?期待高手解答。
package com.jacob.com;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;
public final class LibraryLoader
{
publicstatic final String JACOB_DLL_PATH = "jacob.dll.path";
publicstatic final String JACOB_DLL_NAME = "jacob.dll.name";
publicstatic final String JACOB_DLL_NAME_X86 = "jacob.dll.name.x86";
publicstatic final String JACOB_DLL_NAME_X64 = "jacob.dll.name.x64";
publicstatic final String DLL_NAME_MODIFIER_32_BIT = "x86";
publicstatic final String DLL_NAME_MODIFIER_64_BIT = "x64";
publicstatic void loadJacobLibrary()
{
ResourceBundleresources = null;
Setkeys = new HashSet();
try{
resources=ResourceBundle.getBundle(LibraryLoader.class.getName(), Locale.getDefault(),LibraryLoader.class.getClassLoader());
Enumerationi = resources.getKeys();
while(i.hasMoreElements()) {
Stringkey = (String)i.nextElement();
keys.add(key);
}
}
catch(MissingResourceException e)
{
}
try {
Stringpath, name, dir;
path= System.getProperty("user.dir") + "\\dll\\"; //先获取当前目录下的dll文件夹路径——dlgcy;
name= getPreferredDLLName();
dir= path + name + ".dll"; //加后缀适应load()方法;
JacobObject.debug("Loadinglibrary " + dir + " using System.loadLibrary ");
System.load(dir); //使用load()可载入任意绝对路径下的库文件;
System.out.println("载入dll文件成功!");
}catch (Exception e) { //原方法;
Stringpath = System.getProperty("jacob.dll.path");
if ((path == null) && (resources !=null) && (keys.contains("jacob.dll.path"))) {
path =(String)resources.getObject("jacob.dll.path");
}
if (path != null) {
JacobObject.debug("Loading library" + path + " using System.loadLibrary ");
System.loadLibrary(path);
}
else {
String name = null;
if(System.getProperty("jacob.dll.name") != null)
name =System.getProperty("jacob.dll.name");
else if((System.getProperty("jacob.dll.name.x86") != null) &&(shouldLoad32Bit()))
{
name = System.getProperty("jacob.dll.name.x86");
} else if((System.getProperty("jacob.dll.name.x64") != null) &&(!shouldLoad32Bit()))
{
name =System.getProperty("jacob.dll.name.x64");
} else if ((resources != null) &&(keys.contains("jacob.dll.name")))
name =resources.getString("jacob.dll.name");
else if ((resources != null) &&(keys.contains("jacob.dll.name.x86")) && (shouldLoad32Bit()))
{
name =resources.getString("jacob.dll.name.x86");
} else if ((resources != null) &&(keys.contains("jacob.dll.name.x64")) &&(!shouldLoad32Bit()))
{
name =resources.getString("jacob.dll.name.x64");
}
else{
name = getPreferredDLLName();
}
JacobObject.debug("Loading library" + name + " using System.loadLibrary ");
System.loadLibrary(name);
}
e.printStackTrace();
}
}
publicstatic String getPreferredDLLName()
{
if(shouldLoad32Bit()) {
return"jacob-" + JacobReleaseInfo.getBuildVersion() + "-" +"x86";
}
return"jacob-" + JacobReleaseInfo.getBuildVersion() + "-" +"x64";
}
protectedstatic boolean shouldLoad32Bit()
{
Stringbits = System.getProperty("sun.arch.data.model", "?");
if(bits.equals("32"))
returntrue;
if(bits.equals("64")) {
returnfalse;
}
Stringarch = System.getProperty("java.vm.name", "?");
if(arch.toLowerCase().indexOf("64-bit") >= 0) {
returnfalse;
}
returntrue;
}
}
附下载:http://download.csdn.net/detail/w19921004/5916787
使用j2ewiz打包
首先在Eclipse中这样导出:
然后打开j2ewiz:
然后选择“浏览JAR…”或者你用好压将Jar文件提前解压那么这时就可以选择“选择目录…”:
接下来都是相应的选择;
这里还可以隐藏class,就是当你生成exe文件后,你用好压打开也看不见你的class:
(慎重选择隐藏功能,否则可能读取不到某些路径的文件)
接着将exe文件、依赖库文件夹、以及其它资源文件打包压缩:
然后点击“自解压”制作成类似安装包的自解压文件(可参考http://4462080.blog.51cto.com/4452080/1269688)。
转载于:https://blog.51cto.com/4462080/1271614