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

2

24
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?

Soapbark answered 26/4, 2013 at 14:17 Comment(8)
why is that ampersand(&) ?Reservation
its for redirecting both output stream as well as error stream to the fileSoapbark
have you searched your system for the file?Klee
1) Read the runtime.exec info. page. Implement all the recommendations in the linked Java World article. 2) Then ignore it refers to exec & use a ProcessBuilder(String[]) constructor. 3) I heard something about pipes not working in a Java process.Glaucescent
@PhilippSander Yes its not getting created .the command sh somescript.sh is running anywaysSoapbark
Are you sure it's not being created? Did you check the directory in System.getProperty("user.dir"); ?Wrens
can you be sure that it is not created? maybe it's just not in the directories where you think it should be.... maybe it's in an other directoryKlee
Pipes (and redirects) wouldn't usually work on UNIX because they are things that the shell does. In this case sh is being executed. The behaviour of Runtime.exec is largely undocumented.Finalize
P
47

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
Presumption answered 26/4, 2013 at 14:29 Comment(8)
It works absolutely fine.Thanks ..Are there any references where i can read a bit more about this ,i have to do a lot of things on shell using java.So it will be useful for meSoapbark
@Soapbark API document about ProcessBuilderPresumption
One More thing, if i want to add some options into the command like somecommand -o "outputdirectory" -i "inputfilelist" ,how can i add -o and -i options?Soapbark
@Soapbark new ProcessBuilder("somecommand", "-o", "outputdirectory", "-i", "inputfilelist");Presumption
@Presumption I wrote the code as per you have stated .even when we are redirecting both the streams into a file ,there is some output which stays missing .if i run "sh somescript.sh &> out.txt" on shell ,the out.txt file gets each output written by the script,but using process builder and redirecting both the inputs,then it gives output equivalent to writing sh somescript.sh > out.txt .. are we missing anything in the code ..And the script is actually a perl script which calls java functions internally.Soapbark
for me can not resolve method redirectOutput and redirectError...:(Gridley
I would think that it wouldn't work to redirect both output streams to the same file in this way. I would expect two attempts to open the file to occur, and one of them. to fail. Rather, I'd think that you'd specify one and then have a way. of saying to redirect the other one to the one being sent to the file, much like >blah.out 2>&1.Coakley
@Presumption , the call to redirectError() should have a different filename than the one for redirectOutput(). Could you change the answer to have this line?: builder.redirectError(new File("err.txt"));Provitamin
B
7

When you run a command, there is no shell running and any shell commands or functions are not available. To use something like &> you need a shell. You have one but you are not passing it to it. try instead.

Runtime.getRuntime().exec(new String[] { "sh", "somescript.sh &> out.txt" });
Brake answered 26/4, 2013 at 14:25 Comment(3)
That's not going to compile, is it?Finalize
there is no overloaded exec function which takes such inputs,because its giving compile errorsSoapbark
@commentators: Runtime.getRuntime().exec(new String[] { "sh", "somescript.sh &> out.txt" });Kamenskuralski

© 2022 - 2024 — McMap. All rights reserved.