problems with runtime.exec
843798Nov 20 2005 — edited Nov 21 2005Hi,
I have a problem with runtime.exec() when trying to execute db2 commands on Windows XP. My code looks like this (my source is "When runtime.exec won't" - http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html):
import java.util.*;
import java.io.*;
public class batch
{
public static void main(String args[])
{
try
{
Runtime rt = Runtime.getRuntime();
String mycom[]={"db2cmd",
"db2 connect to testdb2 user db2admin using Start$001",
"db2 terminate"
};
Process proc = rt.exec(mycom);
InputStream stderr = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
System.out.println("<ERROR>");
while ( (line = br.readLine()) != null)
System.out.println(line);
System.out.println("</ERROR>");
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
} catch (Throwable t)
{
t.printStackTrace();
}
}
}
The output is:
SQL0104N An unexpected token "db2" was found following "<identifier>".
Expected tokens may include: "NEW". SQLSTATE=42601
I think that the trouble is with runtime.exec since the first two commands are well executed and the third one doesn't (even if I change the commands). I can't even execute a sequence of most simple DOS commands like "cmd.exe" or "dir". I get the same problem when I try other examples from "When runtime.exec won't". I would appreciate any help because I am relatively inexperienced with Java. Unfortunately I can't use JDBC because I want to import a file (a DB2 command) and the JDBC only supports pure SQL commads.
Thanks a lot,
Artur