-2
Process log_remover = Runtime.getRuntime().exec("echo \"bleh\" > test.txt");
log_remover.waitFor();
log_remover.destroy();

this does nothing

Process node_creation = Runtime.getRuntime().exec("cp -r ../HLR"+String.valueOf(count-1)+" ../HLR"+String.valueOf(count));
node_creation.waitFor();
node_creation.destroy();

however this works :S

0

3 Answers 3

1

The redirection works only if a shell is used. Runtime.exec() does not use a shell.

See Java Executing Linux Command

Sign up to request clarification or add additional context in comments.

1 Comment

hmmm yeah true.. thanks man.. the link helped out
1

Redirection is handled by a shell, and you're not invoking a shell here, so you can't use redirection. Something like this, on the other hand, would work:

Runtime.getRuntime().exec(new String[] {"sh",  "-c", "echo 'bleh' > text.txt"});

Note I've changed this to use the form of exec() that takes an array of Strings, as properly tokenizing quoted strings on a command line is something else that only the shell can do!

3 Comments

y have u put a -c @Ernest Friedman-Hill? if its for shell im using a korn shell
Process log_remover = Runtime.getRuntime().exec("log_remover.sh"); Why isnt this working
I don't know. Is log_remover.sh executable? Is it on your PATH ?
0

Classic mistake I've seen many times before...

The first argument to Runtime.getRuntime().exec() is the executable, so your code is trying to execute a command literally called echo \"bleh\" > test.txt, but it should be trying to execute echo. Arguments to executables are passed in after the executable, like this:

Runtime.getRuntime().exec("echo", new String[]{"bleh"});

Redirecting output is another story, because the *nix operator > is a shell thing. To replicate this in java you have to get the outputstream of the command and pump it to the inputstream of another process

3 Comments

find ./HLR3/LOGS -name '*.txt' -exec rm {} \; how can i execute this @Bohemian
Process log_remover = Runtime.getRuntime().exec("log_remover.sh"); this doesnt work
use the absolute path, eg Runtime.getRuntime().exec("/usr/bin/log_remover.sh") or whatever it is. The java exec environment isn't the same as your shell.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.