Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Call iExplorer, snap screen shot, kill browser, then show image problem....

807605Jun 23 2007 — edited Jun 23 2007
Hello,
I'm trying the following steps:
1) Call iexplorer using Runtime exec (works),
2) Add a ShutdownHook (does not seem to work),
3) Take a screenshot using Robot (works),
4) Save as local file (works),
5) Kill the browser called above (does not work),
6) Show the screenshot saved above (works).

The browser is not being killed by the application. I don't think the ShutdownHook is being executed........

Does anyone have any experience making ShutdownHooks work?? Do you see a way to make my code work?

Thanks in advance.
public class ImageMaker {

  public static void main(String[] args) {
        try{
                final Process p = Runtime.getRuntime().exec("explorer.exe http://www.yahoo.com");
                p.waitFor();
                //wait for page to load before screen shot
                Thread.sleep(5000);
                 Robot robot = new Robot(); 
            BufferedImage bImg =robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
  
            File file = new File("C:\\screencapture.jpg");
            ImageIO.write(bImg, "jpg", file);

//call the shutdown hook to kill the browser.........
             Runtime.getRuntime().addShutdownHook(new Thread(new Runnable(){
	public void run(){
	p.destroy();
	}
                  }));

     //Now call display window for the saved image
	
            JDialog window = new JDialog();
            Container windowContent = 
                window.getContentPane();
            BufferedImage newImage = 
                ImageIO.read(file);
            JLabel label = new JLabel(new 
                ImageIcon(newImage));
            JScrollPane pane = new 
                JScrollPane(label);
            windowContent.add(pane, 
                BorderLayout.CENTER);
            window.setSize(Toolkit.getDefaultToolkit().getScreenSize());
            window.show();


          }catch(Exception ex){
	ex.printStackTrace();
           }
         
    }//end of main

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 21 2007
Added on Jun 23 2007
2 comments
174 views