英文:
JAXB project to be compiled with JDK SE 8 but has to run also on JRE 11
问题
我有一个Java程序,我编译为Java 1.8,因为其中包含了-等内容,因为许多用户只有JRE 1.8。在该程序中,我经常使用javax.xml.bind.*来进行XML的编组和解组。为了帮助用户在JRE 9-10上运行它,我还提供了一个shell脚本,该脚本使用-add-modules
参数调用java。对于那些只安装了Java 11+(没有之前的Java版本)的用户,这将无法工作,对吗?
所以我考虑将JAXB(或EclipseLink Moxy)与我的程序捆绑在一起,仍然使用JDK8进行编译(使用Eclipse IDE)。这样它将在Java 8上运行,也可以在Java 11+上运行,对吗?
谢谢!
英文:
I have a Java program I compile in Java 1.8 because it has - between others - because many users only have JRE 1.8. In that program I use javax.xml.bind.* quite often to marshall and unmarshall XML. To help users run it on JRE 9-10, I also provided a shell script that calls java with the -add-modules
argument. For users that have Java 11+ (and no previous Java version installed), that won't work, will it?
So I thought of bundling JAXB (or EclipseLink Moxy) with my program and still compile it with JDK8 (with Eclipse IDE). So that I runs on Java 8 and still on Java 11+. Will that work?
Thanks!
答案1
得分: 0
这里是解决方案,将这些依赖项添加到您的pom.xml文件中(如果您正在使用Maven):
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>${jaxb.api.version}</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>${jaxb.api.version}</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>${jaxb.api.version}</version>
</dependency>
并且此代码将在Java 8和11的两个版本中编译。
英文:
here a solution add these dependencies to your pom.xml ( if u re using) maven :
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>${jaxb.api.version}</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>${jaxb.api.version}</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>${jaxb.api.version}</version>
</dependency>
and the code will compile in both version java 8 and 11.
答案2
得分: 0
感谢您的评论和回答!
最终我成功运行了,尽管还不完美。我所做的是:
- 从这里下载了 Jakarta JAXB 2.3.2:maven
- 解压并将内容放入我的项目目录
- 在 Eclipse IDE 2020-6 中,我编辑了项目的属性(Cmd+I):
- 在“Java构建路径”主题下,我使用“添加外部JAR”添加了3个JAR文件:jaxb-impl.jar、jakarta.xml.bind-api.jar 和 jakarta.activation.jar
- 显然我之前忘记了的部分:还在“顺序和导出”下,我勾选了这3个JAR文件。
为了测试结果,我告诉 Eclipse IDE 在“运行/调试”下,使用 JRE 12 来运行我的主类。
对我来说这样运行成功了。
英文:
Thank you for your comments and answers!
I finally got it running, though it is not perfect yet. This is what I did:
- downloaded Jakarta JAXB 2.3.2 here: maven
- unzipped it and put the content to my project directory
- in Eclipse IDE 2020-6, I edited the properties of the project (Cmd+I):
- Under the topic "Java Build Path", I added 3 jars with "Add External JAR": jaxb-impl.jar, jakarta.xml.bind-api.jar and jakarta.activation.jar
- The part I obviously forgot before: also under "order and export", I checked that 3 JARs.
To test the result, I told Eclipse IDE under "Run/Debug", register "JRE" to run my main class with JRE 12.
That worked for me.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论