Bring up Java application as admin privilege on MAC OSX osascript
I need your help to find out where my mistake is :)
public class Main extends JFrame{
public Main() {
EventQueue.invokeLater(new Runnable() {
public void run() {
setVisible(true);
}
});
}
public static void main(String[] args) throws IOException, URISyntaxException {
new Main();
File executor = File.createTempFile("Executor", ".sh");
PrintWriter writer = new PrintWriter(executor, "UTF-8");
writer.println("#!/bin/bash");
writer.println();
writer.println("java -$* > /tmp/output.txt 2>&1 &"); // ***
writer.close();
executor.setExecutable(true); // ***
File elevator = File.createTempFile("Elevator", ".sh");
writer = new PrintWriter(elevator, "UTF-8");
writer.println("#!/bin/bash");
writer.println();
writer.println(String.format("osascript -e \"do shell script \\\"%s $*\\\" with administrator privileges\"",
executor.getPath()));
writer.close();
elevator.setExecutable(true); // ***
Runtime.getRuntime().exec(String.format("%s -cp %s Main param", // ***
elevator.getPath(),
Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()));
}
}
This code assumed it launched the application at a new time with administrator privileges. The administrator password is requested, but after that the application does not start. What for? I have no errors in my release
thank
+3
source to share
1 answer
I know where the problem was! this line:
writer.println("java -$* > /tmp/output.txt 2>&1 &");`
need to remove - before $ *
writer.println("java $* > /tmp/output.txt 2>&1 &");
it works! For anyone looking like me HOW TO ENABLE ADMIN PRIVILEGE FOR YOUR JAVA APPLICATION ON MAC OS X you have the code!
This code works great!
File executor = File.createTempFile("Executor", ".sh");
PrintWriter writer = new PrintWriter(executor, "UTF-8");
writer.println("#!/bin/bash");
writer.println();
writer.println("java $* > /tmp/output.txt 2>&1 &");
writer.close();
executor.setExecutable(true);
File elevator = File.createTempFile("Elevator", ".sh");
writer = new PrintWriter(elevator, "UTF-8");
writer.println("#!/bin/bash");
writer.println();
writer.println(String.format("osascript -e \"do shell script \\\"%s $*\\\" with administrator privileges\"",
executor.getPath()));
writer.close();
elevator.setExecutable(true);
Runtime.getRuntime().exec(String.format("%s -cp %s Main param",
elevator.getPath(),
Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()));
Enjoy!
+1
source to share