Skip to content

Instantly share code, notes, and snippets.

@jmini
Last active January 27, 2023 14:37
Show Gist options
  • Save jmini/f5e83b894af43db5f555ab492ff3b806 to your computer and use it in GitHub Desktop.
Save jmini/f5e83b894af43db5f555ab492ff3b806 to your computer and use it in GitHub Desktop.
Run a command from a jbang script
///usr/bin/env jbang "$0" "$@" ; exit $?
import java.io.File;
public class ExecMain {
public static void main(String... args) throws Exception {
File directory = new File(System.getProperty("user.home"));
System.out.println("List files in " + directory);
exec(directory, "ls", "-a", "-l");
}
public static void exec(File directory, String... command) throws Exception {
Process process = new ProcessBuilder()
.command(command)
.directory(directory)
.inheritIO()
.start();
int exitCode = process.waitFor();
System.out.println("Exit code: " + exitCode);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment