【发布时间】:2016-02-17 10:08:16
【问题描述】:
我正在尝试使用以下代码在 Eclipse 中运行 powershell 命令。 powershell 脚本显示 Windows 上已安装应用程序的列表。该脚本在 powershell 中执行时工作正常。但我无法在控制台上获得输出。有人可以告诉我这里有什么问题吗?
import com.profesorfalken.jpowershell.PowerShell;
import com.profesorfalken.jpowershell.PowerShellNotAvailableException;
import com.profesorfalken.jpowershell.PowerShellResponse;
public class TestProcessList {
public static void main(String[] args) throws Exception {
try {
PowerShell powerShell = PowerShell.openSession();
String command = "Get-ItemProperty " +
"HKLM:/Software/Wow6432Node/Microsoft/Windows/CurrentVersion/Uninstall/* " +
"| Select-Object DisplayName, DisplayVersion, Publisher, InstallDate " +
"| Format-Table –AutoSize";
PowerShellResponse response = powerShell.executeCommand(command);
System.out.println("Proceses are:" + response.getCommandOutput());
powerShell.close();
} catch (PowerShellNotAvailableException ex) {
throw new RuntimeException("Failed to run PowerShell", ex);
}
}
}
【问题讨论】:
-
在您的
catch块中添加System.out.println,然后重试。 -
永远不要扔掉这样的异常。您应该始终采取措施报告问题。
-
寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:How to create a Minimal, Complete, and Verifiable example。
-
我为异常添加了打印语句。也不例外。
-
我现在确实关闭了 powershell。现在我收到一个错误“严重:关闭 PowerShell 时出现意外错误:超时!”
标签: java eclipse powershell