我正在尝试从Java exec方法执行cf命令,而不是手动从linux终端输入。我从终端传递了我所做的确切语法然而,当将第一个参数传递给方法时,我得到错误,因为它包含空格我不知道如何调试它?
直接从Linux终端
root@devopsops:/home/Data/free# cf create-service 'IBM Analytics for Hadoop' Free 'IBM_Analytics_for_Hadoop'_APP
Creating service IBM_Analytics_for_Hadoop_APP in org All_Testing / space monitor-ms1 as xyz@gmail.com...
OK
...
如果我从java中的exec方法运行相同的命令,我会收到以下错误
cf create-service 'IBM Analytics for Hadoop' Free 'IBM_Analytics_for_Hadoop'_APP
Here is the standard output of the command:
FAILED
Incorrect Usage.
NAME:
create-service - Create a service instance
ALIAS:
cs
USAGE:
cf create-service SERVICE PLAN SERVICE_INSTANCE
Java代码
主要方法
obj.cfCreateService("'IBM Analytics for Hadoop'","Free");
cfCreateService方法
public String cfCreateService(String servicename, String planname){
String s = null;
StringBuffer log = new StringBuffer("");
try {
String appname= servicename.replaceAll(" ", "_")+"_APP";
String command="cf create-service "+servicename+" "+ planname +" "+ appname;
System.out.println(command);
Process p = Runtime.getRuntime().exec(command);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(p.getErrorStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
log.append(s+"\n");
}
}
}
答案 0 :(得分:0)
你正在做的事情是正确的,但也许你可以尝试将参数括在exec()的数组参数中:
Process p = Runtime.getRuntime().exec(new String[]{"cf", "create-service", servicename, planname, appname});
而不是
String command="cf create-service "+servicename+" "+ planname +" "+ appname;
Process p = Runtime.getRuntime().exec(command);