If for example I chose to run a bash script that would output (echo) the time e.g. CheckDate.sh
. How could I run this from Java and then print the result of the bash script (the date) in my Java program?
Try this code.
String result = null;
try {
Runtime r = Runtime.getRuntime();
Process p = r.exec("example.bat");
BufferedReader in =
new BufferedReader(new InputStreamReader(p.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
result += inputLine;
}
in.close();
} catch (IOException e) {
System.out.println(e);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With