首页 > 解决方案 > 如何在 Linux 操作系统中从 Java 调用“Powershell 脚本文件”

问题描述

班级:-

======================

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

public class TestPowershell {
    public static void main(String[] args) throws IOException     
    {    
        Runtime runtime = Runtime.getRuntime();

        Process proc = runtime.exec("cmd powershell \"\\Test\\Powershell\\powershell.ps1\" ");

    proc.getOutputStream().close();

        InputStream is = proc.getInputStream();

        InputStreamReader isr = new InputStreamReader(is);

        BufferedReader reader = new BufferedReader(isr);

        String line;

        while ((line = reader.readLine()) != null)
        {
            System.out.println(line);
        }

        reader.close();

        proc.getOutputStream().close();

    }

}

我正在尝试通过在 linux 环境中使用 java 来执行 powershell 文件,我遇到了异常(上面我附加了类和异常),请提供一个可以在 linux 中执行 powershell 脚本文件的测试类。提前致谢

标签: javalinuxpowershell

解决方案


感谢您的所有回答。

最后,我知道在使用 PowerShell 时,我们应该在 Windows 操作系统中运行脚本,因为微软是 PowerShell 的所有者,他们在 Windows 操作系统中提供了更多功能。

我所做的是,我在 Windows 操作系统中运行脚本并生成了一个 CSV 文件并保存在 SFTP 文件夹中,通过使用 java 我加载了我的文件并处理了我的下一个进程。


推荐阅读