How to select file on an open explorer window with java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ariiza13
    New Member
    • Dec 2020
    • 5

    How to select file on an open explorer window with java

    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:

    Code:
    private void openFile(File f) {
        try {
            Runtime.getRuntime().exec("explorer.exe  /select," + f.getAbsolutePath());            
        } catch (Exception ex) {
            System.out.println("Error - " + ex);
        }
    }
    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.
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 655

    #2
    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.

    Comment

    • Ariiza13
      New Member
      • Dec 2020
      • 5

      #3
      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.

      Code:
          private void openFile(File f) {
              try {
                  
                  String comand = "cmd.exe /c start explorer.exe /select," + f.getAbsolutePath();
                  Runtime.getRuntime().exec(comand);
                 
              } catch (Exception ex) {
                  System.out.println("Error - " + ex);
              }
          }

      Comment

      • dev7060
        Recognized Expert Contributor
        • Mar 2017
        • 655

        #4
        $ start C:\Users\user\D esktop\
        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:
        Code:
        File file = new File("C:\\Users\\user\\Documents\\abc.cpp");
        Desktop desktop = Desktop.getDesktop();
        desktop.browseFileDirectory(file);
        O/P - java.lang.Unsup portedOperation Exception: 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.
        Code:
        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
        Code:
        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:\User s\user\Document s\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.

        Comment

        • Ariiza13
          New Member
          • Dec 2020
          • 5

          #5
          Thank you so much, the first thing i tried was the browseFileDirec tory 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\D esktop\

          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.

          Comment

          Working...