How to Close All Open Browser Windows With Java

Save

Java does not, in and of itself, contain the tools needed to close other programs built into itself. However, don't let that stop you from applying your Java skills to automating your other tasks. By using Java's built-in "Runtime.getRuntime().exec" command, you can call the necessary tools from the operating system itself to achieve the task.

  • Open your text editor and immediately save the new file as "BrowserKill.java."

  • Paste the following code into the editor:

    import java.io.IOException;

    public class BrowserKill {

    public static void main(String[] args) throws IOException {

    String os = System.getProperty("os.name");

    if (os.contains("Windows")) {

    Runtime.getRuntime().exec("taskkill /F /IM chrome.exe");

    Runtime.getRuntime().exec("taskkill /F /IM iexplorer.exe");

    Runtime.getRuntime().exec("taskkill /F /IM firefox.exe");

    Runtime.getRuntime().exec("taskkill /F /IM safari.exe");

    Runtime.getRuntime().exec("taskkill /F /IM opera.exe");

    } else {

    // Assuming a non Windows OS will be some version of Unix, Linux, or Mac

    Runtime.getRuntime().exec("kill `ps -ef | grep -i firefox | grep -v grep | awk '{print $2}'`");

    Runtime.getRuntime().exec("kill `ps -ef | grep -i chrome | grep -v grep | awk '{print $2}'`");

    Runtime.getRuntime().exec("kill `ps -ef | grep -i safari | grep -v grep | awk '{print $2}'`");

    }

    }

    }

    This simply inspects the OS name to find out if it is a Windows or non-Windows PC. If Windows, it calls the Windows operating system command "taskkill" for a list of common browsers. If a non-Windows PC, it assumes that it is a Unix-based PC and calls the Unix "kill" command for a list of common browsers.

  • Save your Java file.

Tips & Warnings

  • This gets the job done, but as you can see, it has a few disadvantages. First, it assumes that all non-Windows operating systems are Unix-based. This assumption will work for everything from Solaris and Mac OS X but may break down on some more exotic operating systems, such as Haiku. Second, it requires you to explicitly list all the browsers you want to stop.
  • The Unix-based command will kill processes with the "firefox," "chrome" or "safari" anywhere in their process names. It is possible that this could kill processes besides the browser. For example, if you have a game installed named "Wild Animal Safari" and are running it under Unix/Linux/Mac OS X, it may confuse the kill command and be terminated along with the Safari browser.

References

Promoted By Zergnet

Comments

You May Also Like

  • What Does Close All Browser Windows Mean?

    Internet browsers display webpages in windows or tabs. Several windows can be open at a time, allowing the user to switch between...

  • How to Close a JFrame in Java With a Button

    JFrame is the standard window in the Java Swing component set. By default, it provides a title bar and a default "Close"...

  • How to Make a Web Browser

    Many beginning programmers believe that creating a Web browser can only be done in a large, group-project setting with hundreds of thousands...

  • How to Open a URL in Java

    Opening URL addresses in applications is useful for programmers when connecting to websites. You may want to open a URL to communicate...

  • How to Enable Java Applets in Internet Explorer

    Java is an object-oriented programming language developed by Sun Microsystems. It allows developers to create applications capable of running on any operating...

  • Java Runtime Exec Tutorial

    Java's Runtime class provides access to the runtime environment in which the application is running. Runtime's exec methods allow you to run...

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!