Had an initial go at the above and got a basic dialog box working for Mac and Windows using just standard OS features and without any JNI. Will look at Linux implementation tonight as its a little more tricky due to there being no single dialog utility or method.
Implementation looks like this so far:
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
public class MessageBox {
private static void winAlert(String title, String message) {
// create random file name for script
String filename = "dialog" + (int)(Math.random() * 100000);
File file = null;
try {
// store script in temporary location
file = File.createTempFile(filename, ".vbs");
PrintWriter writer = new PrintWriter(file, "UTF-8");
writer.println("msgbox \"" + message
+ "\"" + ",vbOKOnly + vbSystemModal, "
+ "\"" + title + "\"");
writer.close();
// run script
try {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec("wscript " + file.getAbsolutePath());
process.waitFor();
}
catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
finally {
file.delete(); // clean up script
}
}
private static void macAlert(String title, String message) {
Runtime runtime = Runtime.getRuntime();
String[] args1 = { "osascript",
"-e",
"tell app \"System Events\" to display dialog "
+ "\"" + message + "\""
+ "with title \"" + title + "\""
+ "buttons {\"OK\"}"
+ "default button \"OK\""
};
try {
Process process = runtime.exec(args1);
process.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static void linuxAlert(String title, String message) {
// TODO
}
public static void alert(String title, String message) {
String osName = System.getProperty("os.name");
if (osName.startsWith("Win")) {
winAlert(title, message);
} else if (osName.startsWith("Mac") || osName.startsWith("Darwin")) {
macAlert(title, message);
}
else if (osName.startsWith("Linux") || osName.startsWith("Unix")) {
linuxAlert(title, message);
}
}
public static void main(String[] args) {
alert("Dialog", "HELLO WORLD!");
System.out.println("DONE");
}
}