Sometimes, we come across requirements where we need to open a particular folder or directory in the native file explorer. In this tutorial, we will see how to do that, easy and effective.
Opening file explorer with java in any platform – Windows, Linux and Mac
The easiest way to do this is by using Java Desktop. The class provides an open() function Desktop#open(File) to open explorer when the given file is a directory.
private static void openDirectory() throws IOException { File directory = new File("C://Program Files//"); Desktop.getDesktop().open(directory); }
From Java 9 onwards, Desktop is available on the java.desktop module. So, make sure to add “requires java.desktop;” to your module-info.java file.
Open file explorer and select/highlight a file
Now, if you want to open the file explorer and have a file preselected, then there is a way for that as well! You can make use of Desktop.fileBrowseDirectory(File) to launch the file explorer and have a file highlighted. Let’s see a code example for the same.
However, before using this function, you have to keep the following things in mind.
- The code will not work on ALL platforms.
- You need to check whether the feature is supported via Desktop.getDesktop().isSupported(Action.BROWSE_FILE_DIR) function.
- Minimum Java 9 is required
import java.awt.Desktop; import java.awt.Desktop.Action; import java.io.File; public class JavaFileExplorer { public static void main(String[] args) { openDirectory(); } private static void openDirectory() { //Check if the feature supported on your platform if (Desktop.getDesktop().isSupported(Action.BROWSE_FILE_DIR)) { File directory = new File("/home/afsal/Downloads/jdk-8u212-linux-x64.tar.gz"); //Open directory with browse_file_dir option Desktop.getDesktop().browseFileDirectory(directory); } } }
Open file explorer in Linux
Now, if you would like to add specific code for Linux/Ubuntu to open the file explorer, have a look into the following code snippet. We can make use of the xdg-open command to open any directory. The good thing about using this method is that, it works across multiple Linux distributions. They just have to have the xdg-open support.
private static void openDirectoryInLinux() throws Exception { Runtime.getRuntime().exec( new String[]{"sh", "-c", "/usr/bin/xdg-open '/home/genuinecoder/Downloads/'"} ); }
Open file explorer in Windows
Now, if you would like to add specific code for Windows OS to open the file explorer, have a look into the following code snippet. We will execute a cmd command to open Windows explorer.exe.
private static void openDirectoryInWindows() throws Exception { Runtime.getRuntime().exec("explorer C:\\"); }
Open file explorer in MacOS
If you are looking for specific code for macOS, you can make use of the /usr/bin/open function to open the directory on macOS.
private static void openDirectoryInMac() throws Exception { Runtime.getRuntime().exec(new String[]{"/usr/bin/open", file.getAbsolutePath()}); }
Conclusion
In this tutorial, we have learned how to open a directory from a java program. We have numerous methods to achieve the same goal here. We can use java.awt.Desktop for platform independent support and if platform-specific implementation is required, that is also available. You might also be interested in the following topics.
[…] Источник […]
[…] Источник […]
[…] Источник […]
… [Trackback]
[…] Read More here to that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Info to that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Read More Information here on that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Here you will find 98648 more Info on that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Information on that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Information on that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Find More Info here on that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] There you can find 17924 more Information on that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Read More to that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Find More Info here to that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Read More to that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Information to that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Information on that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Find More to that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Read More here on that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Here you can find 25515 additional Information to that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Read More on that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Find More on on that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Information on that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Find More on to that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Find More to that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Find More Info here to that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Find More on that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Info on that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Find More Information here on that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Read More to that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Information on that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Read More Information here on that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Read More to that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Information on that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Here you can find 81838 more Information on that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Read More Information here to that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Read More on to that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
… [Trackback]
[…] Read More here to that Topic: genuinecoder.com/how-to-open-file-explorer-in-java/ […]
cheap androxal cost without insurance
buying androxal generic when available
how to order rifaximin cheap online in the uk
purchase rifaximin generic uk next day delivery
cheap enclomiphene generic uk
cheapest buy enclomiphene generic from canada
kamagra livrer au royaume-uni nourri ex nuit
acheter kamagra sans ordonnance pour expédier du jour au lendemain
dutasteride canada free sample
discount dutasteride cheap real
how to order flexeril cyclobenzaprine cheap no prescription
flexeril cyclobenzaprine weight loss
purchase gabapentin buy from canada
cheapest buy gabapentin buy in the uk
ordering fildena generic pricing
how to order fildena purchase australia
how to buy itraconazole generic from india
itraconazole buy cod
order staxyn usa where to buy
how to buy staxyn purchase singapore
order avodart generic is it safe
order avodart generic is good
cheap xifaxan lowest price viagra
cheap xifaxan ireland over the counter
kanada online lékárna kamagra
koupit kamagra bez lテゥkaナ冱kテゥho pナ册dpisu nebo ト考enstvテュ