Skip to content

Jenv: Java 环境管理器

简介

跨平台Java版本管理器,支持闪电扫描(300ms)和瞬间切换(<1秒)

项目结构

.
├── src/                # Source code directory
│   ├── cmd/            # Command implementations
│   │   ├── add.go      # Add JDK command
│   │   ├── list.go     # List JDKs command
│   │   ├── remove.go   # Remove JDK command
│   │   ├── use.go      # Switch JDK command
│   │   └── root.go     # Root command and flags
│   ├── internal/       # Internal packages
│   │   ├── config/     # Configuration management
│   │   ├── constants/  # Constants definitions
│   │   ├── env/        # Environment handling
│   │   ├── java/       # Java SDK management
│   │   ├── logging/    # Logging utilities
│   │   ├── style/      # UI styling
│   │   └── sys/        # System utilities
│   └── jenv.go         # Main entry point
├── doc/                # Documentation
└── .github/            # GitHub configurations
    └── workflows/      # CI/CD workflows

安装指南

系统要求

  • Windows 10/11、Linux 或 macOS
  • 管理员权限 (Windows)
  • 至少安装一个 JDK

安装步骤

  1. 从GitHub下载最新版本,下载地址
  2. 解压ZIP文件到指定位置
  3. 以管理员权限运行JEnv
  4. 自动配置系统PATH
shell
jenv path
  1. 验证安装
shell
jenv --version

使用说明

查看版本和帮助信息

bash
jenv --version
jenv --help

环境配置

bash
# 初始化 jenv (第一次使用需要)
jenv init

配置系统路径

bash
jenv add-to-path

配置外观

bash
#jenv theme <theme_name>
jenv theme light
jenv theme dark

安装JDK

bash
#jenv add <alias> <jdk_path> 添加JDK
jenv add jdk8 "C:\Java\jdk1.8.0_291"

查看JDK

bash
# 查看所有JDK
jenv list

版本切换

bash
# 在不同的 JDK 版本间切换
# jenv use <alias> 
jenv use jdk8
jenv use jdk17
jenv use jdk21

删除JDK

bash
#jenv remove <alias>
jenv remove jdk8

查看当前JDK

bash
jenv current

扫描并安装JDK

bash
# Windows
jenv scan c:\

# Linux
jenv scan /usr/lib/jvm
jenv scan /opt

# macOS
jenv scan /Library/Java/JavaVirtualMachines

常见问题

为什么需要系统管理员权限?

因为JEnv的机制是需要改变文件的软链接,所以需要管理员权限。

它怎么工作?

Inspired by nvm-windows, JEnv uses symlinks for Java version management across all platforms, which offers several advantages:

  1. Symlink-Based Architecture

    • Windows: Creates a single symlink at C:\java\JAVA_HOME during installation
    • Linux: Creates symlink at /opt/jenv/java_home (system) or ~/.jenv/java_home (user)
    • Switching Java versions only requires updating the symlink target
    • No need to modify system PATH repeatedly
    • Changes persist across system reboots and apply to all console windows/shells
  2. High-Performance Implementation

    • Ultra-fast JDK scanning: Optimized from 3 seconds to 300ms using concurrent processing
    • Dispatcher-Worker Model: Parallel JDK detection with worker goroutines
    • Intelligent Pre-filtering: Aggressive directory filtering to skip unnecessary locations
    • Detailed Statistics: Real-time progress tracking and comprehensive scan results
    • Optimized Path Exclusion: Smart logic to avoid scanning duplicate or invalid paths
  3. Implementation Details

    • During initialization:
      • Windows: Creates JAVA_HOME directory at C:\java\JAVA_HOME
      • Linux: Creates symlink directory based on privileges (system or user level)
      • Adds JAVA_HOME/bin to system PATH (one-time setup)
      • Creates initial symlink to default JDK
    • When switching versions:
      • Simply updates symlink target to desired JDK
      • No PATH modifications needed
      • Changes take effect immediately in all console windows/shells
  4. Permission Handling

    • Windows: Administrator privileges required for system symbolic links
    • Linux: Root privileges optional - falls back to user-level configuration
    • UAC/sudo prompts handled automatically with minimal privilege scope
    • Follows the principle of least privilege
    • Permission requests only occur during initialization and version switching
  5. Multi-Shell Support (Linux)

    • Automatically detects and configures bash, zsh, fish shells
    • Updates appropriate configuration files (.bashrc, .zshrc, config.fish)
    • Ensures environment variables persist across shell sessions

This approach is more efficient than constantly modifying system PATH variables, providing a cleaner and more reliable solution for Java version management across all supported platforms.

最后更新于: