使用 eclipse,当我使用 Java 1.8 执行 ant 任务时,出现以下错误。
不支持低于11的JRE版本。
我的“外部工具配置”是用“单独的JRE”配置的Eclipse Adoptium jdk-8.0.352.8-hotspot
有人知道如何防止这种限制吗?
面向企业 Java 和 Web 开发人员的 Eclipse IDE(包括孵化组件) 版本:2022-12 (4.26.0) 版本号:20221201-1913
较新版本的 Eclipse 需要 JRE 版本高于 11 才能启动
Ant
构建。 Eclipse 可能会使用工作区默认 JRE 来启动 Ant 目标。您始终可以覆盖该配置以使用 >=11 的 JRE 来启动 Ant 构建。
Eclipse 现在将使用您指定的新 JRE 启动您的 Ant 构建。请注意,此配置更改不会影响 Eclipse 完成的 java 类编译。 Eclipse 将继续使用您的默认工作区 JRE 来编译您的 java 类。
已运行。
在 build.xml 中使用 1.8 添加源和目标对我来说很有效
<JAVAC destdir="${build.classes.test}" source="1.8" target="1.8"> <src path="${src.test}" />
后来我用
javap -v MyStuff.class
检查并看到输出 major version: 52
是 1.8
以下是描述情况的错误:
java 任务未在 1.8 上运行
上述方法适用于 ant
javac
任务。但我也有 ant java
来生成一些类。我最终得到了
[java] Caused by: java.lang.ClassNotFoundException: javax.xml.bind.JAXBException
因为 Ant 是使用 Java 17 运行的,而像
javax.xml.bind.JAXBException
这样的 JAXB 类自 Java 11 起就不再包含在 JDK 中。
我的解决方法是不使用 Eclipse 来执行这些 ant 任务,而是使用单独的(旧=我们在 jenkins 构建中使用的相同)ant 安装并从命令行运行任务。请参阅https://ant.apache.org/manual/install.html#getBinary
使用 1.8 运行 ant 任务会导致
Error: A JNI error has occurred, please check your installation and try again.
根据此 eclipse 线程,自 eclipse 2022 版本起,必须使用 JDK 11 来启动 build.xml: https://bugs.eclipse.org/bugs/show_bug.cgi?id=579317
但是您仍然可以使用 JDK 1.8 在 buid.xml 中使用此技巧进行编译:
首先:将 Windows 环境中的 JAVA_HOME 设置为 JDK 1.8 文件夹
然后添加:
<property environment="env" />
<javac ... executable="${env.JAVA_HOME}\bin\javac.exe" fork="true" />
然后你需要使用JDK >= 11启动构建(可以使用eclipse内置的jre)
或者,不要使用 Ant 集成。
相反,创建一个外部工具启动配置:
Main:
Location:
{installation path}\apache-ant-1.10.13\bin\ant.bat
Working directory ${workspace_loc:/your_project}
Arguments:
-buildfile build.xml
Refresh:
(X) the entire workspace
[X] recursively include sub-folders
Environment:
ANT_HOME {installation path}\apache-ant-1.10.13
JAVA_HOME {path_to_your_java_8_directory}
Common:
[X] Allocate Console (necessary for input)