gpt4 book ai didi

java - 在java中运行windows powershell命令

转载 作者:行者123 更新时间:2023-11-30 08:03:39 38 4
gpt4 key购买 nike

我能够在 Windows PowerShell 中运行此命令。

Add-Content -Path C:/Users/User/Desktop/sda.txt -Value "`nThis is the last line"

我尝试通过 Java 运行类似的命令,但它没有执行该命令。

Runtime runtime = Runtime.getRuntime();
System.out.println("powershell Add-Content -Path C:/Users/User/Desktop/sda.txt -Value " + "\"`nThis is the last line\"");
runtime.exec("powershell Add-Content -Path C:/Users/User/Desktop/sda.txt -Value " + "\"`nThis is the last line\"");
<小时/>

按照aquaraga的建议进行第二次尝试

Runtime runtime = Runtime.getRuntime();
Process proc;
System.out.println("powershell Add-Content -Path C:/Users/User/Desktop/sda.txt -Value " + "\"`nThis is the last line\"");
proc = runtime.exec("powershell Add-Content -Path C:/Users/User/Desktop/sda.txt -Value " + "\"`nThis is the last line\"");
try {
proc.waitFor();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

最佳答案

将 powershell 命令参数括在单引号中:

proc = runtime.exec("powershell Add-Content -Path C:/Users/User/Desktop/sda.txt -Value " + "\"'`nThis is the last line'\"");

关于java - 在java中运行windows powershell命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31498112/

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