Powershell脚本不在Java代码中运行
我有一个简单的脚本,我想从Java代码中调用它。脚本运行不正常 脚本非常简单: mkdir 0000000Powershell脚本不在Java代码中运行,java,powershell,Java,Powershell,我有一个简单的脚本,我想从Java代码中调用它。脚本运行不正常 脚本非常简单: mkdir 0000000 public static void main(String[] args) throws Exception{ String path = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"; String command = "C:\\test\\test.ps1"; Runtime
public static void main(String[] args) throws Exception{
String path = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
String command = "C:\\test\\test.ps1";
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(path + " " + command);
proc.destroy();
}
未创建目录“0000000”。我使用JDK7和Windows10
如有任何建议,将不胜感激 我修改了下面的代码,终于成功了
public static void main(String[] args) throws Exception{
String path = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
String command1 ="cmd /c \"cd C:\\test && " + path + " /c .\\test.ps1\"";
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(command1);
Thread.sleep(2000);
proc.destroy();
}
我改变了代码如下,最后它的工作
public static void main(String[] args) throws Exception{
String path = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
String command1 ="cmd /c \"cd C:\\test && " + path + " /c .\\test.ps1\"";
Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(command1);
Thread.sleep(2000);
proc.destroy();
}
exec()
异步运行该进程,因此后续的proc.destroy()
会在它可以做任何事情之前立即终止它。如果从交互式shell运行程序,只需删除proc.destroy()
即可缓解问题,但要真正解决问题,需要等待外部进程完成。您可能还希望捕获exec()
(或waitFor()
)可能引发的异常
import java.io.*;
public static void main(String[] args) throws Exception{
String path = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
String command = "C:\\test\\test.ps1";
Runtime runtime = Runtime.getRuntime();
try {
Process proc = runtime.exec(path + " " + command);
proc.waitFor();
} catch (Exception e) {
// do exception handling here
}
}
exec()
异步运行该进程,因此后续的proc.destroy()
会在它可以做任何事情之前立即终止它。如果从交互式shell运行程序,只需删除proc.destroy()
即可缓解问题,但要真正解决问题,需要等待外部进程完成。您可能还希望捕获exec()
(或waitFor()
)可能引发的异常
import java.io.*;
public static void main(String[] args) throws Exception{
String path = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
String command = "C:\\test\\test.ps1";
Runtime runtime = Runtime.getRuntime();
try {
Process proc = runtime.exec(path + " " + command);
proc.waitFor();
} catch (Exception e) {
// do exception handling here
}
}
删除
proc.destroy()
@Ansgar Wiechers:这不起作用删除proc.destroy()
@Ansgar Wiechers:这对你的答案不起作用,但这不起作用。destroy()是问题的一部分。命令本身也应该更改。@DanielSmith导入语句后缺少分号,我没有解释异常waitFor()
throws。这两个问题现在都解决了。该代码经过测试,确实有效。不需要更改命令字符串。您使用哪个操作系统?我使用win10,但它不起作用。proc.waitFor有时会产生很多问题,在我的情况下,我更喜欢2秒的延迟。@DanielSmith我在Windows 7和JDK 8上运行了它。我刚刚用JDK7在Windows10上再次测试了它。同样的结果:我发布的代码工作得非常好。当然,它确实需要执行策略才能真正允许运行PowerShell脚本,但如果这是问题所在,那么您的答案中的代码也不会起作用。谢谢您的答案,但这不起作用。destroy()是问题的一部分。命令本身也应该更改。@DanielSmith导入语句后缺少分号,我没有解释异常waitFor()
throws。这两个问题现在都解决了。该代码经过测试,确实有效。不需要更改命令字符串。您使用哪个操作系统?我使用win10,但它不起作用。proc.waitFor有时会产生很多问题,在我的情况下,我更喜欢2秒的延迟。@DanielSmith我在Windows 7和JDK 8上运行了它。我刚刚用JDK7在Windows10上再次测试了它。同样的结果:我发布的代码工作得非常好。当然,它确实需要执行策略来实际允许运行PowerShell脚本,但如果这是一个问题,那么您的答案中的代码也不会起作用。使用Thread.sleep(2000)
而将proc.destroy()
保留在原位会设置外部命令的硬超时,这意味着命令将在2秒后终止,无论它是否已完成。要等待外部命令的完成,请使用我的回答中所述的waitFor()
。不需要更改命令行(从CMD运行powershell.exe
,而不是直接运行它)。在保留proc.destroy()
的同时,使用Thread.sleep(2000)
会设置外部命令的硬超时,这意味着该命令将在2秒后终止,不管它是否完成了。要等待外部命令的完成,请使用我的回答中所述的waitFor()
。不需要更改命令行(从CMD运行powershell.exe
,而不是直接运行它)。