I have created a desktop window using Java Swing toolkit, which is opened and the requirement is to close that window using java code.
Window can be : file explorer window or any window you can consider which open on computer.
I have created a desktop window using Java Swing toolkit, which is opened and the requirement is to close that window using java code.
Window can be : file explorer window or any window you can consider which open on computer.
I understood, that you want to close a native window on a windows system.
In case that means to kill the underlying process, then you can use taskkill as supposed by almostA in the other answer.
If you really want to just close a window without terminating the process behind, you need a different approach:
Java applications run inside the java virtual machine so they have their own virtual environment and the java framework does not provide such methods.
(The java framework is also cross platform and it is different from operating system to operating system on how to get such a window and how to close it.)
I never worked with the Java Native Interface (https://docs.oracle.com/javase/7/docs/technotes/guides/jni/index.html) but from my understanding, that could be used to write native code which is then executed from a Java VM.
Or maybe it is much simpler to find a small tool which is doing this and then executing that external tool through the ProcessBuilder/Process/Runtime classes (I don't know of such a tool, but there might be such tools around. User Interface Testing tools might be able to do this and much more! And there are automatisation Tools around ....)
The technical idea behind: Windows applications have a message queue and you can use an API to access it. So you can send messages to windows (and controls inside windows) to do something, e.g. to close.
The API can be found inside user32.dll / winuser.h:
FindWindow functions e.g.: https://learn.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-findwindowa -> You can get the handle with that function which is then used in the next function.
CloseWindow: https://learn.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-closewindow
If you do not find an existing commandline toll to start, maybe you can quickly write a .Net application using an existing wrapper like CWindow on CodeProject: https://www.codeproject.com/Articles/25925/CWindow-A-wrapper-class-for-the-window-API-functio
Maybe a little complex but that are the ways that I know of to control windows.
I assume you are using Windows OS. You can execute a command line instruction:
Runtime.getRuntime().exec("cmd /c start cmd.exe /K \"<your command>\"");
For example, to close Firefox browser (by killing it's process):
Runtime.getRuntime().exec("cmd /c start cmd.exe /K \"taskkill /F /IM firefox.exe\"");
Thread about commands closing a particular explorer window: https://superuser.com/questions/1263315/how-to-close-a-particular-opened-folder-using-cmd-or-batch-file
Alternatively you can use a command to kill a specified process (just like the example shown above): https://www.windows-commandline.com/taskkill-kill-process/