Skip to content

Instantly share code, notes, and snippets.

@ebabel
Created September 2, 2015 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ebabel/173666bb5e68ff232e13 to your computer and use it in GitHub Desktop.
Save ebabel/173666bb5e68ff232e13 to your computer and use it in GitHub Desktop.
If launch via vnc, it will launch the GUI app "Mail"
package com.company;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
new Thread(new Runnable() {
@Override
public void run() {
try {
while (true) {
runLoop();
Thread.sleep(1000 * 10);
// Thread.sleep(1000*60*20);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}).start();
}
private static void runLoop() throws IOException, InterruptedException {
File file = new File(".doRunForRunMailJar");
System.out.println("does file exist? " + file.exists());
if ( file.exists() ) {
file.delete();
runProcess("killall Mail");
Thread.sleep(1000*5);
runProcess("open /Applications/Mail.app");
}
}
private static void runProcess(final String process) throws IOException, InterruptedException {
System.out.println("Running process: " + process);
// http://stackoverflow.com/a/14168097/247325
final Process p = Runtime.getRuntime().exec(process);
inheritIO(p.getInputStream(), System.out);
inheritIO(p.getErrorStream(), System.err);
p.waitFor();
System.out.println("Done running process: " + process);
}
private static void inheritIO(final InputStream src, final PrintStream dest) {
new Thread(new Runnable() {
public void run() {
Scanner sc = new Scanner(src);
while (sc.hasNextLine()) {
dest.println(sc.nextLine());
}
}
}).start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment