467,092 Members | 1,242 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,092 developers. It's quick & easy.

How to select file on an open explorer window with java

Nibble
i'm trying to make a program that downloads some files, and now i want to open the folder (on the windows explorer) where those files are (it's always the same folder) and select the last file that was downloaded, i found this code to do it on java:

Expand|Select|Wrap|Line Numbers
  1. private void openFile(File f) {
  2.     try {
  3.         Runtime.getRuntime().exec("explorer.exe  /select," + f.getAbsolutePath());            
  4.     } catch (Exception ex) {
  5.         System.out.println("Error - " + ex);
  6.     }
  7. }
It works, but every time i press the button to open the folder it opens a new explorer window with the selected file so i end up with many explorer windows opened. Is there a way to do this on the folder that is already open?

I've been looking on the web for this but i only found the same code that i'm already using. All of those other posts about this issue are from many years ago so i don't know if it used to work that way on previous versions.

Thank you so much if any of you can help me.
6 Days Ago #1
  • viewed: 1344
Share:
4 Replies
dev7060
Expert 256MB
It works, but every time i press the button to open the folder it opens a new explorer window with the selected file so i end up with many explorer windows opened. Is there a way to do this on the folder that is already open?
Try the start. It should give focus to the existing explorer window that's open with the same path.
6 Days Ago #2
Nibble
Hi, thank you for helping me.
Do you mean using start like this? i just tried this way and still open a new explorer window every time.

Expand|Select|Wrap|Line Numbers
  1.     private void openFile(File f) {
  2.         try {
  3.  
  4.             String comand = "cmd.exe /c start explorer.exe /select," + f.getAbsolutePath();
  5.             Runtime.getRuntime().exec(comand);
  6.  
  7.         } catch (Exception ex) {
  8.             System.out.println("Error - " + ex);
  9.         }
  10.     }
6 Days Ago #3
dev7060
Expert 256MB
$ 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:
Expand|Select|Wrap|Line Numbers
  1. File file = new File("C:\\Users\\user\\Documents\\abc.cpp");
  2. Desktop desktop = Desktop.getDesktop();
  3. 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.
Expand|Select|Wrap|Line Numbers
  1. Process p = null;
  2. ProcessBuilder pb = null;
  3. ...
  4. pb = new ProcessBuilder("explorer.exe", "/select," + "C:\\Users\\user\\Documents\\abc.cpp");
  5. p = pb.start();
//use a flag var to execute the below lines when not the first case
Expand|Select|Wrap|Line Numbers
  1. p.waitFor();
  2. p.notifyAll();
  3. 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.
5 Days Ago #4
Nibble
Thank you so much, the first thing i tried was the browseFileDirectory method, and couldn't make it work, i hope some day they fix that method. Well i guess imma try this one.

$ start C:\Users\user\Desktop\

it's easier to sort files by creation file and simply press "home" key to get last one selected. If not i will try the filechooser to see if you can move or rename files with it.

Thank you.

edit: Actually i was thinking if is it possible that the program does "press" keys, if so, it can press letter by letter of the name of the file and it will end up with the actual file selected.
But i don't know if there's a way to send keys to a explorer window on java, i'll do some research.
5 Days Ago #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

8 posts views Thread by Rob Wahmann | last post: by
1 post views Thread by drunkfool28@gmail.com | last post: by
3 posts views Thread by Sergey Poberezovskiy | last post: by
4 posts views Thread by Thomas Scheiderich | last post: by
7 posts views Thread by Brian Henry | last post: by
5 posts views Thread by Mad Scientist Jr | last post: by
1 post views Thread by sathyanrockie@gmail.com | last post: by
3 posts views Thread by dibakar | last post: by
7 posts views Thread by imad.sabonji@globeexpress.com | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.