Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open up windows explorer in java

I have been looking for an answer to this on Stack Overflow, but I couldn't find an answer that worked for me.

Using Java, how do I create a button that will launch an Explorer Window to a specified directory? If this is possible, how do I make it work for OSX and Linux?

like image 801
Michael Scott Avatar asked Jun 24 '12 00:06

Michael Scott


People also ask

How do I open Java Explorer?

To view the project explorer, click on Window menu then, click on Show View and select Project Explorer. There is simpler way to open project explorer, when you are in the editor press alt + shift + w and select project explorer.

How do I open up Windows Explorer?

File Explorer in Windows 11 helps you get the files you need quickly and easily. To check it out in Windows 11, select it on the taskbar or the Start menu, or press the Windows logo key + E on your keyboard.

How do I open Windows Explorer from terminal?

To use Command Prompt to run File Explorer, use these steps: Open Start. Search for Command Prompt and click the top result to open the console. Type the following command and press Enter: explorer.

How do I open a file with Java?

Java FileInputStream class is used to open and read a file. We can open and read a file by using the constructor of the FileInputStream class. The signature of the constructor is: public FileInputStream(File file) throws FileNotFoundException.


2 Answers

I am not sure how it works in other OS but in Windows you can use something like this

Desktop.getDesktop().open(new File("c:\\"));

Edit

Found another way (check link to FileExplorer class from that answer). Also you can use System.getProperty("os.name") to determine operation system.

like image 149
Pshemo Avatar answered Oct 17 '22 19:10

Pshemo


javax.swing.JButton myButton = new javax.swing.JButton("BUTTON TEXT");
myButton.addActionListener(new java.awt.event.ActionListener() {

  @Override
  public void actionPerformed(ActionEvent e) {
    java.awt.Desktop.getDesktop().open(new java.io.File("MY PATH NAME HERE"));
  }
});
like image 24
Mike Price Avatar answered Oct 17 '22 18:10

Mike Price