如何在Yocto OS中部署Java 17运行环境?
Yocto构建OpenJDK 17遇到的问题及解决方案
问题背景
我们团队正在为phyCORE i.MX 8M Plus嵌入式设备构建Yocto OS镜像,应用是基于Java 17开发的微服务。之前在其他系统用Docker部署,但缺乏Yocto构建经验,现在遇到瓶颈:已安装的meta-java层只支持OpenJDK 8;尝试构建包含OpenJDK 17的自定义层时,bitbake执行<recipe-name>的do_configure()阶段交叉编译失败。想咨询有没有支持Java 8以上版本的现成Yocto层,或者能让自定义层正常运行的建议。
当前构建配置
local.conf
MACHINE ?= "phyboard-pollux-imx8mp-3" DISTRO ?= "ampliphy-vendor" ######################## # global configuration # ######################## # That are the default values of bitbake. Adapt these to your workspace and # host preferences. #DL_DIR = "${TOPDIR}/downloads" #SSTATE_DIR = "${TOPDIR}/sstate-cache" # License Handling # - Uncomment for i.MX6 proprietary GPU libraries #LICENSE_FLAGS_WHITELIST += "license-nxp_v14-june-2016_imx-gpu-viv" # - Uncomment for Freescale i.MX6 VPU firmware blobs #LICENSE_FLAGS_WHITELIST += "license-freescale_v12-march-2016_firmware-imx" # build artifacts are deployed here DEPLOY_DIR = "${TOPDIR}/deploy" # enable bitbakes git shallow functionality: this reduces download volume # from 7.3G to 1.6 G as of sumo. If you need the git history of a project, # you can run a "git remote update" in the tmp dir, as the remotes are # correctly specified BB_GIT_SHALLOW = "1" # Select configuration UI for linux and barebox recipe. The openembedded # default is 'menuconfig', 'nconfig' has more features. # busybox only supports menuconfig #KCONFIG_CONFIG_COMMAND = "menuconfig" KCONFIG_CONFIG_COMMAND = "nconfig" KCONFIG_CONFIG_COMMAND_pn-busybox = "menuconfig" # SDK will be build for that cpu #SDKMACHINE ?= "x86_64" # bitbake disk monitor setup BB_DISKMON_DIRS ??= "\\ STOPTASKS,${TMPDIR},1G,100K \\ STOPTASKS,${DL_DIR},1G,100K \\ STOPTASKS,${SSTATE_DIR},1G,100K \\ STOPTASKS,/tmp,100M,100K \\ ABORT,${TMPDIR},100M,1K \\ ABORT,${DL_DIR},100M,1K \\ ABORT,${SSTATE_DIR},100M,1K \\ ABORT,/tmp,10M,1K" ####################### # Image configuration # ####################### # The following line disables the autostart of the phytec-qtdemo by # default, but you can start the demo anytime using # $ systemctl start phytec-qtdemo.service #SYSTEMD_AUTO_ENABLE_pn-phytec-qtdemo = "disable" # You can disable and enable FSTYPES as you wish. e.g. 'ext4'. # This variable is ordering dependend. #IMAGE_FSTYPES = "tar.gz wic ubifs emmc" # To change the variable for any imx8 machine: #IMAGE_FSTYPES_mx8 = "tar.gz wic ubifs emmc" # Turn on debugging options of the kernel #DEBUG_BUILD_pn-linux-mainline = "1" # Turn on debugging options of the imx kernel #DEBUG_BUILD_pn-linux-imx = "1" # Turn on debugging options of the barebox #DEBUG_BUILD_pn-barebox = "1" # Include kernel sources in SDK TOOLCHAIN_TARGET_TASK_append = " kernel-devsrc" # The default package class of the distro ampliphy is 'package_ipk'. The first # value is used as the package manager to build the image and sdk. To build # also tar packages use #PACKAGE_CLASSES = "package_ipk package_tar" # Variable IMAGE_ROOTFS_EXTRA_SPACE from poky/meta/conf/documentation.conf: # Defines additional free disk space created in the image in Kbytes. By # default, this variable is set to '0'. # This example line adds an additional 512 MiB of free space to the root # filesystem: #IMAGE_ROOTFS_EXTRA_SPACE = "524288" # See http://www.yoctoproject.org/docs/1.8/ref-manual/ref-manual.html#ref-features-image # Through these variables, you can add several different predefined # packages such as development utilities or packages with debug information # needed to investigate application problems or profile applications EXTRA_IMAGE_FEATURES = "" # - "Makes an image suitable for development (e.g. allows root logins without # passwords and enables post-installation logging)" EXTRA_IMAGE_FEATURES += "debug-tweaks" # - "Installs debug symbol packages for all packages installed in a given # image." #EXTRA_IMAGE_FEATURES += "dbg-pkgs" # - "Installs debugging tools such as strace and gdb." #EXTRA_IMAGE_FEATURES += "tools-debug" ###################### # rauc configuration # ###################### # certificate location for signing bundles #CERT_PATH = "${TOPDIR}/../sources/meta-ampliphy/openssl-ca" #################### # QT configuration # #################### # We disable qt widgets per default to save about 10 MB of storage. If # you need qt widget support, you can enable it with this line #DISTRO_FEATURES_append = " qtwidgets" CONF_VERSION = "1" # Uncomment to accept NXP EULA (needed, if any NXP / freescale layer is used) # EULA can then be found under ../sources/meta-freescale/EULA ACCEPT_FSL_EULA = "1" # A TISDK BSP requires per default an external toolchain # Provide a default path that is compatible to our TISDK docker image TOOLCHAIN_BASE ?= "/opt/PHYTEC_BSPs" ############################################ # Addition of openjdk-8 and minicom to the default build PREFERRED_PROVIDER_virtual/java-initial-native = "cacao-initial-native" PREFERRED_PROVIDER_virtual/java-native = "jamvm-native" PREFERRED_PROVIDER_virtual/javac-native = "ecj-bootstrap-native" IMAGE_INSTALL_append = " minicom openjdk-8" ############################################
bblayers.conf
# POKY_BBLAYERS_CONF_VERSION is increased each time build/conf/bblayers.conf # changes incompatibly POKY_BBLAYERS_CONF_VERSION = "2" BBPATH = "${TOPDIR}" BBFILES ?= "" OEROOT := "/home/ubuntu/yocto/sources/poky" # add BSPDIR variable, as it is needed by recipes of NXP BSPDIR := "${OEROOT}/../.." ####################################### # IMPORTANT NOTE: change to correct location and username on your build! MYBSPDIR := "/home/ubuntu/yocto/sources" ######################################## # In our BSPs active layers are defined by the manifest.xml for all BSPs. The # bblayers.conf gets created by the init_bblayers.py script. BBLAYERS ?= "" BBLAYERS += "\\ ${OEROOT}/../poky/meta \\ ${OEROOT}/../poky/meta-poky \\ ${OEROOT}/../meta-openembedded/meta-oe \\ ${OEROOT}/../meta-openembedded/meta-networking \\ ${OEROOT}/../meta-openembedded/meta-python \\ ${OEROOT}/../meta-openembedded/meta-multimedia \\ ${OEROOT}/../meta-openembedded/meta-filesystems \\ ${OEROOT}/../meta-openembedded/meta-perl \\ ${OEROOT}/../meta-openembedded/meta-gnome \\ ${OEROOT}/../meta-imx/meta-bsp \\ ${OEROOT}/../meta-imx/meta-sdk \\ ${OEROOT}/../meta-imx/meta-ml \\ ${OEROOT}/../meta-browser/meta-chromium \\ ${OEROOT}/../meta-clang \\ ${OEROOT}/../meta-freescale \\ ${OEROOT}/../meta-freescale-3rdparty \\ ${OEROOT}/../meta-freescale-distro \\ ${OEROOT}/../meta-nxp-demo-experience \\ ${OEROOT}/../meta-python2 \\ ${OEROOT}/../meta-qt5 \\ ${OEROOT}/../meta-virtualization \\ ${OEROOT}/../meta-rauc \\ ${OEROOT}/../meta-phytec \\ ${OEROOT}/../meta-ampliphy \\ ${OEROOT}/../meta-security \\ ${OEROOT}/../meta-security/meta-tpm \\ " ################################### # Addition of the meta-java layer BBLAYERS += "${MYBSPDIR}/meta-java" ###################################
解决方案建议
现成Yocto层选择
- meta-openjdk:该层支持OpenJDK 11及以上版本(包括17),适配Dunfell及之后的Yocto版本,直接集成到现有构建即可,无需自己从头写recipe。
- 更新meta-java层分支:官方meta-java在master、kirkstone等较新分支中已经支持OpenJDK 11/17,替换当前旧版本的meta-java层为对应Yocto版本的兼容分支即可。
自定义层修复要点
如果坚持维护自定义层,针对do_configure()失败的问题,从以下方面排查:
- 依赖版本适配:OpenJDK 17需要更新的基础依赖,比如glibc 2.31+、GCC 10+,确保Yocto构建环境的工具链满足要求。
- 交叉编译环境配置:在recipe中正确设置交叉编译变量,比如
TARGET_CC、TARGET_CXX,同时确保构建机器上安装了原生OpenJDK 17作为宿主编译环境。 - 平台补丁适配:OpenJDK源码需要针对ARM64架构的补丁,参考成熟层(比如meta-openjdk)中的补丁文件,修复嵌入式平台的兼容性问题。
- 构建资源调整:OpenJDK编译耗内存,建议构建机器至少16G RAM,在local.conf中设置
PARALLEL_MAKE = "-j$(nproc)"(根据核心数调整),避免内存不足导致编译失败。
配置调整示例
替换meta-java层后,更新local.conf中的Java相关配置:
# 替换为OpenJDK 17作为默认Java提供者 PREFERRED_PROVIDER_virtual/java = "openjdk-17" PREFERRED_PROVIDER_virtual/javac = "openjdk-17" # 将OpenJDK 17加入镜像安装列表 IMAGE_INSTALL_append = " openjdk-17" # 确保OpenJDK需要的特性已开启 DISTRO_FEATURES_append = " largefile ipv6"
内容的提问来源于stack exchange,提问作者bc_tom
相关产品推荐
相关产品推荐

