ProcessBuilder不适用于参数 [英] ProcessBuilder does not work with argument

查看:61
本文介绍了ProcessBuilder不适用于参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,它单击表中带有用户计算机名称的单元格中的一个功能,在命令行中,将调用一个外部程序以远程连接到计算机.该命令如下所示:

I'm writing a program, one of the functions it clicking on a cell in the table with the name of the computer of a user, in the command line, a external program is called to connect to the computer remotely. The command looks like this:

"C:\Program Files\Vnc\MegaVNC\vncviewer.exe" /user vncadmin /password xxxxxx /server comp-01

"C:\ Program Files \ Vnc \ MegaVNC \ vncviewer.exe"是文件的位置,并带有"/user vncadmin/password xxxxxx/server comp-01"参数.请注意,论点不带引号.当您从命令行调用此命令时,一切正常.

"C:\Program Files\Vnc\MegaVNC\vncviewer.exe" is the location of the file, and "/ user vncadmin /password xxxxxx /server comp-01" argument. Please note that argument is written without quotes. When you call this command from the command line, everything works.

我在ProcessBuilder中的代码:

My code with ProcessBuilder:

ProcessBuilder pb = new ProcessBuilder("C:/Program Files/Vnc/MegaVNC/vncviewer.exe ",  "/user vncadmin /password xxxxxx /server comp-01").start();

但是它不起作用.如果删除该参数,则将打开被调用的程序.但是,当使用参数进行初始化时,程序本身会挂在参数上,并且连接会崩溃.我尝试了以下方法:

But it does not work. If you remove the argument, the called program is opened. But when initializing with an argument, the program itself hangs on the argument and the connection crashes. I tried the following:

ProcessBuilder pb = new ProcessBuilder("C:/Program Files/Vnc/MegaVNC/vncviewer.exe", "/user vncadmin ", "/password xxxxxx ", "/server comp-01").start();

它也不起作用.我猜问题出在斜线或空格的错误解释编译器中.有什么想法吗?

It does not work either. I guess that the problem is in the wrong interpretation compiler of the slash or spaces. Any thoughts?

推荐答案

您应将所有命令行参数作为单独的参数传递给流程生成器.否则,它们在内部被引用,因此被调用程序将其解释为单个参数.

You should pass all command line arguments as separate parameters to the process builder. Otherwise, they are internally quoted and therefore interpreted as a single argument by the called program.

ProcessBuilder pb = new ProcessBuilder("C:/Program Files/Vnc/MegaVNC/vncviewer.exe", "/user", "vncadmin", "/password", "xxxxxx", "/server", "comp-01").start();

我创建了一个愚蠢的批处理文件"C:/Temp/test.bat":

I created a stupid batch "C:/Temp/test.bat":

@echo off
set argCount=0
for %%x in (%*) do (
   set /A argCount+=1
   echo %%x
)
echo Number of processed arguments: %argCount%

使用 new ProcessBuilder("C:/Temp/test.bat","a b").start(); ,我得到结果:

"a b"
Number of processed arguments: 1

使用 new ProcessBuilder("C:/Temp/test.bat","a","b").start(); ,我得到结果:

a
b
Number of processed arguments: 2

这篇关于ProcessBuilder不适用于参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