Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / September 2006

Tip: Looking for answers? Try searching our database.

Java start new Process (Browser) and notice when Browser Window is closed.

Thread view: 
Peter.weik@indatex.com - 15 Sep 2006 13:22 GMT
Hello,

I have a java programm which shall open a Browser with a certain URL
AND!!!!! if this browser window is closed from a use after a while, the
java programm should be informed about this.

The following code is what I did. I am able to open a browser OS
independently.
--------------------------------------------------------------------------------------------------------------------
package com.indatex.browsercommunication;
import java.io.*;
/* Note - you must include the url type -- either "http://" or
* "file://".
*/
public class BrowserControlOSIndependent
{
        // Used to identify the windows platform.
   private   String WIN_ID = "Windows";
   // The default system browser under windows.
   private  String WIN_PATH = "rundll32";
   // The flag to display a url.
   private  String WIN_FLAG = "url.dll,FileProtocolHandler";
   // The default browser under unix.
   private  String UNIX_PATH = "netscape";
   // The flag to display a url.
   private  String UNIX_FLAG = "-remote openURL";
   private Process p = null;

   public boolean hasBrowserFinished()throws Exception{
       if(p.exitValue()>0){
               return true;
       }
       p.destroy();
       return false;
   }

   public void displayURL(String url)
   {
       boolean windows = isWindowsPlatform();
       String cmd = null;
       try
       {
           if (windows)
           {
               // cmd = 'rundll32 url.dll,FileProtocolHandler
http://...'
               cmd = WIN_PATH + " " + WIN_FLAG + " " + url;
               p = Runtime.getRuntime().exec(cmd);
               try {
                                       p.waitFor();
                               } catch (InterruptedException e) {
                                       // TODO Auto-generated catch
block
                                       e.printStackTrace();
                               }
           }
           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 = 'netscape -remote
openURL(http://www.javaworld.com)'
               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 = 'netscape http://www.javaworld.com'
                       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(IOException 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  boolean isWindowsPlatform()
   {
       String os = System.getProperty("os.name");
       if ( os != null && os.startsWith(WIN_ID))
           return true;
       else
           return false;

   }

}

-----------------------------------------------------------------------------
Thats everything fine. BUT!! if I close the Browser Window the java
programm shall be notified.
How does this work???

I thougt the easiest way is to examine the exitValue() of the process
in a lets say endles loop.
That doesnt work with process.exitValue() I always get 0, if the
browser is closed or not.

Could you help me?

Thank,
Peter
Andrew Thompson - 15 Sep 2006 14:56 GMT
peter.weik@indatex.com wrote:
> Hello,
>
> I have a java programm which shall open a Browser with a certain URL

The URL being?

> AND!!!!! if this browser window is closed from a use after a while, the
> java programm should be informed about this.

(snip example)

If the toddler trips over the plug and drops the computer, your
Java program will not be informed.  I just wanted to make it
clear that it is *impossible* to guarantee that the application
would be informed.

> Could you help me?

Failing the above, and assuming all this is done with the
user's acitve consent (and trust) you might ..

Open a frames based web-page that has an applet in
one frame and the target document in another.  The applet
send information via socket back to the application.

Every 0.nn milliseconds, the applet sends a message that
conforms it is active.  If the signal has not been received in
2 x 0.nn ms, the application can assume the browser window
is closed.

Note that you might have to takes some extra precautions to
ensure that the the address of the page shown in the framed
web page has not changed (has the user clicked a link?).

HTH

Andrew T.
Peter.weik@indatex.com - 15 Sep 2006 17:23 GMT
Andrew Thompson schrieb:

> peter.weik@indatex.com wrote:
> > Hello,
[quoted text clipped - 34 lines]
>
> Andrew T.
Hello Andrew,
I try to make it more concret:

process = Runtime.getRuntime().exec  ("rundll32
url.dll,FileProtocolHandler " + theUrl);

process.waitFor();
while(1==1){
    if(process.exitValue() > 0){ //Process doesnt exist anymore
            doSomething();
            break;
   }
}

The java programm (it is a component) starts, the loop is running, If I
close now the browser, the exitValue() is still 0!!

I simply dont understand. i have a variable of the process, the process
doesnt exist anymore (Browser Window closed from the user), why isn't
there a property in 'brower' which tells me "The process which a
reference is not alive anymore").

Have I been clear?

Thanks,
have a nice weekend,
Peter
Andrew Thompson - 15 Sep 2006 17:43 GMT
> I try to make it more concret:

I will read the problem carefully this time.

> process = Runtime.getRuntime().exec  ("rundll32
> url.dll,FileProtocolHandler " + theUrl);
[quoted text clipped - 16 lines]
>
> Have I been clear?

Yes.  That seems relatively clear, but I am no expert in
processes, so I had better make no comment on that
aspect of the problem.  OTOH - I still recommend that
this is the entirely *wrong* strategy for detecting the end
of the 'display of a web page' - is that not the real intent
here?

After all, after your Java process has displayed the URL
in my browser, I might then use that window to surf 'home',
and while your process still reposrts that the JVM that ran
the browser is still 'active' - you cannot be sure it is displaying
the correct page.

As an aside - what is the content of this URL?
Do you *control* (can edit) the content?
Why is it so important for your application to know
whether the content is still open for display?

(There are a number of alternate strategies you might
try for the end effect you want, but I still do not have
a clear idea of what it is you are trying to *achieve*
by all this.)

Andrew T.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.