openjdk安装
如文章“要从Java 11中删除的API ”所述,JDK 11不再包含JAXB实现。 在本文中,我将结合使用JAXB (用于XML绑定的Java体系结构)参考实现提供的xjc编译器和OpenJDK 11,将XML模式文件编译成Java类。
在Java SE 6之前,想要与Java SE应用程序一起使用JAXB的开发人员需要单独获取JAXB实现,因为Java发行版未提供该实现。 从Java SE 6开始,Java包含一个JAXB实现。 这在许多情况下很方便,但是当开发人员希望使用比JDK所提供的版本更高或不同的JAXB实现时,事情变得有些困难。 当OpenJDK 9引入了模块化时, JAXB实现被移到了java.xml.bind模块中,并标记为不推荐删除。 JAXB实现与JDK 11一起全部删除。 这篇文章探讨了将JAXB的xjc编译器与OpenJDK 11一起使用。
由于JDK 11不再包括JAXB的实现,因此必须单独购买一个。 在本文中,我将使用2.3.0版的JAXB参考实现。 本文中使用的JDK版本是JDK 11.0.2 General-Availability Release 。
<div>
<img width="963" height="195" src="https://s2.51cto.com/images/blog/202410/04110706_66ff5bda3ae9425908.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_30,g_se,x_10,y_10,shadow_20,type_ZmFuZ3poZW5naGVpdGk=" alt="">
</div>
在不带参数的情况下运行xjc
脚本会导致帮助/使用情况呈现到标准输出中。
Usage: xjc [-options ...] <schema file/URL/dir/jar> ... [-b <bindinfo>] ...
If dir is specified, all schema files in it will be compiled.
If jar is specified, /META-INF/sun-jaxb.episode binding file will be compiled.
Options:
-nv : do not perform strict validation of the input schema(s)
-extension : allow vendor extensions - do not strictly follow the
Compatibility Rules and App E.2 from the JAXB Spec
-b <file/dir> : specify external bindings files (each <file> must have its own -b)
If a directory is given, **/*.xjb is searched
-d <dir> : generated files will go into this directory
-p <pkg> : specifies the target package
-m <name> : generate module-info.java with given Java module name
-httpproxy <proxy> : set HTTP/HTTPS proxy. Format is [user[:password]@]proxyHost:proxyPort
-httpproxyfile <f> : Works like -httpproxy but takes the argument in a file to protect password
-classpath <arg> : specify where to find user class files
-catalog <file> : specify catalog files to resolve external entity references
support TR9401, XCatalog, and OASIS XML Catalog format.
-readOnly : generated files will be in read-only mode
-npa : suppress generation of package level annotations (**/package-info.java)
-no-header : suppress generation of a file header with timestamp
-target (2.0|2.1) : behave like XJC 2.0 or 2.1 and generate code that doesnt use any 2.2 features.
-encoding <encoding> : specify character encoding for generated source files
-enableIntrospection : enable correct generation of Boolean getters/setters to enable Bean Introspection apis
-disableXmlSecurity : disables XML security features when parsing XML documents
-contentForWildcard : generates content property for types with multiple xs:any derived elements
-xmlschema : treat input as W3C XML Schema (default)
-dtd : treat input as XML DTD (experimental,unsupported)
-wsdl : treat input as WSDL and compile schemas inside it (experimental,unsupported)
-verbose : be extra verbose
-quiet : suppress compiler output
-help : display this help message
-version : display version information
-fullversion : display full version information
Extensions:
-Xinject-code : inject specified Java code fragments into the generated code
-Xlocator : enable source location support for generated code
-Xsync-methods : generate accessor methods with the 'synchronized' keyword
-mark-generated : mark the generated code as @javax.annotation.Generated
-episode <FILE> : generate the episode file for separate compilation
-Xpropertyaccessors : Use XmlAccessType PROPERTY instead of FIELD for generated classes
xjc
编译器脚本(bash文件和DOS批处理文件)可以方便地调用jaxb-xjc.jar
。 脚本将其作为可执行JAR ( java -jar )调用,如以下摘录所示:
- Windows版本(
xjc.bat
):%JAVA% %XJC_OPTS% -jar "%JAXB_HOME%\lib\jaxb-xjc.jar" %*
- Linux版本(
xjc.sh
):exec "$JAVA" $XJC_OPTS -jar "$JAXB_HOME/lib/jaxb-xjc.jar" "$@"
如上面的脚本摘录所示,Java启动器的调用中包含一个环境变量XJC_OPTS
。 不幸的是,JAXB参考实现JAR不能简单地通过-classpath
添加到类路径中,因为使用java -jar
运行可执行的JAR仅遵循通过MANIFEST.MF
的Class-Path
(该条目存在于jaxb-ri-2.3.0.jar
作为“ Class-Path: jaxb-core.jar jaxb-impl.jar
”)。
一种解决方法是修改脚本以将JAR用作常规JAR(不带-jar
)并显式执行XXCFacade类,以便可以将类路径显式提供给Java启动器。 Windows xjc.bat
脚本xjc.bat
进行了演示:
%JAVA% -cp C:\lib\javax.activation-api-1.2.0.jar;C:\jaxb-ri-2.3.0\lib\jaxb-xjc.jar com.sun.tools.xjc.XJCFacade %*
除了JAXB参考实现JAR javax.activation-api-1.2.0.jar
,我还需要在类路径中包括javax.activation-api-1.2.0.jar
JAR,因为JavaBeans Application Framework ( JAF )是JDK也不再提供的依赖项(通过删除JAXB的同一JEP 320删除了)。
当然,也可能根本不使用XJC脚本,而是直接运行Java启动器。 该脚本确保设置了环境变量JAXB_HOME
。 该环境变量应指向JAXB参考实现扩展到的目录。
通过这些更改,可以使用JDK 11在命令行上针对XSD执行JAXB xjc
编译器。
翻译自: https://www.javacodegeeks.com/2019/01/running-jaxb-xjc-compiler-with-openjdk-11.html
openjdk安装