How to kill firefox child process/tab from Java/C++

后端 未结 3 363
没有蜡笔的小新
没有蜡笔的小新 2021-01-14 14:16

I have my application in Java which invokes a browser [IE or Firefox etc ] ..

Requirement is when my Java Application exits i have to kill all the web pages [Child p

相关标签:
3条回答
  • 2021-01-14 14:47

    Good question.

    There are several solution.

    The first is the following. Run browser not directly but using script (shell for unix, bat file for windows) that will report you the process ID of the browser that it runs. Then use kill -9 PID on linux and taskkill on windows. This solution will require you writing scripts for different platform (at list for 2).

    But I have other suggestion. If the URL that you are opening in browser is yours (I mean you can add some javascript there) you can do the following. The URL that you are opening will create AJAX call to server. When you close your application you should say to server to send command to the browser to close it. At this moment javascript that is running into the browser will close its window. This is the most cross-platform solution.

    BTW I think that the server that is used by mentioned AJAX component may be your own application. And the signal that you send to the AJAX component is just connection error. Really, if your application is the server and you are closing it, the AJAX call will fail. This 100% means that the browser should be closed too.

    0 讨论(0)
  • 2021-01-14 15:07

    try using pkill (killing by process name) command.

    Runtime.getRuntime().exec("pkill firefox");
    

    or in c++

    system("pkill firefox");
    

    But it's not platform independent. It will run on Unix-like operating system, and not in windows.

    0 讨论(0)
  • 2021-01-14 15:11

    In windows, you can run :

    wmic process get description, executablepath, processid | findstr firefox

    which will give you the PID of the firefox process. Then you can use tskill PID to kill the process.

    0 讨论(0)
提交回复
热议问题