在我的类路径上选择了 Java 的默认 JAXB 实现

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/547599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 12:48:52  来源:igfitidea点击:

Java's default JAXB implementation is chosen over my classpath

javaxmlxsltjaxbclasspath

提问by dacracot

I've written a java application that utilizes JAXB for XSL transforms. I've included the saxon9.jar in my classpath so that I can use XSLT 2.0 rather than XSLT 1.0 on the command line.

我编写了一个 Java 应用程序,它利用 JAXB 进行 XSL 转换。我已经在我的类路径中包含了 saxon9.jar,这样我就可以在命令行上使用 XSLT 2.0 而不是 XSLT 1.0。

java -classpath ./lib/saxon9.jar:./ -jar myApp.jar

I've included code in my XSL to report the XSLT used.

我在我的 XSL 中包含了代码来报告使用的 XSLT。

<xsl:comment><xsl:text >
</xsl:text>XSLT Version: <xsl:value-of select="system-property('xsl:version')" /> <xsl:text >
</xsl:text>XSLT Vendor: <xsl:value-of select="system-property('xsl:vendor')" /> <xsl:text >
</xsl:text>XSLT Vendor URL: <xsl:value-of select="system-property('xsl:vendor-url')" /> <xsl:text >
</xsl:text></xsl:comment>

It reports.

它报告。

XSLT Version: 1.0
XSLT Vendor: Apache Software Foundation (Xalan XSLTC)
XSLT Vendor URL: http://xml.apache.org/xalan-j

This is the default implementation that is part of the JVM.

这是作为 JVM 一部分的默认实现。

How do I get it to use the Saxon that I specified?

我如何让它使用我指定的撒克逊人?



Follow up:

跟进:

So none of these methods worked (except placing the saxon jar in the endorsed directory), but they all should have worked. It seems the combination of my using the "-jar myApp.jar" and wanting an alternative XSLT implementation were incompatible. In other words...

所以这些方法都没有奏效(除了将撒克逊罐放在认可目录中),但它们都应该有效。似乎我使用“-jar myApp.jar”和想要替代 XSLT 实现的组合是不兼容的。换句话说...

java -Djavax.xml.transform.TransformerFactory=net.sf.saxon.TransformerFactoryImpl -classpath ./lib/saxon9.jar:./ -jar myApp.jar

java -Djavax.xml.transform.TransformerFactory=net.sf.saxon.TransformerFactoryImpl -classpath ./lib/saxon9.jar:./ -jar myApp.jar

...does not work, but this does...

......不起作用,但这确实......

java -Djavax.xml.transform.TransformerFactory=net.sf.saxon.TransformerFactoryImpl -classpath ./lib/saxon9.jar:./myApp.jar org.dacracot.myApp

java -Djavax.xml.transform.TransformerFactory=net.sf.saxon.TransformerFactoryImpl -classpath ./lib/saxon9.jar:./myApp.jar org.dacracot.myApp

...interestingly, I don't even have to specify the factory and I get the saxon version...

...有趣的是,我什至不必指定工厂,我就得到了撒克逊版本...

java -classpath ./lib/saxon9.jar:./myApp.jar org.dacracot.myApp

java -classpath ./lib/saxon9.jar:./myApp.jar org.dacracot.myApp

回答by kgiannakakis

Have you tried placing your jar in the endorseddirectory?

你有没有试过把你的 jar 放在认可的目录中?

If you don't want the effect to be global, use the following option:

如果您不希望效果是全局的,请使用以下选项:

-Djava.endorsed.dirs=my_dir

in the command line.

在命令行中。

回答by nbeyer

Check out the javadoc for TransformerFactory.newInstance()for all the possible ways to configure the factory.

查看TransformerFactory.newInstance()的 javadoc,了解配置工厂的所有可能方法。

  1. System Property
  2. lib/jaxp.properties
  3. Services config file (may be in the saxon JAR, /META-INF/services/javax.xml.transform.TransformerFactory)
  1. 系统属性
  2. lib/jaxp.properties
  3. 服务配置文件(可能在 saxon JAR 中,/META-INF/services/javax.xml.transform.TransformerFactory)

回答by KitsuneYMG

The last example you posted(w/o -Djavax...) works because the META-INF/services directory contains a file named

您发布的最后一个示例(w/o -Djavax...)有效,因为 META-INF/services 目录包含一个名为

java.xml.transform.TransformerFactory

java.xml.transform.TransformerFactory

with the content

与内容

net.sf.saxon.TransformerFactoryImpl

net.sf.saxon.TransformerFactoryImpl

The reason using

使用的原因

java -cp lib/saxon9.jar;myjar.jar

works and

作品和

java -cp lib/saxon9.jar -jar myjar.jar

doesn't is that using the -jar option tells java(w).exe to ignore the rest of the command line and pull everything (classpath included) from the jar's manifest file. If you put saxon9.jar in your jar manifest's classpath it should work.

不是使用 -jar 选项告诉 java(w).exe 忽略命令行的其余部分并从 jar 的清单文件中提取所有内容(包括类路径)。如果您将 saxon9.jar 放在 jar 清单的类路径中,它应该可以工作。

Manifest-Version: 1.0
Main-Class: net.my.MainClass
Class-Path: lib/saxon9.jar

回答by McDowell

This system property should set the replacement TransformerFactory:

此系统属性应设置替换 TransformerFactory:

java -Djavax.xml.transform.TransformerFactory=com.foobar.AcmeTransformer MyApp

I believe the factory class you want is net.sf.saxon.TransformerFactoryImpl

我相信你想要的工厂类是net.sf.saxon.TransformerFactoryImpl

回答by Paul Tomblin

How about

怎么样

java -Xbootclasspath:lib/saxon9.jar -classpath . -jar myApp.jar