$ start C:\Users\user\Desktop\
No select switch available to use with this hence works only with directories.
I tried a few things. Consider a file
abc.cpp that needs to have the focus in an existing explorer window.
Using Java Desktop API:
- File file = new File("C:\\Users\\user\\Documents\\abc.cpp");
-
Desktop desktop = Desktop.getDesktop();
-
desktop.browseFileDirectory(file);
O/P -
java.lang.UnsupportedOperationException: The BROWSE_FILE_DIR action is not supported on the current platform
Seems like there's an unresolved bug with win 10:
https://bugs.openjdk.java.net/browse/JDK-8233994
Approach #2: Store the processid of the explorer and try to destroy the previous window using the same object before invoking the new one.
- Process p = null;
-
ProcessBuilder pb = null;
-
...
-
pb = new ProcessBuilder("explorer.exe", "/select," + "C:\\Users\\user\\Documents\\abc.cpp");
-
p = pb.start();
//use a flag var to execute the below lines when not the first case
- p.waitFor();
-
p.notifyAll();
-
p.destroyForcibly();
Doesn't work. The process runs as external and the current thread is not the owner.
Another workaround but isn't an ideal/suitable solution at all. It would reset the taskbar and other child processes. Explorer is created by
userinit process and many processes are children of it. So it probably wouldn't be a good idea to play around.
$ taskkill /IM explorer.exe /F & explorer.exe /select,"C:\Users\user\Documents\abc.cpp" & explorer.exe
The last command is appended to restart and see things like the taskbar and tray icons again. I wouldn't recommend doing it. "The solution cannot be worse than the problem itself."
API modal dialogs like JFileChooser could work with setSelectedFile(). That way only one child dialog can be active at a time and interaction with the parent frame can only be done again after the dialog window is closed. This behavior may not suit in accordance with what you're looking for.