Java: open file (Windows + Mac)

Possible duplicate:
How to run a standard (native) application for a given file with Java?

I have a Java application that opens a file. This works fine on windows, but not on Mac.

The problem is that I am using the window configuration to open it. The code:

Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + file);

Now my question is, what is the code to open it on a Mac? Or is there another way to open a PDF file that works on multiple platforms?

EDIT:

I created the file as follows:

File folder = new File("./files");
File[] listOfFiles = folder.listFiles();

in a loop, I add them to the array:

fileArray.add(listOfFiles[i]);

If I try to open a file from this array using Desktop.getDesktop (). open (file), it says that it cannot find this file (the path is corrupted because I used "./files" as a folder)

+5
3

:

public class OSDetector
{
    private static boolean isWindows = false;
    private static boolean isLinux = false;
    private static boolean isMac = false;

    static
    {
        String os = System.getProperty("os.name").toLowerCase();
        isWindows = os.contains("win");
        isLinux = os.contains("nux") || os.contains("nix");
        isMac = os.contains("mac");
    }

    public static boolean isWindows() { return isWindows; }
    public static boolean isLinux() { return isLinux; }
    public static boolean isMac() { return isMac; };

}

:

public static boolean open(File file)
{
    try
    {
        if (OSDetector.isWindows())
        {
            Runtime.getRuntime().exec(new String[]
            {"rundll32", "url.dll,FileProtocolHandler",
             file.getAbsolutePath()});
            return true;
        } else if (OSDetector.isLinux() || OSDetector.isMac())
        {
            Runtime.getRuntime().exec(new String[]{"/usr/bin/open",
                                                   file.getAbsolutePath()});
            return true;
        } else
        {
            // Unknown OS, try with desktop
            if (Desktop.isDesktopSupported())
            {
                Desktop.getDesktop().open(file);
                return true;
            }
            else
            {
                return false;
            }
        }
    } catch (Exception e)
    {
        e.printStackTrace(System.err);
        return false;
    }
}

:

file.getAbsoluteFile() file.getCanonicalFile().

+12

, *.dll, - windows-ish.

, Linux, MAC:

import java.awt.Desktop;
import java.io.File;

Desktop d = Desktop.getDesktop();  
d.open(new File("foo.pdf"))
+10

open,

Runtime.getRuntime().exec("/usr/bin/open " + file);

Martijn
, :

Runtime.getRuntime().exec(new String[]{"/usr/bin/open", file.getAbsolutePath()});
+3

All Articles