Help with java.net.URL

Get help on programming - C++, Java, Delphi, etc.
Post Reply
general_koffi
Registered User
Posts: 312
Joined: 11 Sep 2004, 02:00
Location: Johannesburg

Help with java.net.URL

Post by general_koffi »

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?
Core 2 Duo E6300 @2.2Ghz
ASUS P5B
Force3D Radeon HD 4870 512MB
4GB DDR2-800 RAM
Creative X-Fi Xtremegamer
User avatar
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:

Post by hamin_aus »

Code: Select all

public void launchBrowser(URL) 
{ 
    System.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + 
URL.toString()); 

}
:?:
Image
User avatar
rustypup
Registered User
Posts: 8872
Joined: 13 Dec 2004, 02:00
Location: nullus pixius demonica
Contact:

Post by rustypup »

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...

Code: Select all

java.awt.Desktop.getDesktop().browse(myURL.toURI());
if you're not developing in 1.6, consider using the JDIC, while at the same time you should really make a noise...
Most people would sooner die than think; in fact, they do so - Bertrand Russel
Worship
Registered User
Posts: 505
Joined: 28 Jan 2006, 02:00
Location: Johannesburg

Re: Help with java.net.URL

Post by Worship »

Koffi I see you also need a little help.

This is what we figured out - This is the display webpage method
public 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 ();
}
}
This is how to call the webpage
displayURL ("file:///G:/Comp/New%20Folder/index.htm");
general_koffi
Registered User
Posts: 312
Joined: 11 Sep 2004, 02:00
Location: Johannesburg

Post by general_koffi »

if you're not developing in 1.6, consider using the JDIC, while at the same time you should really make a noise...
It's a bit late for that. We're in matric. :p

*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
Worship
Registered User
Posts: 505
Joined: 28 Jan 2006, 02:00
Location: Johannesburg

Post by Worship »

Problem Solved :D
Post Reply