简体   繁体   中英

Close program by opened by her file

How to close the process programmaticaly (Java) by opened by her file. For exemple, i have some file named notes.txt . He is opening programmatically (using Java, but for ask i used CMD) over default program for txt-files , in my case it is Notepad and thereby it is creating new process named Notepad:
在此处输入图片说明 在此处输入图片说明

As you can see, i have one more process Notepad with an open file data.txt.

How can i programmatically complete process Notepad with file notes.txt so that data.txt is stays, using CMD or maybe some Java tools.

How to do it? Maybe i should get default program for opening the txt-files (and further other extensions) and then to do something with them...
In general, i need any ideas...

Thanks in advance. Regards...

Since you know the Window Titles, for example:

data.txt - Notepad
notes.txt - Notepad

then you can use the Window's taskkill utility:

try {
    Runtime rt = Runtime.getRuntime();
    rt.exec("taskkill /F /FI \"WINDOWTITLE eq notes.txt - Notepad\" /T");
} 
catch (IOException | SecurityException | IllegalArgumentException ex) {
    ex.printStackTrace();
}

Ultimately, if notes.txt and data.txt are open then notes.txt will be closed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM