Jakub Hampl Jakub Hampl - 5 months ago 25
HTML Question

Open a folder in finder/explorer from a webpage?

If I have a file system path can I open a window in Explorer (on windows) or in Finder (on OS X) displaying the folder that the path leads to?

Cookie points for answers that are cross-platform and/or plugin-less.

Answer

You need to be able to run a new process from a browser. There are a few ways to do this. I'll show the JNLP way to do this.

Create a jnlp file as follows:

    <?xml version="1.0" encoding="utf-8"?>
<jnlp spec="1.0+" codebase="http://example/" href="jnlpTest.jnlp">
    <information>
        <title>Some Title</title>
        <vendor>Some Vendor</vendor>
        <homepage href="http://example/" />
        <description>Some Description</description>
    </information>
    <security>
        <all-permissions/>
    </security>
    <resources>
        <j2se version="1.6+" />
        <jar href="jnlpTest.jar" />
    </resources>
    <application-desc main-class="MainClass" />
</jnlp>

Create a jnlpTest.jar from the following:

public class MainClass {
    public static void main(String args[]) {
        Runtime rt = Runtime.getRuntime();
        try {
            //TODO - different exec for Mac
            rt.exec("explorer.exe");
        } catch (IOException e) {
            //exception
        }

    }
}

With a Manifest:

Manifest-Version: 1.0
Main-Class: MainClass

Sign your JNLP jar:

keytool -genkey -keystore testKeys -alias jdc
jarsigner -keystore testKeys jnlpTest.jar jdc

place both the jar and jnlp file on a web server. Make sure the mime type JNLP is served as application/x-java-jnlp-file.

Reference for making a JNLP: http://java.dzone.com/articles/java-web-start-jnlp-hello

Now when a user clicks your jnlp link they will download the jar and be asked if it is ok to run. Running it will cause the explorer window to open. I know it's not the best solution, but any solution will require asking the users permission to execute code on their machine.

Comments