Results 1 to 8 of 8

Thread: Web Page

  1. #1
    coder_java_ is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default Web Page

    Web Page
    i was just wondering if Java could open a web page like www[dot]google[dot]com
    in swing:
    say a button is clicked(actionperformed),
    //....
    //I want the command here.
    I even want to close the page after sometime(not manually).
    How can I do so?
    Thank You.

  2. #2
    DecafMatan's Avatar
    DecafMatan is offline Member
    Join Date
    Nov 2008
    Location
    Orange County, California
    Posts
    9
    Rep Power
    0

    Default

    Not my own code, but I've used this class before:

    Java Code:
    /////////////////////////////////////////////////////////
    //  Bare Bones Browser Launch                         //
    /////////////////////////////////////////////////////////
    
    import java.lang.reflect.Method;
    import javax.swing.JOptionPane;
    
    public class BareBonesBrowserLaunch {
    
       private static final String errMsg = "Error attempting to launch web browser";
    
       public static void openURL(String url) {
          String osName = System.getProperty("os.name");
          try {
             if (osName.startsWith("Mac OS")) {
                Class fileMgr = Class.forName("com.apple.eio.FileManager");
                Method openURL = fileMgr.getDeclaredMethod("openURL",
                   new Class[] {String.class});
                openURL.invoke(null, new Object[] {url});
                }
             else if (osName.startsWith("Windows"))
                Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
             else { //assume Unix or Linux
                String[] browsers = {
                   "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
                String browser = null;
                for (int count = 0; count < browsers.length && browser == null; count++)
                   if (Runtime.getRuntime().exec(
                         new String[] {"which", browsers[count]}).waitFor() == 0)
                      browser = browsers[count];
                if (browser == null)
                   throw new Exception("Could not find web browser");
                else
                   Runtime.getRuntime().exec(new String[] {browser, url});
                }
             }
          catch (Exception e) {
             JOptionPane.showMessageDialog(null, errMsg + ":\n" + e.getLocalizedMessage());
             }
          }
    
       }
    To open a webpage:

    Java Code:
    BareBonesBrowserLaunch.openURL(urlStr);

  3. #3
    coder_java_ is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default

    but how to close?

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  5. #5
    coder_java_ is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default

    I am talking about closing the opened Web Browser through Java.
    Like:
    BareBonesBrowserLaunch.openURL(urlStr);
    to open.
    .................................
    to close it after the page is loaded,or after some given time.
    Thanks

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    You cannot close an application using Java. Actually you cannot open an application either. In that above code you don't have open an application(ie: browser). Confused? In reality what happen there is, that code segment simply telling that browser what's being sent, actually content of the browser. Then browser decide how to execute that. Nothing related or nothing to do with Java in that sense.

  7. #7
    coder_java_ is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default

    so in that sense,Java is not so powerful rite?
    so I have no way to control applications?
    It is possible so many popular softwares right?
    so not even an experienced Software Engineer can do this?
    Thanks for your help.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by coder_java_ View Post
    so in that sense,Java is not so powerful rite?
    so I have no way to control applications?
    It is possible so many popular softwares right?
    so not even an experienced Software Engineer can do this?
    Thanks for your help.
    No lol, Java is more powerful to do this. To deal with application executions you have to learn about Process.

Similar Threads

  1. Go back to previous page using session in JSP page
    By gopikarikati009 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 06-23-2011, 10:30 AM
  2. setting the view to a jsp page from a self refeshing page
    By deepal_205 in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 08-15-2008, 04:41 PM
  3. Replies: 21
    Last Post: 08-01-2008, 11:13 AM
  4. warning that page has expired and i need to refresh the page again
    By 82rathi.angara in forum JavaServer Pages (JSP) and JSTL
    Replies: 5
    Last Post: 07-15-2008, 01:48 PM
  5. help with jsp page
    By praveena in forum New To Java
    Replies: 3
    Last Post: 02-05-2008, 11:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •