4

If i have opened a text file using Desktop API in java and i want to close it again only using the code? I have opened it like this

Desktop d=Desktop.getDesktop();
    try {
        d.open(new File("C:/Users/home/Desktop/123.txt"));

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Thread.sleep(2000);

Now i want to close it?how will i do it,what code do i have to Write,Since there is no method in the Desktop API of the java

3 Answers 3

2

The Desktop#open() tells the OS to use its file type mappings to select and launch the appropriate application for a given file. Unless that application itself provides an API by which you can control it, you cannot close it. The desktop API does not retain a connection to the application that gets launched.

In any case, since the Java API does not return the process ID of the launched application, it is going to be problematic to even find the process.

2
  • Is there an another way through which i can open as well as close it? May 23, 2014 at 18:11
  • Only if you have a specific application in mind, and that application has an API by which it can be controlled. It would require some sort if inter-process communication API, such a control socket, or the ability to act on specific signals. If all you want to do is forcibly terminate the app, then the Java Process infrastructure might be usable, but we can't tell without a lot more details. May 23, 2014 at 18:13
1

In the API:

Launches the associated application to open the file.

If the specified file is a directory, the file manager of the current platform is launched to open it.

This means you can't close the file from the Desktop class. This is a related question you might want to look at. If you are worried about closing files like you would with streams, you are fine (since this isn't a stream where you need to save work).

If you want to still close the file, you would need to go thru the list of processes (how to do that for Windows) and some how identify which process is the application you just opened the file with, then close that with OS commands.

Also, the issue with doing something like this is:

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

It will only work on a Windows platform (check this out). If you want do this, you must know exactly what application it is every case and you must do it differently for each different OS:

System.getProperty("os.name")
2
  • Is there an another way through which i can open as well as close it? May 23, 2014 at 18:11
  • @user3239652 You have to know exactly what application it is. Else wise you can't do this (at least from my knowledge). May 23, 2014 at 18:15
1

It depends on what is the default application that opens txt file. Generally in windows its notepad. If that is the case you could try

Desktop d=Desktop.getDesktop();
    try {
        d.open(new File("test.txt"));
        Thread.sleep(5000);
        Runtime.getRuntime().exec("taskkill /IM notepad.exe");
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
    e.printStackTrace();
}

The problem with this is it closes all open notepad instances :(

1
  • Yes, I know.. But that's the first idea I got.. ;)
    – Syam S
    May 23, 2014 at 18:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.