aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Open an HTML page in IE or Netscape from Java Application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Open an HTML page in IE or Netscape from Java Application" Watch "Open an HTML page in IE or Netscape from Java Application" New topic
Author

Open an HTML page in IE or Netscape from Java Application

christy smile
Ranch Hand

Joined: Oct 15, 2001
Posts: 101
Hi, All,
I would like to know how to open an HTML page in IE or netscape from a java application. I have created an on-line help for the user guide and I would like to open it from a menu. I know that I could open it with JEditorPane, but I think a browser might be better since I used <frame> in my online documentation. Or, do you guys think JEditorPane would be a more appropriate choice.
Thanks.
Christy
Samiul Karim
Greenhorn

Joined: Sep 30, 2002
Posts: 9
Please see the following example:
public class BrowserControl
{
public static void displayURL(String url)
{
boolean windows = isWindowsPlatform();
String cmd = null;
try
{
if (windows)
{
cmd = WIN_PATH + " " + WIN_FLAG + " " + url;
Process p = Runtime.getRuntime().exec(cmd);
}
else
{
// Under Unix, Netscape has to be running for the "-remote"
// command to work. So, we try sending the command and
// check for an exit value. If the exit command is 0,
// it worked, otherwise we need to start the browser.
cmd = UNIX_PATH + " " + UNIX_FLAG + "(" + url + ")";
Process p = Runtime.getRuntime().exec(cmd);
try
{
// wait for exit code -- if it's 0, command worked,
// otherwise we need to start the browser up.
int exitCode = p.waitFor();
if (exitCode != 0)
{
// Command failed, start up the browser
cmd = UNIX_PATH + " " + url;
p = Runtime.getRuntime().exec(cmd);
}
}
catch(InterruptedException x)
{
System.err.println("Error bringing up browser, cmd='" +
cmd + "'");
System.err.println("Caught: " + x);
}
}
}
catch(Exception x)
{
// couldn't exec browser
System.err.println("Could not invoke browser, command=" + cmd);
System.err.println("Caught: " + x);
}
}
/**
* Try to determine whether this application is running under Windows
* or some other platform by examing the "os.name" property.
*
* @return true if this application is running under a Windows OS
*/
public static boolean isWindowsPlatform()
{
String os = System.getProperty("os.name");
if ( os != null && os.startsWith(WIN_ID))
return true;
else
return false;
}
/**
* Simple example.
*/
public static void main(String[] args)
{
displayURL("http://www.javaworld.com");
}
// Used to identify the windows platform.
private static final String WIN_ID = "Windows";
// The default system browser under windows.
private static final String WIN_PATH = "rundll32";
// The flag to display a url.
private static final String WIN_FLAG = "url.dll,FileProtocolHandler";
// The default browser under unix.
private static final String UNIX_PATH = "netscape";
// The flag to display a url.
private static final String UNIX_FLAG = "-remote openURL";
}
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Christy,

I would like to know how to open an HTML page in IE or netscape from a java application. I have created an on-line help for the user guide and I would like to open it from a menu. I know that I could open it with JEditorPane, but I think a browser might be better since I used <frame> in my online documentation. Or, do you guys think JEditorPane would be a more appropriate choice.

That's not a good idea, because it is platform-specific and therefore not portable. I used frames also. You need to write a HyperlinkListener for your JEditorPane to take care of the frame events. Here's my code:

Hope this helps,
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Pete Lyons
Ranch Hand

Joined: Aug 18, 2002
Posts: 109
Just a general note that Java Web Start has this ability, which is very handy. So Sun has some cross-platform open URL in browser code, but I'm not sure....wait google has answered all. Here is a nice tutorial
Launching a Browser from Java
Ronnie Phelps
Ranch Hand

Joined: Mar 12, 2001
Posts: 329
Is there a reason why I have 1.4 but I can't seem to locate javax.jnlp.BasicService ? Is it not included with the other api's???
christy smile
Ranch Hand

Joined: Oct 15, 2001
Posts: 101
Hi, Pete,
thank you for the link... However, I am doing the SCJD on Jdk1.3, which does not have javax.jnlp package (at least I think), so I do not think I could use this approach ... right?
Thanks.
Christy
christy smile
Ranch Hand

Joined: Oct 15, 2001
Posts: 101
Hi, Michael,
Thank you for the code segment. Now I got the JEditorPane to work with the frames. however, none of my picture was loaded. Do I have to write any code to load the pictures in the URL or it should be handled correctly by default? Thank you!
Christy
christy smile
Ranch Hand

Joined: Oct 15, 2001
Posts: 101
Hi, Michael,
I have another question for you. How do I specify the absolute URL for an html file on my local file system? I tried file:///d:/html/UserDoc.html, and got the following exception while executing JEditorPane.setPage(url) :
java.io.IOException: invalid url
at javax.swing.JEditorPane.setPage(Unknown Source)
at HelpViewer.showPage(HelpViewer.java:46)
at HelpViewer.<init>(HelpViewer.java:26)
at HelpViewer.main(HelpViewer.java:57)
The "url" is created by:
URL url = getClass().getResource(page);
where page is "file:///d:/html/UserDoc.html"
Thank you!
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Christy,

I have another question for you. How do I specify the absolute URL for an html file on my local file system? I tried file:///d:/html/UserDoc.html, and got the following exception while executing JEditorPane.setPage(url) :

First off, why do you need to access an absolute URL on the local file system? Your JEditorPane should only be using help files stored in your jar file. You should also design all your links in your help files to be relative instead of absolute. My help files were placed both in the executable jars for access at runtime and also in the docs directory of my submission for use thru a browser. If you absolutely (no pun intended) need to access a file on the local system you won't use getClass().getResource(). You need to construct a new URL with one of the URL class constructors for example:

Your images are probably not showing up because you are using absolute URLs. Try changing all of your links to relative.
Hope this helps,
Michael Morris
Pete Lyons
Ranch Hand

Joined: Aug 18, 2002
Posts: 109
Originally posted by Ronnie Phelps:
Is there a reason why I have 1.4 but I can't seem to locate javax.jnlp.BasicService ? Is it not included with the other api's???

I was just noting it's availability. It's part of Java Web Start, not the general JDK. You would need to install Web Start separately. Unfortunately, you cannot use it in the Developer exam.
I would follow Michael's advice - put the html and image files in your jar, and get them by relative path. I also added code that looked on the filesystem if it could not find them through getClass().getClassloader(), but that was just so it would work while I was developing and running with my .class files directly on disk instead of jarred up.
christy smile
Ranch Hand

Joined: Oct 15, 2001
Posts: 101
Hi, Michael, Pete,
I am so embarassed to ask the following questions:
(1) How do I specify the relative URL ?
My directory structure is the following:
HelpViewer.java displays the on-line help document.
It is in suncertify.client.gui package.
The on-line help documents are in the root. Therefore, for me to refer to the on-line help document in HelpViewer class, I have to use
getClass().getResource("../../../MyUserDoc.html"), is that correct?
I don't think that's the right way to specify the relative path.
The only time my on-line help comes up is if I move the html document to the same directory as the HelpViewer class.
(2) My snap shots are still not displaying even if I don't use absolute URL. The problem might be that I put all the snap shot files in a separate directory from the main html directory. e.g. Snap shots are in html\bmp and all the html files are in html directory. Do you think this might be causing the problem?
(3) I did not understand fully about jarring up the html files... Are you serving the html files out of a jar file? Or the jar files is the top level jar file that you submitted to Sun. And when the grader runs the problem, the html directory is just a regular directory.
I hope I am not confusing anyone since I am totally confused...
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Christy,

(1) How do I specify the relative URL ?
My directory structure is the following:
HelpViewer.java displays the on-line help document.
It is in suncertify.client.gui package.
The on-line help documents are in the root. Therefore, for me to refer to the on-line help document in HelpViewer class, I have to use
getClass().getResource("../../../MyUserDoc.html"), is that correct?
I don't think that's the right way to specify the relative path.
The only time my on-line help comes up is if I move the html document to the same directory as the HelpViewer class.

The best way to open your help files it to have a constructor in your HelpViewer class that takes a URL as an argument. I constructed the help system at GUI startup, set its default close at HIDE_ON_CLOSE and called its show() method only when the user requested it.
I put all of the help files in the executable jars. For the client, there was a directory in the client package (suncertify/client/) called help/. The same was true for the server. Inside that directory were all the HTML help files and a subdirectory called images which contained all my screen shots in JPEG format. With that in place, I just did the following:

As you can see, I used the relative URL "help/help.html" as the base document. Now all I have to do is make all links relaive to help/. Here is an excerpt from one of my HTML files:


(2) My snap shots are still not displaying even if I don't use absolute URL. The problem might be that I put all the snap shot files in a separate directory from the main html directory. e.g. Snap shots are in html\bmp and all the html files are in html directory. Do you think this might be causing the problem?

What type of image are you using? For HTML you need to use GIF, JPEG or PNG. Also note that JEditorPane is very slow about loading images. It's OK to put your images in a separate directory and in fact is good practice. You could refer to an image similar to:

For URLs you need to use forward slashes / not back slashes \.

(3) I did not understand fully about jarring up the html files... Are you serving the html files out of a jar file? Or the jar files is the top level jar file that you submitted to Sun. And when the grader runs the problem, the html directory is just a regular directory.

As I indicated above, put the HTML files in a subdirectory in your client and/or server packages in your executable jar files.
Hope this helps,
Michael Morris
christy smile
Ranch Hand

