Open up windows explorer in java
Asked Answered
R

2

6

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?

Rama answered 24/6, 2012 at 0:18 Comment(0)
D
14

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.

Daglock answered 24/6, 2012 at 0:28 Comment(5)
The Desktop version has the advantage of being portable code. The other way involves your code knowing what "explorer" to use for the current OS. (On the other hand, you get to choose ...)Sorrow
So if I used the getDesktop code it WILL work with all operating systems Stephen?Rama
@MichaelScott It should, provided that whoever wrote the AWT toolkit implementation for said OS implemented the open() method in a way that will open a file manager for a directory path. The way OS integration is done in the JDK, it's not really possible to guarantee this for every OS. The JDK itself only implements this for Windows, Motif, or using gnome_url_show under X11.Incoming
@Incoming It does not work for me on Ubuntu 13.04. I get an IOException: Failed to show URI:file:/home/.Pappas
@RobinJonsson Consider creating separate question for your problem. Include informations about Java version. Is problem related only to /home directory? Are you able to open other directories? What code are you using? Maybe problem is with privileges, how are you running you application?Daglock
A
2
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"));
  }
});
Applause answered 24/6, 2012 at 0:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.