Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run dmg on mac through java

Tags:

java

macos

Please let me know how to run / invoke .dmg application in Java. I used below syntax Runtime.getruntime.exec("sample.dmg") but it throws an error as "permission denied" with error code 13. Please let me know how to invoke dmg.

like image 837
santosh Avatar asked Dec 05 '25 10:12

santosh


2 Answers

A DMG file is not directly executable. You need to use the Mac OSX open command:

Runtime.getRuntime().exec(new String[] { "/usr/bin/open", "sample.dmg" });

You may need to have the full path to the dmg file but otherwise it seems to work fine for me. Open also let's you specify the application to do the opening so you can do cool things like send this document in the Mail application as an attachment:

Runtime.getRuntime().exec(
    new String[] { "/usr/bin/open", "-a", "Mail", "letter.pdf" });

I use open from the Mac command like this all of the time.

like image 123
Gray Avatar answered Dec 08 '25 02:12

Gray


You should use Desktop.open(File file) which will use the appropriate opener for the OS. In your case on a Mac it will use open. This is better than Runtime.exec as you're not relying the exact location of the open command. Furthermore, this would even work on Windows if it had a DMG mounting application, such as Mediafour's MacDrive.

The bottom line is use Desktop.open(File file) to open a file with the operating system's default opener, that's what it's there for. Most people only use Runtime.exec as a last resort.

like image 45
Steve Kuo Avatar answered Dec 08 '25 03:12

Steve Kuo