我正在 Eclipse 中开发一个 Web 服务客户端。我使用 maven 的 wsimport 目标为 Web 服务生成了工件(参见下面的 pom.xml),然后在 main 方法中编写了一个像这样的简单客户端:
TeamsService service = new TeamsService();
Teams port = service.getTeamsPort();
List<Team> teams = port.getTeams();
for (Team team : teams) {
System.out.println("Team name: " + team.getName() + " (roster count: " + team.getRosterCount() + ")");
for (Player player : team.getPlayers())
System.out.println(" Player: " + player.getNickname());
}
不幸的是,当我在 Eclipse 中将项目作为 Java 应用程序运行时,我得到了 ClassNotFoundException。我尝试使用 exec:java 目标执行客户端,它工作得很好,所以我猜问题可能在于 eclipse 配置(例如 m2e 插件或类路径)。
我真的很感激任何帮助。
PS:我是新手,所以也许我在这里缺少一些基本的东西。
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.foo</groupId>
<artifactId>JWSClients</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.2.8</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jvnet.jax-ws-commons</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
</execution>
</executions>
<configuration>
<wsdlUrls>
<wsdlUrl>http://localhost:8888/teams?wsdl</wsdlUrl>
</wsdlUrls>
<sourceDestDir>${basedir}/src/main/java</sourceDestDir>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>
org.jvnet.jax-ws-commons
</groupId>
<artifactId>
jaxws-maven-plugin
</artifactId>
<versionRange>
[2.3,)
</versionRange>
<goals>
<goal>wsimport</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<properties>
<encoding>UTF-8</encoding>
</properties>
</project>
堆栈跟踪:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/istack/localization/Localizable
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at com.sun.xml.ws.client.WSServiceDelegate.<init>(WSServiceDelegate.java:208)
at com.sun.xml.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:112)
at javax.xml.ws.Service.<init>(Service.java:77)
at ch01.team.TeamsService.<init>(TeamsService.java:42)
at ch01.team.Client.main(Client.java:7)
Caused by: java.lang.ClassNotFoundException: com.sun.istack.localization.Localizable
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 29 more
执行 exec:java 时的 Maven 日志
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building JWSClients 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[WARNING] The POM for org.eclipse.m2e:lifecycle-mapping:jar:1.0.0 is missing, no dependency information available
[WARNING] Failed to retrieve plugin descriptor for org.eclipse.m2e:lifecycle-mapping:1.0.0: Plugin org.eclipse.m2e:lifecycle-mapping:1.0.0 or one of its dependencies could not be resolved: Failure to find org.eclipse.m2e:lifecycle-mapping:jar:1.0.0 in http://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced
[INFO]
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ JWSClients >>>
[INFO]
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ JWSClients <<<
[INFO]
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ JWSClients ---
Team name: Abbott and Costello (roster count: 2)
Player: Bud
Player: Lou
Team name: Marx Brothers (roster count: 3)
Player: Chico
Player: Groucho
Player: Harpo
Team name: Burns and Allen (roster count: 2)
Player: George
Player: Gracie
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.561s
[INFO] Finished at: Sat Aug 03 16:52:19 COT 2013
[INFO] Final Memory: 14M/154M
[INFO] ------------------------------------------------------------------------