coder97 coder97 - 3 months ago 10
Java Question

Open a downloaded JAR file Mac with Java code

I am creating a simple download program that opens a file with the web browser to download it. On Mac, there is a dialog when you open a downloaded executable JAR file that says you
can't open it because it is from an unidentified developer.
Is there a way to open the JAR file without the dialog by using Java code? Here is my code:

File newFile = new File(System.getProperty("user.home")+"/Library/AppTest/Application.jar");
File file = new File(System.getProperty("user.home")+"/Downloads/AppTest.jar");
try {
Files.move(file.toPath(), newFile.toPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
Desktop.getDesktop().open(file);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


The problem with the code is when it executes, it just shows this dialog, and will not let you open the file. I want to fix this without having to go to settings, because other users may have the same problem.

Answer

To open a JAR file on a mac, use Runtime:

String command = "java -jar path/to/jar/file";
Process p = Runtime.getRuntime().exec(command);

the above code is from this link.

Comments