Last active
January 27, 2023 14:37
-
-
Save jmini/f5e83b894af43db5f555ab492ff3b806 to your computer and use it in GitHub Desktop.
Run a command from a jbang script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
///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