Java 在eclipse中执行powershell命令

Java 在eclipse中执行powershell命令,java,eclipse,powershell,Java,Eclipse,Powershell,我正在尝试使用以下代码在eclipse中运行powershell命令。powershell脚本显示windows上已安装应用程序的列表。脚本在powershell中执行时工作正常。但是我无法在控制台上获得输出。有人能告诉我这里有什么问题吗 import com.profesorfalken.jpowershell.PowerShell; import com.profesorfalken.jpowershell.PowerShellNotAvailableException; import co

我正在尝试使用以下代码在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 (PowerShellNotAvailableException ex) {
    throw new RuntimeException("Failed to run PowerShell", ex)
}
然后,您将看到出现了什么问题,包括整个堆栈跟踪和可能的原因

更新: 实际上,您正在单个命令的内部使用管道命令(“执行命令字符串中的“|”)。它不会工作,因为管道不容易在java中实现。 基于以下命令“ps aux | grep java”示例尝试解决方案:

import org.apache.commons.io.IOUtils;
公共静态void main(字符串[]args)引发异常
{
进程p1=Runtime.getRuntime().exec(新字符串[]{“ps”,“aux”});
InputStream输入=p1.getInputStream();
进程p2=Runtime.getRuntime().exec(新字符串[]{“grep”,“java”});
OutputStream output=p2.getOutputStream();
IOUtils.copy(输入、输出);
output.close();//指示grep完成
List result=IOUtils.readLines(p2.getInputStream());
系统输出打印项次(结果);
}
资料来源:

由于我不知道PowerShell库的API,您必须修改示例以自己使用PowerShell库。

来自类的代码

这意味着您的命令需要超过2000毫秒才能关闭/完成。
我认为在代码中添加自定义睡眠可能会有所帮助。我不熟悉
JPowerShell
,您应该看看
PowerShell
类。或者试试图迪克的答案。

你必须增加一些等待时间。我使用java执行了一些命令,但无法在控制台上打印命令输出,即使我使用的是
response.getCommandOutput()
。因此,我尝试在下面添加等待:

PowerShellResponse response;
Map<String, String> maxWait = new HashMap<String, String>();
maxWait.put("maxWait", "300000");
PowerShell powerShell = PowerShell.openSession().configuration(maxWait);
response = powerShell.executeCommand(yourCommand);
System.out.println(response.getCommandOutput());
powershell响应;
Map maxWait=newhashmap();
maxWait.put(“maxWait”,“300000”);
PowerShell PowerShell=PowerShell.openSession().configuration(maxWait);
response=powerShell.executeCommand(您的命令);
System.out.println(response.getCommandOutput());

等待时间是等待最长时间。

catch
块中放入
System.out.println
,然后重试。永远不要丢弃这样的异常。您应该总是做一些事情来报告问题。寻求调试帮助的问题(“为什么此代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:。我为异常添加了打印语句。也不例外。我现在确实关闭了powershell。现在我收到一个错误“严重:关闭PowerShell时出现意外错误:超时!”我将尝试此操作。谢谢
int closingTime = 0;

while (!closeTask.isDone () 
    && !closeTask.isDone()) {
            if (closingTime > MAX_WAIT) {
        Logger.getLogger(PowerShell.class.getName()).log(Level.SEVERE, "Unexpected error when closing PowerShell: TIMEOUT!");
        break;
    }
    Thread.sleep(WAIT_PAUSE);
    closingTime += WAIT_PAUSE;
static final int WAIT_PAUSE = 10;
static final int MAX_WAIT = 2000;
PowerShellResponse response;
Map<String, String> maxWait = new HashMap<String, String>();
maxWait.put("maxWait", "300000");
PowerShell powerShell = PowerShell.openSession().configuration(maxWait);
response = powerShell.executeCommand(yourCommand);
System.out.println(response.getCommandOutput());