2011-04-23 55 views
5

我的main():爪哇 - 捕System.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"); 

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:\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上的打印。

如何捕捉這類錯誤並處理它們?示例:發生此錯誤時,我想打印「請安裝C++庫」並在其後面控制流程。

回答

15

嘗試用

更換

catch (Exception e) 

loadBinaries()方法的底部

UnsatisfiedLinkErrorError一個子類,這是Exception一個子類:ErrorExceptionThrowable兩個子類,Java異常層次結構的根。

通常情況下,您不會捕獲Error s。不過,您似乎有這樣的理由,因爲您可以向用戶顯示一條消息,說明「Library X丟失,請安裝它」。

3

你得到一個UnsatisfiedLinkError這是Exception一個子類,因此不會被你的catch子句捕獲。如果您希望它被捕獲,請將捕獲更改爲catch(Error e)

你看,Java的異常層次結構有點不直觀。你有兩個班,分別是ExceptionError,其中每個班次延伸Throwable。因此,如果你想抓住一切你需要趕上Throwable(不推薦)。

RuntimException順便說一句,是Exception的一個子類。