python脚本配置java环境
适用Windows与Linux
Linux系统的全局配置文件路径:/etc/profile
首先需要下载jdk,然后在命令行使用脚本
使用方式:jdk路径不带bin目录
python set_java_env.py /path/to/jdk
set_java_env.py内容如下:
import os
import sys
def set_java_env(java_path):
# 设置 JAVA_HOME
os.environ[python set_java_env.py /path/to/jdk'JAVA_HOME'] = java_path
# 将 Java 的 bin 目录添加到 PATH
path = os.environ.get('PATH', '')
java_bin = os.path.join(java_path, 'bin')
if java_bin not in path:
os.environ['PATH'] = f"{java_bin}{os.pathsep}{path}"
if __name__ == "__main__":
# 检查命令行参数
if len(sys.argv) != 2:
print("Usage: python set_java_env.py <java_installation_path>")
sys.exit(1)
# 获取 Java 安装路径
java_installation_path = sys.argv[1]
# 调用函数设置环境变量
set_java_env(java_installation_path)
print("java environment has configurated successfully~")
bat脚本配置java环境
set_java_env.bat:
@echo off
if "%1"=="" (
echo Usage:./set_java_env.bat /path/to/jdk
exit /b
)
set JAVA_HOME=%1
set PATH=%JAVA_HOME%\bin;%PATH%
echo java environment has configurated successfully~
java -version
标签:脚本,bin,set,Java,java,自动,env,path,os
From: https://www.cnblogs.com/lingoblog/p/18319551