Linux | Ubuntu配置JDK源码编译环境

这篇具有很好参考价值的文章主要介绍了Linux | Ubuntu配置JDK源码编译环境。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

环境说明

  • 宿主机环境:Windows 11 家庭版 21H2 版本
  • 宿主机操作系统版本:22000.348
  • 虚拟机环境:VMware® Workstation 17 Pro
  • 虚拟机操作系统版本:Ubuntu 22.10
  • 参考:《深入理解Java虚拟机:JVM高级特性与最佳实践(第3版)》

Win 11 使用 VMware 的问题

在 Win 11 家庭版中使用 VMware 可能会产生一些兼容性问题,比如蓝屏、无法启动镜像等。可解决的方式包括:

  • 更换 VMware 至新版本,测试使用 VMware 15、16、17 版本,仅 17 版本不会产生蓝屏现象
  • 开启 Hyper-V 功能,在控制面板-程序-启动或关闭 Windows 功能-勾选“虚拟机平台”与“Windows 虚拟机监控程序平台”
  • 禁用 Device Guard,Win 11 家庭版无法正常启动 gpedit.msc,需要在注册表 regedit 中找到 Device Guard 将其默认值置 0

源码文档

在源码中对 Building the JDK 有详细的说明,并且很贴心的给出了 TL;DR(Too long; Didn't Read),原文如下:

TL;DR (Instructions for the Impatient)

If you are eager to try out building the JDK, these simple steps works most of the time. They assume that you have installed Mercurial (and Cygwin if running on Windows) and cloned the top-level JDK repository that you want to build.

  1. Get the complete source code:
    hg clone http://hg.openjdk.java.net/jdk/jdk

  2. Run configure:
    bash configure

    If configure fails due to missing dependencies (to either the toolchain, build tools, external libraries or the boot JDK), most of the time it prints a suggestion on how to resolve the situation on your platform. Follow the instructions, and try running bash configure again.

  3. Run make:
    make images

  4. Verify your newly built JDK:
    ./build/*/images/jdk/bin/java -version

  5. Run basic tests:
    make run-test-tier1

If any of these steps failed, or if you want to know more about build requirements or build functionality, please continue reading this document.

即如下步骤:

  • 获取源码
  • 获取依赖并执行 configure
  • 编译源码
  • 确认编译版本
  • 运行基础测试

本文是在 Ubuntu 环境下配置 JDK 源码编译环境的详细过程。

apt 软件源更新

修改apt软件源,官方源下载速度较慢,切换至国内镜像源提高速度。先将系统软件源文件做个备份,然后用国内镜像源替换文件中内容。vi 使用方法在此不作赘述。

sudo vi /etc/apt/sources.list

可替换的国内镜像源包括阿里开源镜像站、清华大学开源镜像站等。如下是清华大学开源镜像站中 Ubuntu 22.10 的镜像源,其中deb-src部分是源码镜像。

# Tsinghua
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ kinetic main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ kinetic main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ kinetic-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ kinetic-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ kinetic-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ kinetic-backports main restricted universe multiverse

# 镜像站有同步延迟,安全更新可能不够及时,不建议替换 security 源,使用下述官方源
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ kinetic-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ kinetic-security main restricted universe multiverse

deb http://security.ubuntu.com/ubuntu/ kinetic-security main restricted universe multiverse
deb-src http://security.ubuntu.com/ubuntu/ kinetic-security main restricted universe multiverse

# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ kinetic-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ kinetic-proposed main restricted universe multiverse

软件源修改完毕后,更新源文件:

sudo apt update

软件安装的指令如下,XXX是需要安装的软件名,添加可选参数 -y 默认同意安装过程中所有的确认部分:

sudo apt install XXX

软件及依赖库安装

更新完软件源后即可安装需要用到的软件与依赖。

可能会用到的
  • net-tools:网络配置工具,可选安装。
  • open-vm-tools-desktop:VMware Tools,该版本可实现分辨率自动调整,可选安装。
  • gedit:Desktop 版本的文本编辑器,可选安装。
  • openssh-server:ssh 服务,可选安装。
  • mercurial:检索跳转安装,类似 git,用于拉取资源等,可选安装。
  • git:项目版本控制,用于拉取项目等,需安装。
  • dpkg:用于软件版本管理,包含 update-alternatives 等,需安装。
JDK依赖
  • openjdk-11-jdk
  • build-essential
其他依赖
  • libfreetype6-dev
  • libcups2-dev
  • libx11-dev libxext-dev libxrender-dev libxrandr-dev libxtst-dev libxt-dev
  • libasound2-dev
  • libffi-dev
  • autoconf
  • libfontconfig1-dev
版本调整

由于下载的安装构建工具版本过高,在后续编译过程中会导致报错,需要安装 GCC-7 的版本,需要经过下述步骤:

# Ubuntu 更新软件源配置文件
sudo vi /etc/apt/sources.list

# 向文件中添加如下软件源
deb [arch=amd64] http://archive.ubuntu.com/ubuntu focal main universe

# 更新源文件
sudo apt update

# 安装 gcc-7 g++-7
sudo apt -y install gcc-7 g++-7

# 多版本控制
# 维护刚安装的 7 与 ubuntu 22.10 默认安装的 12
# 最后的数字越大 优先级越高
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 400
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-12 300

sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 400
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-12 300

# 可通过如下指令查看当前 gcc 版本管理情况
sudo update-alternatives --config gcc
sudo update-alternatives --display gcc

OpenJDK 12 源码获取

方式一

通过 git 获取

# 切换至合适目录存放代码

# 获取源码
sudo git clone https://github.com/openjdk/jdk.git

# 源码下载后默认文件夹名为 jdk
cd jdk

# 切换分支为 12
sudo git checkout jdk-12-ga

该方式也可直接从宿主机下载相应版本代码,然后通过FTP、共享文件等方式发送文件至虚拟机中。

方式二

OpenJDK 官网直接获取,但从该网站下载不稳定,可能会多次下载失败

# 切换至合适目录存放代码

# 获取源码 需安装 mercurial
sudo hg clone https://hg.openjdk.java.net/jdk/jdk12

源码编译

使用 configure 命令来对编译进行控制,如检查依赖项、参数选择、构建等。在源码中包括对此部分的描述,在文件 jdk/doc/building.md 中包含详细描述。

  • --enable-debug - Set the debug level to fastdebug (this is a shorthand for --with-debug-level=fastdebug)
  • --with-debug-level=<level> - Set the debug level, which can be release, fastdebug, slowdebug or optimized. Default is release. optimized is variant of release with additional Hotspot debug code.
  • --with-native-debug-symbols=<method> - Specify if and how native debug symbols should be built. Available methods are none, internal, external, zipped. Default behavior depends on platform. See Native Debug Symbols for more details.
  • --with-version-string=<string> - Specify the version string this build will be identified with.
  • --with-version-<part>=<value> - A group of options, where <part> can be any of pre, opt, build, major, minor, security or patch. Use these options to modify just the corresponding part of the version string from the default, or the value provided by --with-version-string.
  • --with-jvm-variants=<variant>[,<variant>...] - Build the specified variant (or variants) of Hotspot. Valid variants are: server, client, minimal, core, zero, custom. Note that not all variants are possible to combine in a single build.
  • --with-jvm-features=<feature>[,<feature>...] - Use the specified JVM features when building Hotspot. The list of features will be enabled on top of the default list. For the custom JVM variant, this default list is empty. A complete list of available JVM features can be found using bash configure --help.
  • --with-target-bits=<bits> - Create a target binary suitable for running on a <bits> platform. Use this to create 32-bit output on a 64-bit build platform, instead of doing a full cross-compile. (This is known as a reduced build.)

在这里我们使用如下指令执行 configure 命令:

bash configure --enable-debug --with-jvm-variants=server --disable-warnings-as-errors

执行成功会有如下输出:

====================================================
A new configuration has been successfully created in
/home/gx/sourcecode/jdk/build/linux-x86_64-server-fastdebug
using configure arguments '--enable-debug --with-jvm-variants=server --disable-warnings-as-errors'.

Configuration summary:
* Debug level:    fastdebug
* HS debug level: fastdebug
* JVM variants:   server
* JVM features:   server: 'aot cds cmsgc compiler1 compiler2 epsilongc g1gc graal jfr jni-check jvmci jvmti management nmt parallelgc serialgc services shenandoahgc vm-structs zgc' 
* OpenJDK target: OS: linux, CPU architecture: x86, address length: 64
* Version string: 12-internal+0-adhoc.gx.jdk (12-internal)

上述输出结果如果有 error 或 warning,不要忽视,检查是否有版本不匹配等问题,这可能会导致 make 失败。

configure 命令执行完毕,可以进行编译。但在此之前,由于 OpenJDK 12 的源码部分对 make 指令兼容性有问题。需要先调整,否则会导致编译失败。这一问题在 OpenJDK 后续版本中有修复。

在 jdk 路径下执行如下指令,修改配置内容:

sudo vi /make/common/MakeBase.gmk

大概在900多行的位置处有如下代码:

# Does the actual work with parameters stripped.
# If the file exists AND the contents is the same as the variable, do nothing
# else print a new file.
# Always returns the name of the file where the value was printed.
# Param 1 - Name of variable
# Param 2 - (optional) name of file to store value in
DependOnVariableHelper = \
    $(strip \
        $(eval -include $(call DependOnVariableFileName, $1, $2)) \
        $(if $(call equals, $(strip $($1)), $(strip $($1_old))),,\
          $(call MakeDir, $(dir $(call DependOnVariableFileName, $1, $2))) \
          $(if $(findstring $(LOG_LEVEL), trace), \
              $(info NewVariable $1: >$(strip $($1))<) \
              $(info OldVariable $1: >$(strip $($1_old))<)) \
          $(call WriteFile, $1_old:=$(call DoubleDollar,$(call EscapeHash,$($1))), \
              $(call DependOnVariableFileName, $1, $2))) \
        $(call DependOnVariableFileName, $1, $2) \
    )

将其修改为如下代码并保存:

DependOnVariableHelper = \
    $(strip \
        $(eval $1_filename := $(call DependOnVariableFileName, $1, $2)) \
        $(if $(wildcard $($1_filename)), $(eval include $($1_filename))) \
        $(if $(call equals, $(strip $($1)), $(strip $($1_old))),,\
          $(call MakeDir, $(dir $($1_filename))) \
          $(if $(findstring $(LOG_LEVEL), trace), \
              $(info NewVariable $1: >$(strip $($1))<) \
              $(info OldVariable $1: >$(strip $($1_old))<)) \
          $(call WriteFile, $1_old:=$(call DoubleDollar,$(call EscapeHash,$($1))), \
              $($1_filename))) \
        $($1_filename) \
    )

修改完成后,便可以进行编译了:

make images

编译完成后,通过如下指令验证JDK版本:

./build/linux-x86_64-server-fastdebug/jdk/bin/java --version

如果是如下结果则表明编译成功:

openjdk 12-internal 2019-03-19
OpenJDK Runtime Environment (fastdebug build 12-internal+0-adhoc.gx.jdk)
OpenJDK 64-Bit Server VM (fastdebug build 12-internal+0-adhoc.gx.jdk, mixed mode)

如果多次编译,在每次编译前要清除之前的编译记录,使用如下指令:文章来源地址https://www.toymoban.com/news/detail-438388.html

make clean && make dist-clean

到了这里,关于Linux | Ubuntu配置JDK源码编译环境的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • JDK下载、安装和环境配置教程(Linux环境)

    一、下载JDK 下载JDK官网地址:进入官网—点击Java archiveJava archiveJava archive Java Downloads | Oracle https://www.oracle.com/java/technologies/downloads/  往下翻可以看到所有的版本—版本根据自己需求选择  选择自己要下载的版本——下翻——选择tar包或rpm包(这里选择tar包)——点击下载(官

    2024年02月06日
    浏览(35)
  • Ubuntu20.4配置arm交叉编译环境

    我是在虚拟机中配置的,如果你的嵌入式设备足够完成自己的编译,可以不考虑虚拟机的。 新安装的Ubuntu20.04系统请先执行以下代码 到aarch64下载对应的aarch64的base镜像。 之后执行如下命令,创建armsys文件夹,之后将刚下载的镜像拷贝到该文件夹下并且解压 安装一些必要的软

    2024年02月06日
    浏览(40)
  • Linux系统下JDK17安装+环境配置

    我们进入到 cd /usr/lib/下然后创建一个jdk17的文件夹   mkdir jdk17 进入到jdk17目录下 下载jdk17包   wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.tar.gz 压解jdk17包 tar -zxvf  jdk-17.0.1_linux-x64_bin.tar.gz 压解完毕后 vim/etc/profile 配置环境变量       #jdk17 export JAVA_HOME=/usr/lib/jdk17

    2023年04月08日
    浏览(47)
  • linux安装jdk并配置环境变量教程

    1 下载 官网下载地址(下载jdk.tar.gz包): Java Archive | Oracle https://www.oracle.com/technetwork/java/archive-139210.html 2 解压 3 配置环境变量  打开并编辑环境变量配置文件 在配置文件末尾追加以下内容,将以下内容粘贴或输入到配置文件末尾(先按i键进入编辑模式,编辑完毕后,然后按

    2024年02月09日
    浏览(44)
  • 如何在Linux中配置JDK环境变量

    目录 如何在Linux中配置JDK环境变量 一、下载JDK 二、配置环境变量 三、刷新配置 Java Downloads | Oracle  得到安装包 上传至服务器   解压 解压成功后 找到 /etc/profile 文件  编辑文件

    2024年02月16日
    浏览(43)
  • 从头搭建Android源码编译环境(Ubuntu 18.04 / 20.04 / 22.04)

    在新安装的Ubuntu上(版本20.04LTS),完成搭建Android源码编译环境步骤如下。 顺带说一句,当前用的比较多的Ubuntu是18.04和20.04,在实际项目中一直在用,可用性和稳定性都没问题。 最新的Ubuntu22.04版本,系统默认的二进制库变化比较大,编译Android源码有问题(实测过,没细研

    2024年02月06日
    浏览(101)
  • Linux 环境下安装JDK1.8并配置环境变量

    jdk1.8的安装包可以去Oracle(甲骨文)官网下载 (地址:https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html ) 但是去Oracle需要登录。可以访问我的百度网盘下载地址: 百度网盘 提取码:bawp 将下载好的安装包放到我们刚刚创建好的 /usr/java 目录下,执行如下命令解压安装包

    2024年02月13日
    浏览(45)
  • RK3588开发板编译环境Ubuntu20.04编译配置增加交换内存

    迅为提供的编译环境 Ubuntu20.04 默认配置了交换内存是 9G,如果在编译过程中,因内 存不够而编译报错,可以参考本小节进行设置。 这里举例分配 5G 交换内存。 在开始之前,使用命令检查一下您的 ubuntu 的 swap 分区。 sudo swapon --show 通过以下命令创建一个用于 swap 的文件 su

    2024年02月11日
    浏览(31)
  • Linux中JDK下载、安装、环境配置详细教程

    1. 官网链接:https://www.oracle.com/java/technologies/downloads/archive/ 2. 下滑选择需要安装的版本(这里以Java SE 8为例) 3. 下载对应的Linux版本 1. 打开Linux选择存放的目录 2.上传刚刚下载好的安装包 (这里选择一款可以上传本地文件到Linux的软件就行,推荐使用MobaXterm) #进入到文件夹

    2024年02月11日
    浏览(51)
  • Ubuntu上搭建ARM Linux GCC交叉编译环境

    在Ubuntu操作系统上搭建ARM Linux GCC交叉编译环境是为了能够在x86架构的主机上编译运行适用于ARM架构的程序。本文将介绍详细的步骤以及相应的源代码。 安装必要的软件包 首先,我们需要安装一些必要的软件包,包括GCC、GNU Binutils和GDB。打开终端,运行以下命令来安装这些软

    2024年02月02日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包