2011-10-31 26 views
2

我試圖從Java啓動鏈接。我這樣做的方法是調用火狐,IE或Safari這樣的:從JAR啓動鏈接:「Firefox無法在www。%u.com上找到服務器。」

public class LinkLauncher implements Runnable { 
    static String Link; 
    public void launchLink(String link){ 
     Link = " \""+link+"\""; 
     Runnable runnable = new LinkLauncher(); 
     Thread thread = new Thread(runnable); 
     thread.start(); 
    } 
    public void run() { 
     if (Desktop.isDesktopSupported()) { 
      Desktop desktop; 
      desktop = Desktop.getDesktop(); 
      URI uri = null; 
      try { 
       uri = new URI(Link); 
       desktop.browse(uri); 
      } catch (IOException ioe) { 
      } catch (URISyntaxException use) { 
      } 
     } else { 
      Shell Shell = new Shell(); 
      String Cmd[]={"firefox", Link}; 
      String LaunchRes=Shell.sendShellCommand(Cmd); 
      if (LaunchRes.contains("CritERROR!!!")){ 
       String MCmd[]={"open" , Link}; 
       String MLaunchRes=Shell.sendShellCommand(MCmd); 
       if (MLaunchRes.contains("CritERROR!!!")){ 
        String WCmd[]={"explorer", Link}; 
        Shell.sendShellCommand(WCmd); 
       } 
      } 
     } 
    } 

} 

這種方法從NetBeans的偉大工程,但一旦我做一個java jar文件就停止工作。

當我從netbeans轉到jar時,它不會缺少任何庫。它只是在Firefox或其他瀏覽器中顯示%U作爲鏈接。

任何方式我可以解決這個問題?

的完整代碼,請http://hummingbird-hibl.googlecode.com/svn/trunk/

+0

這是關閉主題,但爲什麼'鏈接'靜態?你打算在LinkLauncher的其他實例上共享Link對象嗎? – gigadot

+0

我認爲你可以使'launchLink'方法變爲靜態,這樣你就可以在沒有創建'LinkLauncher'的對象的情況下午餐一個鏈接。否則,你只能在鏈接午餐時創建兩個「LinkLauncher」選項。 – gigadot

+0

以(空格)開頭的東西怎麼能成爲一個有效的URI?爲什麼這個代碼會忽略異常?爲了更好地幫助,請發佈一個[SSCCE](http://sscce.org/) –

回答

1

不支持的桌面,但Java的要求它是。我修改了這樣的代碼,因此最後嘗試的是啓動「支持」瀏覽器。

public class LinkLauncher implements Runnable { 
    static String Link; 
    public void launchLink(String link){ 
     Link = link; 
     Runnable runnable = new LinkLauncher(); 
     Thread thread = new Thread(runnable); 
     thread.start(); 
    } 
    public void run() { 

     Shell Shell = new Shell(); 
     String Cmd[]={"firefox", Link}; 
     String LaunchRes=Shell.sendShellCommand(Cmd); 
     if (LaunchRes.contains("CritERROR!!!")){ 
      String MCmd[]={"open" , Link}; 
      String MLaunchRes=Shell.sendShellCommand(MCmd); 
      if (MLaunchRes.contains("CritERROR!!!")){ 
       String WCmd[]={"explorer", Link}; 
       String WLaunchRes=Shell.sendShellCommand(WCmd); 
       if (WLaunchRes.contains("CritERROR!!!")){ 
        if (Desktop.isDesktopSupported()) { 
         Desktop desktop; 
         desktop = Desktop.getDesktop(); 
         URI uri = null; 
         try { 
          uri = new URI(Link); 
          desktop.browse(uri); 
         } catch (IOException ioe) { 
         } catch (URISyntaxException use) { 
         } 
        } 
       } 
      } 
     } 
    } 
} 
+0

'Shell'類在NetBeans 7.3中不可用。 – MountainX

相關問題