I'm trying to run an external Program with
Expand|Select|Wrap|Line Numbers
- Process p = Runtime.getRuntime().exec("/bin/sh -c \"/bin/gzip -c /home/user/workspace/TarGz/pics.tar > pics.tar.gz\"");
- CleanStream cleanError = new CleanStream(p.getErrorStream(), "ERROR");
- CleanStream cleanOutput = new CleanStream(p.getInputStream(), "OUTPUT");
- clearError.start();
- clearOutput.start();
- p.waitFor();
Here's the CleanStream.java:
Expand|Select|Wrap|Line Numbers
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- public class ClearStream extends Thread {
- InputStream is;
- String type = null;
- boolean typeSet = false;
- ClearStream(InputStream is)//, String type)
- {
- this.is = is;
- //this.type = type;
- }
- ClearStream(InputStream is, String type)
- {
- this.is = is;
- this.type = type;
- typeSet = true;
- }
- public void run()
- {
- try
- {
- InputStreamReader isr = new InputStreamReader(is);
- BufferedReader br = new BufferedReader(isr);
- String line=null;
- while ( (line = br.readLine()) != null)
- {
- System.out.print("");
- if(typeSet) System.out.println(type + "> " + line);
- }
- }
- catch (IOException ioe)
- {
- ioe.printStackTrace();
- }
- }
- }
Expand|Select|Wrap|Line Numbers
- ERROR> -c: 1: Syntax error: Unterminated quoted string
I tried packing the String into an array, to avoid this, by using exec(String[] cmdarray).
It looks like this then:
Expand|Select|Wrap|Line Numbers
- String[] use = {"/bin/sh -c \"/bin/gzip -c /home/user/workspace/TarGz/pics.tar > pics.tar.gz\""};
- Process p = Runtime.getRuntime().exec(use);
- // same as after here
Expand|Select|Wrap|Line Numbers
- java.io.IOException: Cannot run program "/bin/sh -c "/bin/gzip -c /home/user/workspace/TarGz/pics.tar > pics.tar.gz"": errir=2, No such file or directory
- at java.lang.ProcessBuilder.start(ProcessBuilder.java:459)
- at java.lang.Runtime.exec(Runtime.java:593)
- at java.lang.Runtime.exec(Runtime.java:466)
- at TarSomething.main(TarSomething.java:18)
- Caused by: java.io.IOException: java.io.IOException: error=2, No such file or directory
- at java.lang.UNIXProcess.<init>(UNIXProcess.java:148)
- at java.lang.ProcessImpl.start(ProcessImpl.java:65)
- at java.lang.ProcessBuilder.start(ProcessBuilder.java:452)
- ... 3 more
I've been trying to solve this problem for a while, but without success.
Can anyone help me?
Greetings,
Nepomuk