【问题标题】:Java - Catching System.load() ErrorsJava - 捕获 System.load() 错误
【发布时间】:2011-08-11 11:11:39
【问题描述】:

我的主要():

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

当我运行这个 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。但我的问题不是这个。我担心这个错误之后程序的去向!在执行 loadbinaries 之后,我没有看到 catch 上的打印、方法中循环之后的打印以及 main 上的打印。

如何捕捉此类错误并进行处理?示例:出现此错误时,我想打印“please intall c++ libraries”并控制其后的流程。

【问题讨论】:

    标签: java load java-native-interface


    【解决方案1】:

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

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

    When to catch java.lang.Error?

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

    【讨论】:

    • 虽然不捕获Error 或其子类是一个很好的一般规则,但这种情况似乎是一个例外。
    【解决方案2】:

    尝试替换

    catch (Exception e)
    

    在你的loadBinaries() 方法的底部

    catch (UnsatisfiedLinkError e)
    

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

    通常情况下,您不会捕捉到Errors。但是,您似乎有一个合理的理由这样做,因为您可以向您的用户显示一条消息,说“库 X 丢失,请安装它”。

    【讨论】:

      【解决方案3】:

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

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

      RuntimExceptionException 的子类。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-16
        • 1970-01-01
        • 1970-01-01
        • 2020-10-28
        • 2014-12-22
        • 1970-01-01
        • 1970-01-01
        • 2012-11-03
        相关资源
        最近更新 更多