通过 wsimport 可执行 Web 服务客户端时出现 java 异常

标签 java web-services jakarta-ee

我正在关注This Tutorial

我创建了这个简单的网络服务

package com.examples.services;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class HelloWeb {

    @WebMethod
    public String sayHi(String name) {
        return "Hi " + name;
    }
}

还有这个简单的服务器:

package com.examples.services;

import javax.xml.ws.Endpoint;

public class Server {
    public static void main(String args[]) {
        Endpoint.publish("http://localhost:9898/HelloWeb", new HelloWeb());
        System.out.println("here we go");
    }
}

当我测试获取 WSDL 时,它运行良好,我得到了以下 xml 结果:

<?xml version="1.0" encoding="UTF-8" ?> 
- <!--  Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. 
  --> 
- <!--  Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. 
  --> 
- <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://services.examples.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://services.examples.com/" name="HelloWebService">
- <types>
- <xsd:schema>
  <xsd:import namespace="http://services.examples.com/" schemaLocation="http://localhost:9898/HelloWeb?xsd=1" /> 
  </xsd:schema>
  </types>
- <message name="sayHi">
  <part name="parameters" element="tns:sayHi" /> 
  </message>
- <message name="sayHiResponse">
  <part name="parameters" element="tns:sayHiResponse" /> 
  </message>
- <portType name="HelloWeb">
- <operation name="sayHi">
  <input message="tns:sayHi" /> 
  <output message="tns:sayHiResponse" /> 
  </operation>
  </portType>
- <binding name="HelloWebPortBinding" type="tns:HelloWeb">
  <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" /> 
- <operation name="sayHi">
  <soap:operation soapAction="" /> 
- <input>
  <soap:body use="literal" /> 
  </input>
- <output>
  <soap:body use="literal" /> 
  </output>
  </operation>
  </binding>
- <service name="HelloWebService">
- <port name="HelloWebPort" binding="tns:HelloWebPortBinding">
  <soap:address location="http://localhost:9898/HelloWeb" /> 
  </port>
  </service>
  </definitions>

但是当我尝试创建这样的 Web 服务客户端时:

<project name="My Web Service Build" default="wsimport">

    <target name="wsimport">

        <exec executable="wsimport">

            <arg line="-keep -s ./src -p com.examples.services.client http://localhost:9898/HelloWeb?wsdl"/>

        </exec>           

    </target>



</project>

运行该客户端时出现此异常:

Buildfile: C:\Users\User\workspace\MyWebService\wsbuild.xml
wsimport:

BUILD FAILED
C:\Users\User\workspace\MyWebService\wsbuild.xml:5: Execute failed: java.io.IOException: Cannot run program "wsimport": CreateProcess error=2, The system cannot find the file specified

Total time: 646 milliseconds

解决办法是什么 我对网络服务非常陌生

编辑后

enter image description here

最佳答案

在您的环境变量中,确保 jdk bin 文件夹位于您的路径上。请按照以下说明操作:http://java.com/en/download/help/path.xml

该文件夹类似于 C:\Program Files\java\jdk1.7.0_09\bin

您可以通过打开一个新的命令提示符窗口并输入 java -version 进行测试,如果有效,则 bin 文件夹位于您的路径上。

还要检查 javac -version 是否也有效。如果第一个有效,但这个不行,则可能意味着您的路径上有 JRE 而不是 JDK。

<小时/>

执行以下操作:

  1. 在 Eclipse Window->Preferences->Installed JREs 中,找到您已安装的 JDK。
  2. 打开环境属性(右键单击我的计算机 -> 属性 -> 高级,然后单击环境变量)。
  3. 在系统变量中找到路径变量并点击编辑。
  4. 请注意,此路径变量是用分号分隔的文件夹列表。找到现有的 Java 并将其删除。然后将步骤 1 中在 Eclipse 中找到的 JDK 位置附加到末尾,后跟/bin。

例如mypathvar;C:\Program Files\Java\jdk1.6.0_37\bin

点击确定,然后打开一个新的命令提示符并输入javac -version,它现在应该可以工作,但您需要打开一个新的命令提示符。如果它不起作用,请谷歌如何将 java 添加到我的路径变量中。

如果你在 eclipse 中找不到 JDK,你可以随时下载一个(google“下载 JDK”)

关于通过 wsimport 可执行 Web 服务客户端时出现 java 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14986998/

相关文章:

java - Spring-WS:尝试使用 WebServicesParserFactory 设置 NameSpaceAware 时出现 NullPointerException

asp.net - WCF 服务绑定(bind)设置

java - NsdManager 不解析多个发现的服务

java - 如何让 if 语句搜索分隔符?

java - 从数据库查找表填充所有下拉字段

java - 如何在 JAX-WS Web 服务中获取经过身份验证的用户信息?

java - Logback审计问题

java - Java EE 6 中的类文件 javax/mail/MessagingException 中的方法中缺少代码属性,该属性不是 native 或抽象的

java - 非 JSR299 bean 无法代理生产者方法

java - Java如何对数据集进行排序