使用OpenJDK 11运行JAXB xjc编译器
如文章“ 要從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一起全部刪除 。 這篇文章探討了在OpenJDK 11中使用JAXB的xjc編譯器 。
由于JDK 11不再包含JAXB的實現,因此必須單獨購買一個。 在本文中,我將使用2.3.0版的JAXB參考實現 。 本文中使用的JDK版本是JDK 11.0.2 General-Availability Release 。
在不帶參數的情況下運行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 theCompatibility 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 referencessupport 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 informationExtensions:-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 classesxjc編譯器腳本(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 。 不幸的是,不能簡單地通過-classpath將JAXB參考實現JAR添加到類路徑中,因為使用java -jar 運行可執行的JAR僅遵循通過MANIFEST.MF的Class-Path (該條目存在于jaxb-ri-2.3.0.jar作為“ Class-Path: jaxb-core.jar jaxb-impl.jar ”)。
一種解決方法是修改腳本以將JAR用作常規JAR(不帶-jar )并顯式執行類XJCFacade ,以便可以將類路徑顯式提供給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應用程序框架 ( 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 11运行JAXB xjc编译器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 国铁集团:不可能给第三方平台优先购票权
- 下一篇: 小米商城推出“特惠屏”活动,小米 13、