Java - 捕获 System.load() 错误



My main((:

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");

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;
}

当我运行这个主程序时,我得到下一个输出:

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:javaworkspaceLibmylib.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 上的打印、方法中循环后的打印以及主上的打印

如何捕获此类错误并处理它们?示例:发生此错误时,我想打印"请安装所有c ++库"并控制其后的流程。

尝试替换

catch (Exception e)

loadBinaries()方法的底部

catch (UnsatisfiedLinkError e)

UnsatisfiedLinkErrorError的子类,不是Exception的子类:ErrorException都是Throwable的子类,是Java异常层次结构的根。

通常,您不会抓住Error。 但是,您似乎有合理的理由在这里这样做,因为您可以向用户显示一条消息,指出"库 X 丢失,请安装它"。

您得到的UnsatisfiedLinkError不是Exception的子类,因此不会被您的catch子句捕获。如果您希望捕获它,请将捕获更改为 catch(Error e) .

你看,Java的异常层次结构有点不直观。您有两个类,ExceptionError ,每个类都扩展Throwable。因此,如果你想抓住所有你需要抓住Throwable的东西(不推荐(。

顺便说一下,RuntimExceptionException的一个子类。

这是一个错误。 以及不按照 java PMD 捕获错误的好做法。

您可以点击这些链接了解更多信息

什么时候捕获java.lang.Error?

http://pmd.sourceforge.net/rules/strictexception.html

相关内容

最新更新