Jenkins 构建项目提示没有找到 JDK

在升级 CentOS 后,Jenkins 提示没有找到 JDK。

FATAL: no nio in java.library.path: [/usr/java/packages/lib, /usr/lib64, /lib64, /lib, /usr/lib]
java.lang.UnsatisfiedLinkError: no nio in java.library.path: [/usr/java/packages/lib, /usr/lib64, /lib64, /lib, /usr/lib]
	at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2670)
	at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:830)
	at java.base/java.lang.System.loadLibrary(System.java:1873)
	at java.base/sun.nio.fs.LinuxNativeDispatcher$1.run(LinuxNativeDispatcher.java:136)
	at java.base/sun.nio.fs.LinuxNativeDispatcher$1.run(LinuxNativeDispatcher.java:134)
	at java.base/java.security.AccessController.doPrivileged(Native Method)
	at java.base/sun.nio.fs.LinuxNativeDispatcher.<clinit>(LinuxNativeDispatcher.java:134)
	at java.base/sun.nio.fs.LinuxFileSystem.getMountEntries(LinuxFileSystem.java:81)
	at java.base/sun.nio.fs.LinuxFileStore.findMountEntry(LinuxFileStore.java:75)
	at java.base/sun.nio.fs.UnixFileStore.<init>(UnixFileStore.java:69)
	at java.base/sun.nio.fs.LinuxFileStore.<init>(LinuxFileStore.java:49)
	at java.base/sun.nio.fs.LinuxFileSystemProvider.getFileStore(LinuxFileSystemProvider.java:51)
	at java.base/sun.nio.fs.LinuxFileSystemProvider.getFileStore(LinuxFileSystemProvider.java:39)
	at java.base/sun.nio.fs.UnixFileSystemProvider.getFileStore(UnixFileSystemProvider.java:373)
	at java.base/java.nio.file.Files.getFileStore(Files.java:1488)
	at org.eclipse.jgit.util.FS$FileStoreAttributes.getFileStoreAttributes(FS.java:362)
	at org.eclipse.jgit.util.FS$FileStoreAttributes.get(FS.java:346)
	at org.eclipse.jgit.util.FS.getFileStoreAttributes(FS.java:913)
	at org.eclipse.jgit.internal.storage.file.FileSnapshot.<init>(FileSnapshot.java:226)
	at org.eclipse.jgit.internal.storage.file.FileSnapshot.<init>(FileSnapshot.java:207)
	at org.eclipse.jgit.internal.storage.file.FileSnapshot.save(FileSnapshot.java:104)
	at org.eclipse.jgit.internal.storage.file.FileRepository.<init>(FileRepository.java:205)
	at org.eclipse.jgit.lib.BaseRepositoryBuilder.build(BaseRepositoryBuilder.java:625)
	at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.getRepository(CliGitAPIImpl.java:3374)
	at hudson.plugins.git.GitAPI.getRepository(GitAPI.java:288)

错误信息的截图如下:

问题和解决

出现上面的问题可能是因为在 CentOS 操作系统中升级 JDK 后环境变量有了修改。

运行下面的命令来选择需要的 JDK

/usr/sbin/alternatives --config java

配置和运行结果如下:

[[email protected] ~]# /usr/sbin/alternatives --config java

There are 2 programs which provide 'java'.

  Selection    Command
-----------------------------------------------
 + 1           java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.11.0.9-2.el8_4.x86_64/bin/java)
*  2           java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.292.b10-1.el8_4.x86_64/jre/bin/java)

Enter to keep the current selection[+], or type selection number: 1

当完成配置后重启 Jenkins 就可以解决问题了。