Want to invoke a linux shell command from Java?

Exec does not execute a command in your shell.

Exec does not execute a command in your shell try Process p = Runtime.getRuntime(). Exec(new String{"csh","-c","cat /home/narek/pk. Txt"}); instead.

EDIT:: I don't have csh on my system so I used bash instead. The following worked for me Process p = Runtime.getRuntime(). Exec(new String{"bash","-c","ls /home/XXX"}).

Doesn't work, sorry – Narek Sep 11 '09 at 13:34 @Narek. Sorry about that. I fixed it by removing the extra \" 's Apparently they aren't needed.

I don't have csh on my system, but it works with bash. – KitsuneYMG Sep 11 '09 at 15:46 1 As others has mentioned this should be independent wether you have csh or bash, isn't it? – Narek Sep 17 '09 at 5:33 @Narek.It should, but I don;'t know how csh handles arguments.

– KitsuneYMG Sep 17 '09 at 12:38 won't work with spaces. – Stephan Kristyn Jul 9 at 23:54.

Use ProcessBuilder to separate commands and arguments instead of spaces. This should work regardless of shell used: import java.io. BufferedReader; import java.io.

File; import java.io. IOException; import java.io. InputStreamReader; import java.util.

ArrayList; import java.util. List; public class Test { public static void main(final String args) throws IOException, InterruptedException { //Build command List commands = new ArrayList(); commands. Add("/bin/cat"); //Add arguments commands.

Add("/home/narek/pk. Txt"); System.out. Println(commands); //Run macro on target ProcessBuilder pb = new ProcessBuilder(commands); pb.

Directory(new File("/home/narek")); pb. RedirectErrorStream(true); Process process = pb.start(); //Read output StringBuilder out = new StringBuilder(); BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream())); String line = null, previous = null; while ((line = br.readLine())! = null) if (!line.

Equals(previous)) { previous = line; out. Append(line). Append('\n'); System.out.

Println(line); } //Check result if (process.waitFor() == 0) System.out. Println("Success! "); System.

Exit(0); //Abnormal termination: Log command parameters and output and throw ExecutionException System.err. Println(commands); System.err. Println(out.toString()); System.

Exit(1); } }.

Even after java.util. *; is properly imported I can't get above example to run. – Stephan Kristyn Jul 10 at 0:03 What does not work for you?

– Tim Jul 10 at 16:01 @Stephan I've updated the example code above to remove the logging statements and variables that were declared outside the pasted code, and it should now compile & run. Feel free to try and let me know how it works out. – Tim Jul 11 at 10:16.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions