I am trying to run mdb-export on a file I know exist in that directory. But it does not seem to execute. "ls-l" will so I am sure that the java code is working. The command will execute perfectly from bash.
The failing command is
/usr/bin/mdb-export -Q -d ';' -D '%Y-%m-%d %H:%M:%S' /home/jocke/viking.mdb resultat >> resultat.csv
private void runCommand() {
try {
String workingdirectory=System.getProperty("user.dir");
Runtime runtime = Runtime.getRuntime();
//Process process = runtime.exec("/usr/bin/mdb-export -Q -d ';' -D '%Y-%m-%d %H:%M:%S' /home/jocke/viking.mdb resultat >> resultat.csv");
Process process = runtime.exec("/usr/bin/mdb-export /home/jocke/viking.mdb resultat >> resultat.csv");
//
process.waitFor();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
asked Apr 9, 2014 at 7:49
user2130951
2,7514 gold badges35 silver badges59 bronze badges
-
Don't use 'exec(String)', use exec(String[])'.user207421– user2074212014年04月09日 08:02:59 +00:00Commented Apr 9, 2014 at 8:02
1 Answer 1
You cannot use output redirection this way. Use a ProcessBuilder instead:
ProcessBuilder pb = new ProcessBuilder("/usr/bin/mdb-export", "/home/jocke/viking.mdb", "resultat");
File csv = new File("resultat.csv");
pb.redirectOutput(Redirect.appendTo(csv);
Process p = pb.start();
answered Apr 9, 2014 at 8:02
Drunix
3,3438 gold badges32 silver badges50 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
user2130951
Still can't get it to work. Just getting exitCode = 1. But I just made an export.sh and run that instead. Works like expected.
user2130951
Seems like it's this part that creates the problem. -Q -d ';' -D '%Y-%m-%d %H:%M:%S'
default