0

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
1
  • Don't use 'exec(String)', use exec(String[])'. Commented Apr 9, 2014 at 8:02

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Seems like it's this part that creates the problem. -Q -d ';' -D '%Y-%m-%d %H:%M:%S'

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.