Joined: Oct 15, 2001
Posts: 101
Hi, Michael,
Thank you for your detailed explaination. Now, it is almost crystal clear. I still have a couple concerns.
code:
--------------------------------------------------------------------------------
URL helpURL = getClass().getResource("help/help.html"); helpSystem = new HelpFrame( "Fly By Night Services � Database Client Help System", helpURL);
--------------------------------------------------------------------------------
(1) The relative path is only for going forward (if a you have a directory inside the current directory), what if I want to go backward. For instance, if I have <b>suncertify\client\gui</b> for the java source code, but the help\help.html resides in <b>suncertify\client</b>?
(2) I compiled all my class files into the classes directory, and I noticed that when I jar everything up, the html files have to be in the classes directory as well. Is this true?
Just one more thing I remembered
Since I am going to have a Server User Guide and Client User Guide, do I need to create a combined one and put it at the root directory of the submitted project for the users to use without starting any program?
Thank you very much!
Christy
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Christy,

(1) The relative path is only for going forward (if a you have a directory inside the current directory), what if I want to go backward. For instance, if I have <b>suncertify\client\gui</b> for the java source code, but the help\help.html resides in <b>suncertify\client</b>?

The name of the parent directory is "..", for example:

Assuming that your current page is suncertify/client/gui/index.html, then the above link references suncertify/client/help/help.html.

(2) I compiled all my class files into the classes directory, and I noticed that when I jar everything up, the html files have to be in the classes directory as well. Is this true?

That's what I did and it makes it portable to do it thus. The package or directory structure can change and it will not affect the help files.

Since I am going to have a Server User Guide and Client User Guide, do I need to create a combined one and put it at the root directory of the submitted project for the users to use without starting any program?

That's what I did. I had an index.html in the userdocs/ that had two links: one for the client help files and one for the server help files. So if the user selected the client link he got the identical view he would as selecting Help from the Client GUI.
Hope this helps,
Michael Morris
Thomas Fly
Ranch Hand

Joined: Sep 09, 2002
Posts: 164
Hi Christy,
Michael has explained this all quite well, but here's one more explanation if it hasn't sunk in yet (for you or anybody else having this trouble).
If you have an HTML document (e.g., AbuserManual.html) that contains some application-frame captures, good HTML practice is to put those .gif's or .png's (don't use .jpg's or .bmp's- except .jpg's for actual photos) into an "images" subdirectory (of the directory containing your Abuser's Manual).
And actually, in some cases a .gif or .png may be suitable for what you'd think of as "a photo" :

If you understand packages (and certainly you do), then you understand directory structures, since they end up being the same thing.
To run your app, it must be in the classpath (which generally includes a . or "this directory", so you can run anything in your current working directory).
The "codebase" parameter for your server also functions as a classpath, telling the server where to find the stub so it can download it to the client, for example.
To start up your app, you'll type something like "java suncertify.client.Main" You've therefore specified a relative path (viz., the path suncertify\client- which contains Main.class- relative to your working directory- or some other directory in your classpath).
Now, of course, if "java" isn't in your system path (or current working directory), the above won't work, will it?.. classpath serves the same function as path.
To get to the point, in your AbuserManual.html, specify simply with <img src="images/FBN.gif" etc.>, where images is a subdirectory of where AbuserManual.html is located.
Since you're using frames, AbuserManual.html may just be the frame html, which says how to divide the screen between contents.html, and main.html, for example.
You'd also want use a relative URL to point from your contents to main. Since the 2 files will presumably be in the same directory, a link in contents may look something like <a href="main.html#FindaRealJob" target="main">Take this Job and Shove It!</a>
When you jar it all up (note also that jar's are just zip files, and you can fool around with them perfectly well using a friendly tool like WinZip, whatever "exploded" directory structure you had that worked should be preserved in your jar file.
In fact, you should think of a jar file as just a convenient way to bundle files in a way that preserves their directory structure.
Unfortunately, a jar file doesn't seem to be a perfect substitution for files in a directory. For example, the assessor will have to extract db.db from your jar in order for it to function (allow writes as well as reads) just like the "exploded" db.db
All your class files, HTML, and images should be perfectly happy to work & play from within your jar, however- take a look at %JAVA_HOME%\jre\lib\rt.jar (runtime dot java archive) with WinZip sometime.
Try stuffing the following into your main method if you're having problems; it spews out your classpath, and everything else.

[ October 05, 2002: Message edited by: Thomas Fly ]

Fly by Night Consultants<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr><i>I climbed on the back of a giant albatross<br />which flew through a crack in the cloud<br />to a place where happiness reigned...<br />all year 'round<br />the music played ever so loudly!</i><p><a href="http://thomasfly.com/songs/Traffic/Hole_in_my_Shoe_qt.htm" target="_blank" rel="nofollow">Hole in My Shoe</a><hr></blockquote>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Open an HTML page in IE or Netscape from Java Application