java纯文本的单行区域,如何使用JAVA执行psexec的cmd行并单行登录到文本文件?

I am trying to execute the following line in java(with escaped characters):

"psexec -i -d \\\\computerName -u user -p pass calc 2>

somePath\\psexecOut.txt"

I use the following method to execute cmd lines:

private static String executeCommand(String command) {

StringBuffer output = new StringBuffer();

System.out.println("command is = \n"+command);

Process p;

try {

p = Runtime.getRuntime().exec(command);

p.waitFor();

BufferedReader reader =

new BufferedReader(new InputStreamReader(p.getInputStream()));

String line = "";

while ((line = reader.readLine())!= null) {

output.append(line + "\n");

}

} catch (Exception e) {

e.printStackTrace();

}

return output.toString();

}

The line is executed and calc is starting , but the log part doesn't work. The log file psexecOut.txt is not created.

When I run the command normally (without excaped characters) in cmd it runs fine and the log file is created, but using java it doesn't create the log file.

I suspect that > needs to be escaped but as I read it's already escaped as it is.

How can I execute the psexec with log to text file in a single cmd line using java like I can do manually in windows console ?

解决方案

Solved:

As lit suggested in the comments: cmd.exe /c works.

So the corrected method is:

private static String executeCommand(String command) {

StringBuffer output = new StringBuffer();

System.out.println("command is = \n"+command);

Process p;

try {

p = Runtime.getRuntime().exec("cmd.exe /c "+command); //

p.waitFor();

BufferedReader reader =

new BufferedReader(new InputStreamReader(p.getInputStream()));

String line = "";

while ((line = reader.readLine())!= null) {

output.append(line + "\n");

}

} catch (Exception e) {

e.printStackTrace();

}

return output.toString();

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值