Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6242649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:59:34+00:00 2026-05-24T11:59:34+00:00

Possible Duplicate: How to launch the default (native) application for a given file from

  • 0

Possible Duplicate:
How to launch the default (native) application for a given file from Java?

I have a java application that opens a file. This works perfect on windows, but not on mac.

The problem here is that I use the windows configuration to open it. The code is:

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

Now my question is what is the code to open it in mac? Or is there another way to open a PDF that works multi platform?

EDIT:

I created the file as followed:

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

in a loop i add them to an array:

fileArray.add(listOfFiles[i]);

If i try to open a file from that array with Desktop.getDesktop().open(file), it says it can’t find that file (the path is messed up because I used ‘./files’ as folder)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:59:35+00:00Added an answer on May 24, 2026 at 11:59 am

    Here is an OperatingSystem Detector:

    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; };
    
    }
    

    Then you can open files like this:

    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;
        }
    }
    

    Answer to your edit:

    Try to use file.getAbsoluteFile() or even file.getCanonicalFile().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Not possible to launch a file on a network using Java Desktop?
Possible Duplicate: Intent to launch the clock application on android I have a widget
Possible Duplicate: How to run a database script file from Delphi? I have a
Possible Duplicate: Launching application from a SMS message launch an app from a link
Possible Duplicate: How do you send email from a Java app using Gmail? How
Possible Duplicate: Determine if running on a rooted device On Launch of the application,
Possible Duplicate: Difference between Convert.tostring() and .tostring() Hi Carrying on from this question What
Possible Duplicate: Use Visual Studio web.config transform for debugging I have an asp.net application
Possible Duplicate: Procedure to submit iPhone application to App Store? I know this question
Possible Duplicate: Can we change the device time using an application? is that possible

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.