SpringBoot+jPowerShell项目Docker化(RHEL7镜像)启动报错求助
问题背景
我在开发一个SpringBoot项目,引入了jPowerShell依赖,用RHEL 7基础镜像完成Docker化,并且在镜像中安装了PowerShell,但运行容器时出现报错并退出,错误信息如下:
PowerShell not available
com.profesorfalken.jpowershell.PowerShellNotAvailableException: Cannot execute PowerShell.exe. Please make sure that it is installed in your system
Dockerfile内容如下:
FROM something.abc.com/test-base/rhel:7 ENV http_proxy http://http.proxy.xyz.com:8000 ENV https_proxy http://http.proxy.xyz.com:8000 ENV no_proxy localhost,127.0.0.1,.xyz.com ENV PATH="/usr/java/jdk-15.0.1/bin:$PATH" # Install common packages RUN yum install -y bzip2 RUN yum install -y wget RUN wget https://github.com/PowerShell/PowerShell/releases/download/v7.3.7/powershell-7.3.7-1.rh.x86_64.rpm RUN yum install -y powershell-7.3.7-1.rh.x86_64.rpm RUN pwsh --version # Download and Install OpenJDK 15 RUN cd /tmp && wget https://download.java.net/java/GA/jdk15.0.1/51f4f36ad4ef43e39d0dfdbaf6549e32/9/GPL/openjdk-15.0.1_linux-x64_bin.tar.gz && \ tar -xvzf openjdk-15.0.1_linux-x64_bin.tar.gz && \ mkdir -p /usr/java && mv /tmp/jdk-15.0.1 /usr/java VOLUME /tmp ADD target/xyz-*.jar /app/xyz/sample.jar EXPOSE 8080 ENTRYPOINT ["java","-jar","/app/xyz/sample.jar"]
运行容器错误截图:
PowerShell路径截图:

问题原因
jPowerShell默认会查找Windows平台的PowerShell可执行文件powershell.exe,但在Linux系统中,你安装的PowerShell 7+版本的可执行文件名为pwsh,而非powershell.exe,这直接导致jPowerShell找不到对应执行程序抛出异常。
另外,虽然Dockerfile中安装了pwsh,但PATH环境变量仅添加了JDK路径,虽然后续rpm安装会自动创建软链接到/usr/bin/pwsh(该路径默认在PATH内),但显式配置能彻底避免路径问题。
解决方案
1. 修改jPowerShell配置,指定使用pwsh
在SpringBoot代码中初始化PowerShell会话时,通过PowerShellConfiguration指定可执行文件为pwsh:
import com.profesorfalken.jpowershell.PowerShell; import com.profesorfalken.jpowershell.PowerShellConfiguration; // 构建配置,指定pwsh作为执行程序 PowerShellConfiguration config = PowerShellConfiguration.builder() .executable("pwsh") .build(); // 使用配置打开PowerShell会话 PowerShell powerShell = PowerShell.openSession(config); // 后续执行命令逻辑...
2. 确保Docker镜像中pwsh路径加入PATH(可选但保险)
修改Dockerfile中的PATH环境变量,将pwsh的安装路径(通常为/opt/microsoft/powershell/7)加入:
ENV PATH="/usr/java/jdk-15.0.1/bin:/opt/microsoft/powershell/7:$PATH"
内容的提问来源于stack exchange,提问作者BHL

