Hey
Doing a JAVA project for school.
My problem:
I need to open an external document (.html in this case) from within a Java application. It needs to be opened with whatever is the default opener for that type of file on the machine the app is running on (so Internet Explorer for a .html on most Windows machines etc.)
The file is supposed to be opened when the user clicks a button, or menu option from within the Java app, so it uses the ActionListener etc
I can specify the file with java.net.URL easily enough, but I can't find a method to open the thing with its default program.
Any ideas?
Help with java.net.URL
-
- Registered User
- Posts: 312
- Joined: 11 Sep 2004, 02:00
- Location: Johannesburg
Help with java.net.URL
Core 2 Duo E6300 @2.2Ghz
ASUS P5B
Force3D Radeon HD 4870 512MB
4GB DDR2-800 RAM
Creative X-Fi Xtremegamer
ASUS P5B
Force3D Radeon HD 4870 512MB
4GB DDR2-800 RAM
Creative X-Fi Xtremegamer
- hamin_aus
- Forum Moderator
- Posts: 18363
- Joined: 28 Aug 2003, 02:00
- Processor: Intel i7 3770K
- Motherboard: GA-Z77X-UP4 TH
- Graphics card: Galax GTX1080
- Memory: 32GB G.Skill Ripjaws
- Location: Where beer does flow and men chunder
- Contact:
Code: Select all
public void launchBrowser(URL)
{
System.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " +
URL.toString());
}
- rustypup
- Registered User
- Posts: 8872
- Joined: 13 Dec 2004, 02:00
- Location: nullus pixius demonica
- Contact:
while jamin's solution will work in most cases, it's not really considered best practice.. the exec() function returns a java.lang.Process object which needs to have listeners installed to trap errors... along with this is the fact that, should the exec() command block pending input, the execution thread is blocked as well.
NOTE: the java.lang.Runtime instance is acquired by calling Runtime().getRuntime() and not System.getRuntime()...
java 6 onwards, (which is the flavour you should be developing in), has support for this built into the java.awt.Desktop class...
if you're not developing in 1.6, consider using the JDIC, while at the same time you should really make a noise...
NOTE: the java.lang.Runtime instance is acquired by calling Runtime().getRuntime() and not System.getRuntime()...
java 6 onwards, (which is the flavour you should be developing in), has support for this built into the java.awt.Desktop class...
Code: Select all
java.awt.Desktop.getDesktop().browse(myURL.toURI());
Most people would sooner die than think; in fact, they do so - Bertrand Russel
Re: Help with java.net.URL
Koffi I see you also need a little help.
This is what we figured out - This is the display webpage method
This is what we figured out - This is the display webpage method
This is how to call the webpagepublic static void displayURL (String url)
{
try
{
String cmd = null;
cmd = WIN_PATH + " " + WIN_FLAG + " " + url;
Process p = Runtime.getRuntime ().exec (cmd);
}
catch (IOException ex)
{
ex.printStackTrace ();
}
}
displayURL ("file:///G:/Comp/New%20Folder/index.htm");
-
- Registered User
- Posts: 312
- Joined: 11 Sep 2004, 02:00
- Location: Johannesburg
It's a bit late for that. We're in matric. :pif you're not developing in 1.6, consider using the JDIC, while at the same time you should really make a noise...
*Sigh*
Thanks, guys. Worship is in my computers class and needs to do exactly the same thing, so I'll be in touch with him.
Core 2 Duo E6300 @2.2Ghz
ASUS P5B
Force3D Radeon HD 4870 512MB
4GB DDR2-800 RAM
Creative X-Fi Xtremegamer
ASUS P5B
Force3D Radeon HD 4870 512MB
4GB DDR2-800 RAM
Creative X-Fi Xtremegamer