473,436 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,436 software developers and data experts.

How to select file on an open explorer window with java

5 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.
Jan 12 '21 #1
4 6442
dev7060
638 Expert 512MB
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.
Jan 12 '21 #2
Ariiza13
5 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.     }
Jan 12 '21 #3
dev7060
638 Expert 512MB
$ 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.
Jan 13 '21 #4
Ariiza13
5 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.
Jan 13 '21 #5

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

Similar topics

8
by: Rob Wahmann | last post by:
I'm having trouble with the following code appending the form name and an empty value to the end of the url. Can anyone tell me what's wrong? I appreciate any tips or advice you can pass along! ...
1
by: drunkfool28 | last post by:
When I have Windows Explorer open and then launch an Access application,another Windows Explorer window opens that is titled MS Access.This causes poor performance by Access. I can work around...
3
by: Sergey Poberezovskiy | last post by:
Hi, As part of my Windows application, I need to open a folder containing a file (fileName is specified in txtFile). I have no problem opening the folder: FileInfo info = new...
4
by: Thomas Scheiderich | last post by:
The following code works fine. It opens file reads the data and then closes. **************************************************************************** **************** Dim objFileInfo As...
7
by: Brian Henry | last post by:
Is there anyway to get a thumbnail of a file similar to the way explorer does that can be used in an application? I want to for example if i drag a file over a picture box, if it has the ability to...
5
by: Mad Scientist Jr | last post by:
has anyone done this from code? i just want to automatically size all open and non-mimized windows to the specified height/width (in twips?) maybe include a utility that reports the...
1
by: sathyanrockie | last post by:
Plase tell me what is the difference between window.open and window.showmodelessdialog. Can we use window.showmodelessdialog in place of window.open?
3
by: dibakar | last post by:
Hello I want to open a window using Window.showModalDialog once i click inside of a TextBox.And that window need to come just under the TextBox,so that it seems to a same control. So,what...
7
by: imad.sabonji | last post by:
HI I am using Visual Studio.Net 2003 and using C# I would like to know how is it possible to change the Internet explorer window title? ie. i want to get rid of "Microsot Internet Explorer"...
1
by: acmdo | last post by:
Hello, The following question is about the name of file, with commas and periods, using VB.net 2005 when I try to use the process.start instruction I can open a file in explorer with...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.