sup*_*eta 8 java java-native-interface load
我的主要():
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");
Run Code Online (Sandbox Code Playgroud)
LoadBinaries():
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;
}
Run Code Online (Sandbox Code Playgroud)
当我运行这个主要时,我得到下一个输出:
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)
Run Code Online (Sandbox Code Playgroud)
这个错误是因为它缺少一些c ++系统dll.但我的问题不是这个.我担心这个错误之后程序的位置!我没有看到catch上的print,方法循环后的print以及loadbinaries执行后的main上的print.
我怎样才能抓住这类错误并处理它们?示例:发生此错误时,我想打印"请安装c ++库"并控制其后的流程.
Luk*_*ard 21
尝试更换
catch (Exception e)
Run Code Online (Sandbox Code Playgroud)
在你的loadBinaries()方法的底部
catch (UnsatisfiedLinkError e)
Run Code Online (Sandbox Code Playgroud)
UnsatisfiedLinkError是它的子类Error,它不是以下的子类Exception: Error并且Exception都是ThrowableJava异常层次结构的根的子类.
通常情况下,你没有抓住Error.但是,您似乎有合理的理由这样做,因为您可以向用户显示一条消息,说"库X缺失,请安装它".
你得到的UnsatisfiedLinkError是不是一个子类Exception,因此不会被抓到的catch条款。如果您希望它被捕获,请将捕获更改为catch(Error e).
您看,Java 的异常层次结构有点不直观。您有两个类Exception和Error,每个类都扩展Throwable. 因此,如果你想捕捉所有你需要捕捉 Throwable 的东西(不推荐)。
RuntimException顺便说一下,是 的子类Exception。
| 归档时间: |
|
| 查看次数: |
6834 次 |
| 最近记录: |