我需要在项目中使用web服务。我使用NetBeans,所以我右键单击我的项目,并尝试添加一个新的“Web服务客户端”。上次我检查时,这是创建web服务客户机的方法。但它导致了一个AssertionError,说:

java.lang.AssertionError: org.xml.sax.SAXParseException;systemId: jar文件:/道路/ / glassfish /模块/ jaxb-osgi.jar ! / com/sun/tools/xjc/reader/xmlschema/bindinfo/binding.xsd;lineNumber: 52个;columnNumber: 88;schema_reference:读取架构文档'xjc失败。由于accessExternalSchema属性设置的限制,不允许访问“文件”。

NetBeans的默认Java平台是JDK8 (Oracle的官方版本),所以当我更改NetBeans .conf文件并将JDK7(也来自Oracle)作为我的默认平台时,一切都工作得很好。所以我认为问题出在JDK8上。这是我的java -version输出:

Java版本“1.8.0” Java(TM) SE运行时环境(build 1.8.0-b132) Java HotSpot(TM) 64位服务器虚拟机(build 25.0-b70,混合模式)

目前,我将JDK7作为默认的Java平台。如果有让JDK8工作的方法,请分享。


当前回答

创建一个名为jaxp的文件。“JDK version/jre/lib”路径下的properties(如果不存在),然后在其中添加以下行。

javax.xml.accessExternalSchema = all

其他回答

我在Eclipse IDE (4.4, Luna, Windows 7 x64)中运行蚂蚁构建。与其修改已安装的JRE库或任何ant脚本(我有多个项目,它们的构建中包含XJC),我更喜欢更改Eclipse设置“外部工具配置”,并将以下内容添加到ant构建配置的VM参数中:

-Djavax.xml.accessExternalSchema=all

下面的代码适用于jdk 1.8.0_66中包含的wsimport 2.2.9:

wsimport -J-Djavax.xml.accessExternalSchema=all ....

我在2.4版的artifact org.codehaus.mojo上测试了这个功能,效果很好~

<plugins>
    <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>jaxws-maven-plugin</artifactId>
        <version>2.4.1</version>
        <executions>
            <execution>

                <goals>
                    <goal>wsimport</goal>
                </goals>
                <configuration>
                    <wsdlDirectory>path/to/dir/wsdl</wsdlDirectory>
                </configuration>
                <id>wsimport-web-service</id>
                <phase>generate-sources</phase>
            </execution>
        </executions>
        <dependencies>
            <dependency>
                <groupId>javax.xml</groupId>
                <artifactId>webservices-api</artifactId>
                <version>${webservices-api-version}</version>
            </dependency>
        </dependencies>
        <configuration>
            <vmArgs>
                <vmArg>-Djavax.xml.accessExternalSchema=all</vmArg>
            </vmArgs>
            <sourceDestDir>generated-sources/jaxws-wsimport</sourceDestDir>
            <xnocompile>true</xnocompile>
            <verbose>true</verbose>
            <extension>true</extension>
            <sei>/</sei>
        </configuration>
    </plugin>
</plugins>

如果你正在使用ant,你可以在你的java调用中添加一个jvmarg:

<jvmarg value="-Djavax.xml.accessExternalSchema=all" />

我在一个常规的maven项目中使用了它,并通过运行xjc插件的插件依赖配置解决了这个问题:

     <plugin>
        <!-- Needed to run the plugin xjc en Java 8 or superior -->
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>properties-maven-plugin</artifactId>
        <version>1.0-alpha-2</version>
        <executions>
            <execution>
                <id>set-additional-system-properties</id>
                <goals>
                    <goal>set-system-properties</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <properties>
                <property>
                    <name>javax.xml.accessExternalSchema</name>
                    <value>all</value>
                </property>
                <property>
                    <name>javax.xml.accessExternalDTD</name>
                    <value>all</value>
                </property>
            </properties>
        </configuration>
    </plugin>