gpt4 book ai didi

java - 错误: Cannot open VM: "[datastore] myVM1/myVM1.vmx",未知文件后缀

转载 作者:太空宇宙 更新时间:2023-11-04 08:05:14 29 4
gpt4 key购买 nike

我在 Java 中通过 ProcessBuilder 运行 vmrun 时遇到很多困难。

我有一个像这样的命令字符串:

java -cp . RunTest 'vmrun -T esx -h https://10.123.236.123:443/sdk -u root -p password revertToSnapshot "[datastore] myVM1/myVM1.vmx" snapshot1'

当您运行上面的 vmrun 命令(不使用 Java)时,该命令将成功执行。但对于 Java,我收到了 ff。错误:

Error: Cannot open VM: "[datastore] myVM1/myVM1.vmx", unknown file suffix

代码如下:

   public static void main(String args[]) {
runProcessBuilderMulti(args[0]);
}

static void runProcessBuilderMulti (String cmd){
List<String> list = new ArrayList<String>();
System.out.println("Running Command: "+cmd.replace("\"","\\\""));

Matcher m = Pattern.compile("([^\"]\\S*|\".+?\")\\s*").matcher(cmd);
while (m.find())
list.add(m.group(1));

System.out.println(list);

try {
Process process = new ProcessBuilder(list).start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;

while ((line = br.readLine()) != null) {
System.out.println(line);
}

System.out.println(process.exitValue());
} catch (Exception e) {}
}

在切换到 ProcessBuilder 之前我使用了 Runtime.exec()。我认为这与引号有关,所以我添加了 cmd.replace,但显然这是一个不同的问题。

如有任何帮助,我们将不胜感激。谢谢!

最佳答案

当我发现 ProcessBuilder 使用上面的脚本执行管道命令时遇到问题时,我正在使用 ProcessBuilder。进一步谷歌搜索发现 ProcessBuilder 需要打开 shell 来执行某些命令。

ProcessBuilder b = new ProcessBuilder("/bin/sh", "-c", "ls -l | grep daemon");

功能更新如下:

   static void runProcessBuilderMulti (String cmd){

System.out.println("Running Command: "+cmd.replace("\"","\\\""));

try {
Process process = new ProcessBuilder("/bin/sh", "-c", cmd).start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;

while ((line = br.readLine()) != null) {
System.out.println(line);
}

System.out.println(process.exitValue());
} catch (Exception e) {}
}

我猜测较长的命令更难处理,例如管道命令,因此它需要自己的 shell 来执行。

关于java - 错误: Cannot open VM: "[datastore] myVM1/myVM1.vmx",未知文件后缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12172363/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com