2013-12-11 3 views
13

मैं जावा प्रोग्राम द्वारा स्थानीय (मेरे सिस्टम में) से HTML फ़ाइल खोलने का प्रयास करता हूं। मैंने कुछ कार्यक्रमों को स्टैक ओवरफ़्लो के माध्यम से प्राप्त करने की कोशिश की लेकिन यह उतना काम नहीं कर रहा है।जावा का उपयोग कर एचटीएमएल फाइल कैसे खोलें?

ईजी के लिए .: मेरे पास यह छोटी HTML फ़ाइल है।

<html> 
    <head> 
    Test Application 
    </head> 
    <body> 
    This is test application 
    </body> 
</html> 

मेरे जावा कोड:

Runtime rTime = Runtime.getRuntime(); 
String url = "D:/hi.html"; 
String browser = "C:/Program Files/Internet Explorer/iexplore.exe "; 
Process pc = rTime.exec(browser + url); 
pc.waitFor(); 

किसी भी समाधान या सुझावों की सराहना की।

+0

* "क्रोम में HTML फ़ाइल कैसे खोलें ..?" * क्यों 'क्रोम' उपयोगकर्ता के डिफ़ॉल्ट ब्राउज़र के विपरीत है? –

उत्तर

30

मैं यहाँ डिफ़ॉल्ट ब्राउज़र

File htmlFile = new File(url); 
Desktop.getDesktop().browse(htmlFile.toURI()); 
+0

@RamonBoza: ऐसा लगता है कि यह ठीक काम कर रहा है, धन्यवाद। –

+0

मैंने आपके प्रश्न को अस्वीकार नहीं किया, और नहीं पता कि किसी ने क्यों किया, आपका प्रश्न 100% मान्य है, इसलिए मैंने इसे xD – RamonBoza

+1

पर वोट दिया है यदि उपयोगकर्ता ने "एक्सटेंशन" जैसे फ़ाइल एक्सटेंशन में एक कस्टम "खोलें" कार्रवाई सौंपी है। तो यह ब्राउज़र नहीं खुलता है, लेकिन जिस प्रोग्राम ने उपयोगकर्ता को इसके साथ जोड़ा है .... यह बिल्कुल समाधान नहीं है! – thesaint

5

उपयोग करने के लिए एक विधि है कि शान से विफल रहता है के लिए कोड है पसंद करेंगे।

ध्यान दें कि स्ट्रिंग html फ़ाइल का स्थान हो सकती है। कि से

/** 
* If possible this method opens the default browser to the specified web page. 
* If not it notifies the user of webpage's url so that they may access it 
* manually. 
* 
* @param url 
*   - this can be in the form of a web address (http://www.mywebsite.com) 
*   or a path to an html file or SVG image file e.t.c 
*/ 
public static void openInBrowser(String url) 
{ 
    try 
     { 
      URI uri = new URL(url).toURI(); 
      Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null; 
      if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) 
       desktop.browse(uri); 
     } 
    catch (Exception e) 
     { 
      /* 
      * I know this is bad practice 
      * but we don't want to do anything clever for a specific error 
      */ 
      e.printStackTrace(); 

      // Copy URL to the clipboard so the user can paste it into their browser 
      StringSelection stringSelection = new StringSelection(url); 
      Clipboard clpbrd = Toolkit.getDefaultToolkit().getSystemClipboard(); 
      clpbrd.setContents(stringSelection, null); 
      // Notify the user of the failure 
      WindowTools.informationWindow("This program just tried to open a webpage." + "\n" 
       + "The URL has been copied to your clipboard, simply paste into your browser to access.", 
        "Webpage: " + url); 
     } 
} 
0
URI oURL = new URI(url); 
Desktop.getDesktop().browse(oURL); 

अलावा, यह सुनिश्चित करें कि फ़ाइल पहले से ही अपने इच्छित ब्राउज़र में खोला गया है। फ़ाइल पर आइकन की जांच करें, यदि यह एक टेक्स्ट फ़ाइल की तरह दिख रहा है, तो हो सकता है कि आप टेक्स्ट फ़ाइल के साथ पहले से ही खोले हों। तो वांछित प्रोग्राम में डिफ़ॉल्ट प्रोग्राम को बदलें।

संबंधित मुद्दे