• Post Reply Bookmark Topic Watch Topic
  • New Topic

Opening a browser from a java application  RSS feed

 
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I recently found this code that uses the runtime object to open a browser, and load in the selected url.



This code works fine when for example I want to open www.google.com for example.
But I want to open an htm file (page.htm) on my computer which I've placed in same package as class above.

So the ammended code is..

And this also works fine, my problem is when I jar up the project and try to run it, nothing happens.
Normally if I had an image inside the same jar I'm running, I would use a ClassLoader,
but here I'm expected to just pass a String into the openURL(url) method.

Can anyone help me solve this?
Thanks
 
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, good old BareBonesBrowserLauncher :-)

URLs can point into jar files; see http://docs.sun.com/source/819-0913/author/jar.html for more detail.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I tried pretty much everything, but it wouldn't work.

I basically saved jar on desktop as BROWSER.jar

so its path is...

C:\Documents and Settings\Colin\Desktop\BROWSER.jar

So then I used

String url = "jar:C:\\Documents and Settings\\Colin\\Desktop\\BROWSER.jar!\\main\\page.htm";

As well as loads of other variations, but it couldn't load the page, when it was inside the jar.

Any more thoughts? Thanks
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to concatenate file:// to specify the protocol to reach the file.
just like http:// in case of web pages. Search for file url on google and read some pages
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just one little point: why are you using an Exception rather than an if (browser != null) test? The latter would give much better performance.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!