Romain-p
Romain-p

Reputation: 465

Elevate your java app as Admin privilege on MAC OSX by osascript

I need your help to know where is my error please :)

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 suppose run the application a new time with admin privileges. The admin password is asked but after, the application doesn't run. Why ? I have no error in my output

Thank you

Upvotes: 3

Views: 1233

Answers (1)

Romain-p
Romain-p

Reputation: 465

I know where was the me problem! this line:

writer.println("java -$* > /tmp/output.txt 2>&1 &");`

have to remove - before $*

writer.println("java $* > /tmp/output.txt 2>&1 &");

it works ! For everybody which searched like me to HOW TO ENABLE ADMIN PRIVILEGE FOR YOUR JAVA APPLICATION ON MAC OS X, you have now the code !

This code works perfectly!

        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!

Upvotes: 1

Related Questions