2012-04-09 113 views
2

我要關閉打開網頁瀏覽器/瀏覽器標籤從Java程序特定URL的特定URL。 我可以使用來自java的桌面API在Internet Explorer中打開URL。關閉Web瀏覽器從Java程序

下面的代碼片段在IE

java.awt.Desktop desktop = java.awt.Desktop.getDesktop(); 
desktop.browse(new java.net.URI("http://www.xyzz.com")); 

打開瀏覽器現在,當我再次運行程序我想,以確保沒有已經存在的實例,其中上面的網址在瀏覽器中打開。如果是這樣,關閉它並在新的TAB或瀏覽器窗口中再次打開它。這可能看起來有點奇怪的要求。

我知道基本的做法應該得到關於這個問題,我們需要確定過程,並殺死它比再次調用上面的代碼。

任何建議是值得歡迎的。

+0

這是很難在一個開發平臺,形成獨立的方式來實現。你在尋找一個操作系統特定的解決方案? – 2012-04-09 16:09:20

+0

我很好,如果這適用於Windows。 – Kamal 2012-04-09 16:34:06

+0

*「歡迎任何建議。」* 1)留下sigs。出問題 - 他們是噪音。 2)瞭解如何使用郵件發佈/編輯表單上方的代碼格式化按鈕。 *「在IE中打開瀏覽器」* 1)嗯,什麼? IE打開網頁和其他文檔,而不是'瀏覽器'2)它不會在這裏打開IE瀏覽器。我的默認瀏覽器是FF。 – 2012-04-09 17:16:32

回答

2

這簡直是不可能因爲:

一)你不知道過程是由desktop.browse()開始,

b)您不知道什麼其他。由browse()開始的過程可能是有益的。

算了。即使你能以某種方式破解它,它也不會可靠地工作。

+1

確實,使用桌面你不會走得太遠。但幾乎沒有什麼不可能的。有時候,它甚至不是是否已經有人做的工作 – 2012-04-09 16:22:56

+0

你解決不了問題B,如果用戶在瀏覽器中打開其他標籤或其他地方的導航你還是殺了整個瀏覽器那麼複雜。當然,你的建議可以解決原來的要求,但它會造成一大堆新問題。 – Durandal 2012-04-09 16:26:07

+0

不,但不要求 – 2012-04-09 16:28:03

3

我懷疑有什麼可以與桌面做的,因爲你沒有得到啓動的過程中的任何引用。

我已經在過去使用這種技術(Java 6的前地方桌面不存在),並且由於它與過程的工作,你應該能夠殺死它。

public static void openURL(String url) { 
    StringBuilder sb = new StringBuilder(); 
    if (System.getProperty("os.name").indexOf("Windows")>-1) { 
     String command = null; 
     String urlLC = url.toLowerCase(); 
     if (urlLC.startsWith("https")) { 
      command = WindowsCommandRetriever.getCommandForFileType("https"); 
     } else if (urlLC.startsWith("http")) { 
      command = WindowsCommandRetriever.getCommandForFileType("http"); 
     } 
     if (command == null) { 
      command = WindowsCommandRetriever.commandForExtension(".html"); 
     } 
     if (command.indexOf("%1") > -1) { 
      sb.append(command.substring(0, command.indexOf("%1"))); 
      sb.append(url); 
      sb.append(command.substring(command.indexOf("%1") + "%1".length())); 
     } else { 
      sb.append(command).append(' '); 
      sb.append('"'); 
      sb.append(url); 
      sb.append('"'); 
     } 
    } else { 
     sb.append("open "); 
     sb.append(url); 
    } 
    try { 
     final Process p = Runtime.getRuntime().exec(sb.toString()); 
     // Here you have the process. You can destroy it if you want 
     // You need to figure out how you are going to handle this here. 
    } catch (IOException e1) { 
     e1.printStackTrace(); 
     System.err.println("Error while executing " + sb.toString()); 
    } 
} 

而且WindowsCommandRetriever:

/* 
* (c) Copyright 2010-2011 AgileBirds 
* 
* This file is part of OpenFlexo. 
* 
* OpenFlexo is free software: you can redistribute it and/or modify 
* it under the terms of the GNU General Public License as published by 
* the Free Software Foundation, either version 3 of the License, or 
* (at your option) any later version. 
* 
* OpenFlexo is distributed in the hope that it will be useful, 
* but WITHOUT ANY WARRANTY; without even the implied warranty of 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
* GNU General Public License for more details. 
* 
* You should have received a copy of the GNU General Public License 
* along with OpenFlexo. If not, see <http://www.gnu.org/licenses/>. 
* 
*/ 

