Java执行es命令_从Java运行Shell命令

我在一个应用程序上工作,但有一个关于从Java应用程序运行shell命令的问题。这是代码:

public String execRuntime(String cmd) {

Process proc = null;

int inBuffer, errBuffer;

int result = 0;

StringBuffer outputReport = new StringBuffer();

StringBuffer errorBuffer = new StringBuffer();

try {

proc = Runtime.getRuntime().exec(cmd);

} catch (IOException e) {

return "";

}

try {

response.status = 1;

result = proc.waitFor();

} catch (InterruptedException e) {

return "";

}

if (proc != null && null != proc.getInputStream()) {

InputStream is = proc.getInputStream();

InputStream es = proc.getErrorStream();

OutputStream os = proc.getOutputStream();

try {

while ((inBuffer = is.read()) != -1) {

outputReport.append((char) inBuffer);

}

while ((errBuffer = es.read()) != -1) {

errorBuffer.append((char) errBuffer);

}

} catch (IOException e) {

return "";

}

try {

is.close();

is = null;

es.close();

es = null;

os.close();

os = null;

} catch (IOException e) {

return "";

}

proc.destroy();

proc = null;

}

if (errorBuffer.length() > 0) {

logger

.error("could not finish execution because of error(s).");

logger.error("*** Error : " + errorBuffer.toString());

return "";

}

return outputReport.toString();

}

但是当我尝试执行命令时:

/export/home/test/myapp -T "some argument"

myapp读取"some argument" 为两个单独的参数。但是我只想读取"some argument"一个参数。

当我直接从终端运行此命令时,它执行成功。 我试过'"some argument"',""some argument"","some\

argument" 但对我没有工作。我如何才能将此参数作为一个参数读取。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值