我的主要():
System.out.println("Start loading libraries");
boolean b2 = false;
try{
b2 = FileManager.loadBinaries();
} catch (Exception e){
System.out.println("Exception on loading");
}
System.out.println("Libraries loading ended");
加载二进制文件():
public static boolean loadBinaries(){
String os = System.getProperty("os.name").toLowerCase();
ArrayList<String> bins = new ArrayList<String>();
if(os.indexOf("windows 7") >= 0 || os.indexOf("windows vista") >= 0){
bins.add("/nm/metadata/bin/win/libcurld.dll");
bins.add("/nm/metadata/bin/win/libfftw3f-3.dll");
bins.add("/nm/metadata/bin/win/libmad.dll");
bins.add("/nm/metadata/bin/win/libsamplerate.dll");
bins.add("/nm/metadata/bin/win/seven/mylib.dll");
}
else if(os.indexOf("windows xp") >= 0){
bins.add("/nm/metadata/bin/win/libcurld.dll");
bins.add("/nm/metadata/bin/win/libfftw3f-3.dll");
bins.add("/nm/metadata/bin/win/libmad.dll");
bins.add("/nm/metadata/bin/win/libsamplerate.dll");
bins.add("/nm/metadata/bin/win/xp/mylib.dll");
} else if(os.indexOf("mac") >= 0){
return false;
}
File f = null;
for(String bin : bins){
InputStream in = FileManager.class.getResourceAsStream(bin);
byte[] buffer = new byte[1024];
int read = -1;
try {
String[] temp = bin.split("/");
f = new File(LIB_FOLDER + "/" + temp[temp.length-1]);
File realF = new File(f.getAbsolutePath());
if(!realF.exists()){
FileOutputStream fos = new FileOutputStream(realF);
while((read = in.read(buffer)) != -1) {
fos.write(buffer, 0, read);
}
fos.close();
in.close();
}
System.out.println("Hello Load");
System.load(f.getAbsolutePath());
System.out.println("Bye Load");
} catch (Exception e) { System.out.println("Bye Exception"); FileManager.log(e.getMessage(), true); librariesLoaded = false; return false; }
}
System.out.println("Bye Method");
librariesLoaded = true;
return true;
}
当我运行这个 main 时,我得到下一个输出:
Start loading libraries
Hello Load
Bye Load
Hello Load
Bye Load
Hello Load
Bye Load
Hello Load
Bye Load
Hello Load
Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\java\workspace\Lib\mylib.dll: The application has failed to start because its side-by-side configuration is incorrect. Please see the application event log for more detail
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(Unknown Source)
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.load0(Unknown Source)
at java.lang.System.load(Unknown Source)
at nm.player.FileManager.loadBinaries(FileManager.java:264)
at nm.player.Player.<init>(Player.java:88)
at nm.player.Player.main(Player.java:523)
这个错误是因为它缺少一些 c++ 系统 dll。但我的问题不是这个。我担心程序在这个错误之后会去哪里!我没有看到 catch 上的打印,方法中循环后的打印,也没有看到 loadbinaries 执行后 main 上的打印。
如何捕获此类错误并进行处理?示例:发生此错误时,我想打印“请安装 c++ 库”并控制其后的流程。
最佳答案
尝试替换
catch (Exception e)
在你的loadBinaries()
方法的底部
catch (UnsatisfiedLinkError e)
UnsatisfiedLinkError
是 Error
的子类,不是 Exception
的子类:Error
和 Exception
都是 Throwable
的子类,它是 Java 异常层次结构的根。
通常,您不会捕获错误
。但是,您似乎有理由在此处这样做,因为您可以向用户显示一条消息,提示“缺少库 X,请安装它”。
关于Java - 捕获 System.load() 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5764057/