Hi!
I'm trying to run an external Program with
[CODE=java]
Process p = Runtime.getRunt ime().exec("/bin/sh -c \"/bin/gzip -c /home/user/workspace/TarGz/pics.tar > pics.tar.gz\"") ;
CleanStream cleanError = new CleanStream(p.g etErrorStream() , "ERROR");
CleanStream cleanOutput = new CleanStream(p.g etInputStream() , "OUTPUT");
clearError.star t();
clearOutput.sta rt();
p.waitFor();
[/CODE]under Linux.
Here's the CleanStream.jav a:
[CODE=java]
import java.io.Buffere dReader;
import java.io.IOExcep tion;
import java.io.InputSt ream;
import java.io.InputSt reamReader;
public class ClearStream extends Thread {
InputStream is;
String type = null;
boolean typeSet = false;
ClearStream(Inp utStream is)//, String type)
{
this.is = is;
//this.type = type;
}
ClearStream(Inp utStream is, String type)
{
this.is = is;
this.type = type;
typeSet = true;
}
public void run()
{
try
{
InputStreamRead er isr = new InputStreamRead er(is);
BufferedReader br = new BufferedReader( isr);
String line=null;
while ( (line = br.readLine()) != null)
{
System.out.prin t("");
if(typeSet) System.out.prin tln(type + "> " + line);
}
}
catch (IOException ioe)
{
ioe.printStackT race();
}
}
}
[/CODE]The command works fine, when used within bash, but from Java I get the error:
I checked the Runtime API and found, that exec(String command) has the token parsed with a StringTokenizer , which would explain, that the last quotation mark isn't found.
I tried packing the String into an array, to avoid this, by using exec(String[] cmdarray).
It looks like this then:
[CODE=java]
String[] use = {"/bin/sh -c \"/bin/gzip -c /home/user/workspace/TarGz/pics.tar > pics.tar.gz\""} ;
Process p = Runtime.getRunt ime().exec(use) ;
// same as after here
[/CODE]The error here is even worse:
(Line 18 in TarSomething is Process p = Runtime.getRunt ime().exec(use) ;)
I've been trying to solve this problem for a while, but without success.
Can anyone help me?
Greetings,
Nepomuk
I'm trying to run an external Program with
[CODE=java]
Process p = Runtime.getRunt ime().exec("/bin/sh -c \"/bin/gzip -c /home/user/workspace/TarGz/pics.tar > pics.tar.gz\"") ;
CleanStream cleanError = new CleanStream(p.g etErrorStream() , "ERROR");
CleanStream cleanOutput = new CleanStream(p.g etInputStream() , "OUTPUT");
clearError.star t();
clearOutput.sta rt();
p.waitFor();
[/CODE]under Linux.
Here's the CleanStream.jav a:
[CODE=java]
import java.io.Buffere dReader;
import java.io.IOExcep tion;
import java.io.InputSt ream;
import java.io.InputSt reamReader;
public class ClearStream extends Thread {
InputStream is;
String type = null;
boolean typeSet = false;
ClearStream(Inp utStream is)//, String type)
{
this.is = is;
//this.type = type;
}
ClearStream(Inp utStream is, String type)
{
this.is = is;
this.type = type;
typeSet = true;
}
public void run()
{
try
{
InputStreamRead er isr = new InputStreamRead er(is);
BufferedReader br = new BufferedReader( isr);
String line=null;
while ( (line = br.readLine()) != null)
{
System.out.prin t("");
if(typeSet) System.out.prin tln(type + "> " + line);
}
}
catch (IOException ioe)
{
ioe.printStackT race();
}
}
}
[/CODE]The command works fine, when used within bash, but from Java I get the error:
Code:
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:
[CODE=java]
String[] use = {"/bin/sh -c \"/bin/gzip -c /home/user/workspace/TarGz/pics.tar > pics.tar.gz\""} ;
Process p = Runtime.getRunt ime().exec(use) ;
// same as after here
[/CODE]The error here is even worse:
Code:
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
Comment