简体   繁体   English

通过Java代码运行.dmg文件

[英]Run a .dmg file through java code

如何在OS X下通过Java代码挂载.dmg文件?

This will work (but only on OS X): 这将起作用(但仅在OS X上有效):

    try {
        String[] command = {"/usr/bin/hdiutil", "attach", "/Users/path/to/your.dmg"};
        String sendback = "";    
        Process proc = Runtime.getRuntime().exec(command);
        InputStream istr = proc.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(istr));
        String str;
        while ((str = br.readLine()) != null) {
            sendback = sendback + str;
        }
        int resultCode = proc.waitFor();
        br.close();
        if (resultCode != 0) {
            throw new Exception("failed to open system profiler");
        }
    }
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

DMGs are not executables, are apple diskimages DMG不是可执行文件,而是苹果磁盘映像

reference is here 参考在这里

thus you can't "run" it. 因此您无法“运行”它。

You can use a Process , of course. 您当然可以使用Process But it won't work anywhere except on a Mac. 但是,除了在Mac上,它在任何地方都无法使用。

Saying that is like asking how to run an ISO file through Java. 这样说就像问如何通过Java运行ISO文件。 You simply can't; 你根本做不到; it's a disk image. 这是一个磁盘映像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM