Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Runtime's exec() method is not redirecting the output

Tags:

Process p = Runtime.getRuntime().exec("sh somescript.sh &> out.txt"); 

I am running this command using Java. The script is running but it's not redirecting its stream to the file. Moreover, the file out.txt is not getting created.

This script runs fine if I run it on shell.

Any ideas?

like image 842
user2110167 Avatar asked Apr 26 '13 14:04

user2110167


1 Answers

You need to use ProcessBuilder to redirect.

ProcessBuilder builder = new ProcessBuilder("sh", "somescript.sh"); builder.redirectOutput(new File("out.txt")); builder.redirectError(new File("out.txt")); Process p = builder.start(); // may throw IOException 
like image 90
johnchen902 Avatar answered Oct 20 '22 19:10

johnchen902