0

I am facing the following problem:

I am writing a java-application in Eclipse. Inside my application I want to start a cmd command:

C:/Users/User1/Content-Integration Testing Framework/JDBC Connector/bin/connect -h

The command 'connect -h' is a an enterprise internal application which works fine. If I would use a comand line a would have to chance my current directory like that:

cd C:/Users/User1/Content-Integration Testing Framework/JDBC Connector/bin/

and afterwards I would just type connect -h This works great. But I am not really shure how to execute this command within a java application.

Here they tell me how to run a cmd inside a java application: How to use "cd" command using Java runtime?

But if I do that:

Runtime.getRuntime().exec("'C:/Users/User1/Content-Integration Testing Framework/JDBC Connector/bin/connect' -h");

Eclipse tells me:

java.io.IOException: Cannot run program "'C:/Users/User1/Content-Integration": CreateProcess error=2, Das System kann die angegebene Datei nicht finden
    at java.lang.ProcessBuilder.start(Unknown Source)

It cuts my command at "Content-Integration".

can someone help me please?

Community
  • 1
  • 1
Metalhead89
  • 1,740
  • 9
  • 30
  • 58

3 Answers3

2

You should use the version of exec() that takes multiple args via a String array.

Runtime.exec(String s) will split your string using a tokenizer (this is why quoting the string won't work, and why you see the behaviour you do). If you resolve the executable and arguments yourself, and pass each as an array element in the above e.g.

String[] args = new String[]{"executable", "arg1", "arg2"};
Runtime.getRuntime().exec(args); // don't forget to collect stdout/err etc.

then you will bypass Runtime.exec(String s)'s splitting behaviour.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • Just tried with the solution I propose (including spaces in the path) and it works as expected... – assylias Jul 30 '12 at 09:04
  • Ok thank you very much for your replay. The first problem is solved. I typed the following: Process p = Runtime.getRuntime().exec(new String[]{"C:/Users/User1/'Content-Integration Testing Framework/JDBC Connector/bin/","connect","-h"}); It does not split my first argument but now there is a similar problem: java.io.IOException: Cannot run program "C:/Users/User1/'Content-Integration Testing Framework/JDBC Connector/bin/": CreateProcess error=2, Das System kann die angegebene Datei nicht finden – Metalhead89 Jul 30 '12 at 09:04
  • 1
    @Metalhead89 There seems to be an extra quote `'` after `User1` – assylias Jul 30 '12 at 09:07
  • I did a mistake: there was an "'" in between but it still does not works: it tells me: java.io.IOException: ......: CreateProcess error=5, Zugriff verweigert – Metalhead89 Jul 30 '12 at 09:08
  • @Metalhead89 You probably meant: `Process p = Runtime.getRuntime().exec(new String[]{"C:/Users/User1/Content-Integration Testing Framework/JDBC Connector/bin/connect","-h"})` (with `connect` in the first string) – assylias Jul 30 '12 at 09:10
  • Thats a good question. When I type the command into the commandline I first change to '.../ bin'. Afterwars I type connect -h. I am not shure if I should put 'connect' inside the first string or not. I just did that and then it still tells me: 'CreateProcess error=2, Das System kann die angegebene Datei nicht finden' But if i have three strings (path, connect and -h) it tells me: Access denied – Metalhead89 Jul 30 '12 at 09:15
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/14619/discussion-between-metalhead89-and-assylias) – Metalhead89 Jul 30 '12 at 09:31
1

Have you tried:

Runtime.getRuntime().exec("\"C:/Users/User1/Content-Integration Testing Framework/JDBC Connector/bin/connect\" -h");
assylias
  • 321,522
  • 82
  • 660
  • 783
0

This happens because your path contains spaces. Make sure to wrap it in "" and it will work.

Runtime.getRuntime().exec("\"C:/Users/User1/Content-Integration Testing Framework/JDBC Connector/bin/connect\" -h");
buxter
  • 583
  • 4
  • 14