【问题标题】:executing a powershell command in eclipse在eclipse中执行powershell命令
【发布时间】: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


【解决方案1】:

可能会抛出一些异常。在您的情况下,它不会被重新抛出并被消耗(不好的做法)。 将 catch 块更改为:

} catch (PowerShellNotAvailableException ex) {
    throw new RuntimeException("Failed to run PowerShell", ex)
}

然后你会看到出了什么问题,包括它的整个堆栈跟踪和可能的原因。

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

import org.apache.commons.io.IOUtils;

public static void main(String[] args) throws Exception
{
    Process p1 = Runtime.getRuntime().exec(new String[] { "ps", "aux" });
    InputStream input = p1.getInputStream();
    Process p2 = Runtime.getRuntime().exec(new String[] { "grep", "java" });
    OutputStream output = p2.getOutputStream();
    IOUtils.copy(input, output);
    output.close(); // signals grep to finish
    List<String> result = IOUtils.readLines(p2.getInputStream());
    System.out.println(result);
}

来源:https://stackoverflow.com/a/7226858/1688570

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

【讨论】:

  • 我会试试这个。谢谢!
【解决方案2】:

代码来自PowerShell.java 类。

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;

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

【讨论】:

    【解决方案3】:

    您必须添加一些等待时间。我正在使用 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());
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-09
      • 2018-06-08
      • 1970-01-01
      • 2012-08-03
      • 1970-01-01
      • 2014-11-03
      相关资源
      最近更新 更多