JAXB项目需要使用JDK SE 8进行编译,但也必须在JRE 11上运行。

huangapple go评论68阅读模式
英文:

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 :

   &lt;dependency&gt;
        &lt;groupId&gt;javax.xml.bind&lt;/groupId&gt;
        &lt;artifactId&gt;jaxb-api&lt;/artifactId&gt;
        &lt;version&gt;${jaxb.api.version}&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
        &lt;groupId&gt;com.sun.xml.bind&lt;/groupId&gt;
        &lt;artifactId&gt;jaxb-core&lt;/artifactId&gt;
        &lt;version&gt;${jaxb.api.version}&lt;/version&gt;
    &lt;/dependency&gt;

    &lt;dependency&gt;
        &lt;groupId&gt;com.sun.xml.bind&lt;/groupId&gt;
        &lt;artifactId&gt;jaxb-impl&lt;/artifactId&gt;
        &lt;version&gt;${jaxb.api.version}&lt;/version&gt;
    &lt;/dependency&gt;

and the code will compile in both version java 8 and 11.

答案2

得分: 0

感谢您的评论和回答!

最终我成功运行了,尽管还不完美。我所做的是:

  1. 从这里下载了 Jakarta JAXB 2.3.2:maven
  2. 解压并将内容放入我的项目目录
  3. 在 Eclipse IDE 2020-6 中,我编辑了项目的属性(Cmd+I):
  4. 在“Java构建路径”主题下,我使用“添加外部JAR”添加了3个JAR文件:jaxb-impl.jar、jakarta.xml.bind-api.jar 和 jakarta.activation.jar
  5. 显然我之前忘记了的部分:还在“顺序和导出”下,我勾选了这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:

  1. downloaded Jakarta JAXB 2.3.2 here: maven
  2. unzipped it and put the content to my project directory
  3. in Eclipse IDE 2020-6, I edited the properties of the project (Cmd+I):
  4. 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
  5. 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.

huangapple
  • 本文由 发表于 2020年8月6日 23:38:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63287075.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定