If this is an interactive program then using authopen would be perfect, otherwise I would use launchd.
I just noticed that this answer had been deleted. Why would someone delete the right answer?
Here is a simplified example to illustrate the use of authopen to answer the question:
package macos4;
import java.io.File;
import java.io.IOException;
public class MacOS4 {
public static void main(String[] args) throws IOException, InterruptedException {
String destinationFileName = "/Library/Application Support/tempFile/";
ProcessBuilder builder = new ProcessBuilder("/usr/libexec/authopen", "-c", "-w", "-a", destinationFileName);
builder.redirectInput(new File("src/resources", "input.txt"));
builder.redirectError(ProcessBuilder.Redirect.INHERIT);
builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
Process process = builder.start();
process.waitFor();
process.destroy();
}
}
This will create tempFile if it does not exist and write to it the contents of input.txt from the resources package. It will pop up for authentication:
which means there is no need to pipe the password to the subprocess.
For very important considerations regarding the need to prevent external processes to block on IO buffers see this.
What if you want your job to run unattended? Time for launchd, but keep in mind that the entire program will run as root, unlike the interactive option, which I posted above, where only the file operations run as root. I wrote this simple program that I want to run every 5 minutes. Every time it runs it will add a line to "/Library/Application Support/tempFile" with the current date and time; every 30 minutes it will overwrite the file and start over, but keeping just the last timestamp. Here is the program:
package maclaunchd;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.attribute.FileTime;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MacLaunchd {
public static void main(String[] args) {
try {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date = new Date();
File tempFile = new File("/Library/Application Support/tempFile");
if (!tempFile.exists()) {
tempFile.createNewFile();
}
FileTime creationTime = (FileTime) Files.getAttribute(tempFile.toPath(), "creationTime");
boolean append = (System.currentTimeMillis() - creationTime.toMillis() <= 1800000);
FileWriter fw = new FileWriter(tempFile, append);
try (BufferedWriter bw = new BufferedWriter(fw)) {
bw.write(sdf.format(date) + "\n");
}
} catch (IOException ex) {
Logger.getLogger(MacLaunchd.class.getName()).log(Level.SEVERE, null, ex);
throw new RuntimeException(ex);
}
}
}
Then I created a launchd job definition which I called “maclaunchd.MacLaunchd.daemon.plist”. Here it is:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>GroupName</key>
<string>wheel</string>
<key>KeepAlive</key>
<false/>
<key>Label</key>
<string>maclaunchd.MacLaunchd.daemon</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/java</string>
<string>-jar</string>
<string>/Users/enta/NetBeansProjects/MacLaunchd/dist/MacLaunchd.jar</string>
</array>
<key>RunAtLoad</key>
<false/>
<key>StandardErrorPath</key>
<string>/Users/enta/NetBeansProjects/MacLaunchd/dist/err.log</string>
<key>StandardOutPath</key>
<string>/Users/enta/NetBeansProjects/MacLaunchd/dist/out.log</string>
<key>StartInterval</key>
<integer>300</integer>
<key>UserName</key>
<string>root</string>
<key>WorkingDirectory</key>
<string>/Users/enta/NetBeansProjects/MacLaunchd/dist/</string>
</dict>
</plist>
Next I copied the file to the right place, loaded it and started it:
sudo cp maclaunchd.MacLaunchd.daemon.plist /Library/LaunchDaemons
sudo launchctl load /Library/LaunchDaemons/maclaunchd.MacLaunchd.daemon.plist
sudo launchctl start maclaunchd.MacLaunchd.daemon
If you cat "/Library/Application Support/tempFile" you will see the date being written to it every 5 minutes. To stop, unload and remove the job run:
sudo launchctl stop maclaunchd.MacLaunchd.daemon
sudo launchctl unload /Library/LaunchDaemons/maclaunchd.MacLaunchd.daemon.plist
sudo rm /Library/LaunchDaemons/maclaunchd.MacLaunchd.daemon.plist
Now you have two options to write to "/Library/Application Support/”.