File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes open default OS file manager to a specific folder Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "open default OS file manager to a specific folder" Watch "open default OS file manager to a specific folder" New topic
Author

open default OS file manager to a specific folder

Fon Drank
Greenhorn

Joined: Feb 20, 2007
Posts: 14
I would like to add an action to my java application that takes a user to a specific directory when they click on the toolbar icon. For users using Windows, this is easy since I can just call explorer.exe from the command line. However, I don't know the equivalent in Mac OS or Linux. Can anyone help with this? I think in mac, I can use /usr/bin/open?

if (System.getProperty("os.name").beginsWith("Windows"))
{
Runtime.getRuntime().exec("explorer.exe " + path);
} else if (System.getProperty("os.name").indexOf("Linux")!=-1)
{
// What goes here?
} else if (System.getProperty("os.name").beginsWith("Mac"))
Runtime.getRuntime().exec("usr/bin/open" + path); // is this right?
...
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Your code for the Mac seems to be right for OS X - aside from missing a space after the /usr/bin/open. I don't know if it would work for older systems, but that's probably not a small number of users anyway.

Another option is if you're using JDK 6, you can use java.awt.Desktop to open() the directory with simple platform-independent code. That's nice, but only works if you can reasonably expect your users to have JRE 6 installed. For some applications that may be a perfectly reasonable expectation, but for others, not.


"I'm not back." - Bill Harding, Twister
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
The more save command for Windows platforms is the "start" command. That also not only works for folders, but also for all kinds of files.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Fon Drank
Greenhorn

Joined: Feb 20, 2007
Posts: 14
is Java 6 officially released?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42047
    
  64
is Java 6 officially released?


Not for the Mac.


Ping & DNS - my free Android networking tools app
Fon Drank
Greenhorn

Joined: Feb 20, 2007
Posts: 14
This was a good idea, but I doubt it will work. I just tried it with the JDNC library, which is basically the same as java.awt.Desktop, and I got a error back "The given file is a directory".


Originally posted by Jim Yingst:
Your code for the Mac seems to be right for OS X - aside from missing a space after the /usr/bin/open. I don't know if it would work for older systems, but that's probably not a small number of users anyway.

Another option is if you're using JDK 6, you can use java.awt.Desktop to open() the directory with simple platform-independent code. That's nice, but only works if you can reasonably expect your users to have JRE 6 installed. For some applications that may be a perfectly reasonable expectation, but for others, not.
 
GeeCON Prague 2014
 
subject: open default OS file manager to a specific folder