Solved

Kill the iexplore.exe process using java code

Posted on 2010-09-14
24
1,788 Views
Last Modified: 2012-06-21
I want to get rid of all internet explorer browser windows. I believe this can be done using killing all the iexplore.exe processes. Pleaase provide the java code for this.
Thanks for reading
0
Comment
Question by:PearlJamFanatic
  • 7
  • 7
  • 6
  • +3
24 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 33670661
That can't be done in Java - you'd need native code. You can try a command line util, such as pkill + Java

http://technojeeves.com/joomla/index.php/free/52-runtimeexec
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 33670690
you need to execute the windows command from Java app through Runtime.exec, http://www.tech-recipes.com/rx/446/xp_kill_windows_process_command_line_taskkill/
0
 
LVL 92

Expert Comment

by:objects
ID: 33670696
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:PearlJamFanatic
ID: 33670984
can anyone give the exact piece of code. Not able to figure out from the links you have provided so far.
something like
void kill_All_IE(){
//blah
}
 
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 33671000
this explains the step by steps for Runtime.exec http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
0
 
LVL 16

Expert Comment

by:Valeri
ID: 33671356
try this :
void kill_All_IE() {
try {
    Process process = Runtime.getRuntime().exec("taskkill /F /IM iexplore.exe");
    } catch (IOException e) {
      e.printStackTrace();
    }
}
0
 
LVL 26

Accepted Solution

by:
ksivananth earned 250 total points
ID: 33671532
try this,

try {
String[] cmd = new String[3];
                cmd[0] = "cmd.exe" ;
                cmd[1] = "/C" ;
                cmd[2] = "taskkill /F /IM iexplore.exe" ;

    Process process = Runtime.getRuntime().exec( cmd );
    } catch (IOException e) {
      e.printStackTrace();
    }

0
 
LVL 26

Expert Comment

by:ksivananth
ID: 33671544
also read the traps link I have posted, you need to follow it for correct execution!
0
 
LVL 16

Expert Comment

by:Valeri
ID: 33671593
Just to make things more clear. This is the function:

void kill_All_IE() {
String s;
      try {
          Process process = Runtime.getRuntime().exec("taskkill /F /IM iexplore.exe /T");
          BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
          BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));

          // read the output from the command
          while ((s = stdInput.readLine()) != null) {
              System.out.println(s);
          }

          // read any errors from the attempted command
          while ((s = stdError.readLine()) != null) {
              System.out.println(s);
          }

      } catch (IOException e) {
        e.printStackTrace();
      }
}
You have to read standart output and error stream in order to avoid some hangs of your app.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
ID: 33671611
Try the following to avoid any lockup
	try {
	    String[] cmd = new String[] {
	    "cmd.exe",
	    "/C",
	    "taskkill",
	    "/F",
	    "/IM",
	    "iexplore.exe" };

	    Process process = Runtime.getRuntime().exec( cmd );
	    IOUtils.outputProcessStreams(process);
	} catch (IOException e) {
	    e.printStackTrace();
	}

Open in new window

0
 
LVL 26

Expert Comment

by:ksivananth
ID: 33672484
>>"taskkill",
          "/F",
          "/IM",
          "iexplore.exe"
>>

can be combined to one string!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33672541
It can, but an array is usually stronger as it's less likely to break in the shell
0
 
LVL 16

Expert Comment

by:Valeri
ID: 33672774
What's the difference between
Runtime.getRuntime().exec("taskkill /F /IM iexplore.exe");
and
String[] cmd = new String[3];
                cmd[0] = "cmd.exe" ;
                cmd[1] = "/C" ;
                cmd[2] = "taskkill /F /IM iexplore.exe" ;
Process process = Runtime.getRuntime().exec( cmd );
The first one will be executed silently, the second will open cmd window. Is there any other differences?
0
 
LVL 16

Expert Comment

by:Valeri
ID: 33672792
Probably the second solution will consume output & error streams, or not?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33672843
>>What's the difference between ..?

There's very little difference. You must remember that the purpose of this site is not only to provide the most direct answers but to show best practices too for reusable solutions

0
 
LVL 26

Expert Comment

by:ksivananth
ID: 33672868
>>The first one will be executed silently, the second will open cmd window. Is there any other differences?

have you tried this?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33672909
btw PearlJamFanatic, you should test any command line command before trying to implement it in Java, obviously
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 33672912
>>Runtime.getRuntime().exec("taskkill /F /IM iexplore.exe");

will not execute the actual command, actually this command can be executed in the win command console only, win doesn't understand this. if you don't specify cmd.exe, java will try to execute as win command which will just fail!
0
 
LVL 16

Expert Comment

by:Valeri
ID: 33673284
>> have you tried this?
Yes, I tried it. The IE windows were closed silently... the following code works:
Runtime.getRuntime().exec("taskkill /F /IM iexplore.exe");
>> You must remember that the purpose of this site is not only to provide the most direct answers but to show best practices too for reusable solutions
Yes, I agree with that! You are absolutely right! :-)
I was wondering whether the second solution consumes the output and error streams and the developer don't have to do that in the code?!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33673308
>>I was wondering whether the second solution consumes the output and error streams and the developer don't have to do that in the code?!

Line 11 of my last code will consume and print all
0
 
LVL 16

Expert Comment

by:Valeri
ID: 33675188
>> Line 11 of my last code will consume and print all
Hm... that's the question, will be there anything to consume at all?!
Ok, I will check this! :-)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33675259
>>will be there anything to consume at all?!

Even if there isn't, i refer you to http:#33672843 again. There's always the possibility that stderr could be produced under certain circumstances. Redirection of both stderr and stdout to nul could also be done, but again, that's not a reusable solution in all cases
0
 
LVL 2

Expert Comment

by:chataros
ID: 33681284
import java.io.IOException;


public class test
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        try
        {
            Process p = new ProcessBuilder("cmd", "/k taskkill /IM iexplore.exe /F").start();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

}
0
 
LVL 2

Expert Comment

by:chataros
ID: 33681298
tested on WinXp, java 1.6, but it should work with 1.5 also
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
couple of eclipse 5 45
Windows 10 IE Certificate Issue 10 51
runtime exception 2 41
java imports not found 4 19
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question