public class WindowsCommandRetriever { 
    /** 
    * 
    * @param extension 
    *   the file extension (with or without the preceding '.') 
    * @return the command to execute for the specified <code>extension</code> or null if there are no associated command 
    */ 
    public static String commandForExtension(String extension) { 
     String regKey = "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + extension; 
     String fileType = WinRegistryAccess.getRegistryValue(regKey, "ProgID", WinRegistryAccess.REG_SZ_TOKEN); 
     if (fileType == null) { 
      StringBuilder sb = new StringBuilder("cmd /C assoc "); 
      sb.append(extension.startsWith(".") ? extension : "." + extension); 

      ConsoleReader reader; 
      try { 
       Process process = Runtime.getRuntime().exec(sb.toString()); 
       reader = new ConsoleReader(process.getInputStream()); 
       reader.start(); 
       process.waitFor(); 
       reader.join(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
       return null; 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
       return null; 
      } 
      String result = reader.getResult(); 
      if (result.indexOf("=") > -1) { 
       fileType = result.substring(result.indexOf("=") + 1).trim(); 
      } 
     } 
     if (fileType == null) { 
      return null; 
     } 
     return getCommandForFileType(fileType); 
    } 

    public static String getCommandForFileType(String fileType) { 
     String path = "HKEY_CLASSES_ROOT\\" + fileType + "\\shell\\open\\command"; 
     return WinRegistryAccess.getRegistryValue(path, null, WinRegistryAccess.REG_SZ_TOKEN); 
    } 
} 


/* 
* (c) Copyright 2010-2011 AgileBirds 
* 
* This file is part of OpenFlexo. 
* 
* OpenFlexo is free software: you can redistribute it and/or modify 
* it under the terms of the GNU General Public License as published by 
* the Free Software Foundation, either version 3 of the License, or 
* (at your option) any later version. 
* 
* OpenFlexo is distributed in the hope that it will be useful, 
* but WITHOUT ANY WARRANTY; without even the implied warranty of 
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
* GNU General Public License for more details. 
* 
* You should have received a copy of the GNU General Public License 
* along with OpenFlexo. If not, see <http://www.gnu.org/licenses/>. 
* 
*/ 

import java.io.IOException; 
import java.io.InputStream; 
import java.io.StringWriter; 


public class WinRegistryAccess { 

    private static final String REGQUERY_UTIL = "reg query "; 

    public static final String REG_SZ_TOKEN = "REG_SZ"; 

    public static final String REG_BINARY = "REG_BINARY"; 

    public static final String REG_DWORD_TOKEN = "REG_DWORD"; 

    /** 
    * Returns the value for an attribute of the registry in Windows. If you want to now the processor speed of the machine, you will pass 
    * the following path: "HKLM\HARDWARE\DESCRIPTION\System\CentralProcessor\0" and the following attribute name: ~MHz 
    * 
    * @param path 
    *   - the registry path to the desired value 
    * @param attributeName 
    *   - the name of the attribute or null for the default 
    * @param attributeType 
    *   - the type of attribute (DWORD/SZ/...) default is REG_SZ 
    * @return - the value for the attribute located in the given path 
    */ 
    public static String getRegistryValue(String path, String attributeName, String attributeType) { 
     if (attributeType == null) { 
      attributeType = REG_SZ_TOKEN; 
     } 
     try { 
      if (!path.startsWith("\"")) { 
       path = "\"" + path + "\""; 
      } 
      StringBuilder sb = new StringBuilder(); 
      sb.append(REGQUERY_UTIL); 
      sb.append(path); 
      sb.append(' '); 
      if (attributeName != null) { 
       sb.append("/v "); 
       sb.append(attributeName); 
      } else { 
       sb.append("/ve"); 
      } 
      Process process = Runtime.getRuntime().exec(sb.toString()); 
      ConsoleReader reader = new ConsoleReader(process.getInputStream()); 
      reader.start(); 
      process.waitFor(); 
      reader.join(); 
      String result = reader.getResult(); 
      int p = result.indexOf(attributeType); 
      if (p == -1) { 
       return null; 
      } 
      return result.substring(p + attributeType.length()).trim(); 
     } catch (Exception e) { 
      return null; 
     } 
    } 

    public static class ConsoleReader extends Thread { 
     private InputStream is; 

     private StringWriter sw; 

     ConsoleReader(InputStream is) { 
      this.is = is; 
      sw = new StringWriter(); 
     } 

     @Override 
     public void run() { 
      try { 
       int c; 
       while ((c = is.read()) != -1) { 
        sw.write(c); 
       } 
      } catch (IOException e) { 
       ; 
      } 
     } 

     String getResult() { 
      return sw.toString(); 
     } 
    } 

    public static String getJDKHome() { 
     String key = "\"HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\""; 
     String currentVersionAtt = "CurrentVersion"; 
     String javaHomeAtt = "JavaHome"; 
     String res1 = getRegistryValue(key, currentVersionAtt, null); 
     String res2 = getRegistryValue(key + "\\" + res1, javaHomeAtt, null); 
     return res2; 
    } 

    public static void main(String s[]) { 
     String key = "\"HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\""; 
     String currentVersionAtt = "CurrentVersion"; 
     String javaHomeAtt = "JavaHome"; 
     String res1 = getRegistryValue(key, currentVersionAtt, null); 
     String res2 = getRegistryValue(key + "\\" + res1, javaHomeAtt, null); 
     System.out.println("CurrentVersion '" + res1 + "'"); 
     System.out.println("JavaHome '" + res2 + "'"); 
    } 
} 
+0

感謝Guillaume,我會試試這個解決方案。 – Kamal 2012-04-09 16:55:56