我正在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插件或类路径)。
我真的很感激任何帮助。
P.S:我是新手,所以也许我错过了一些基本的东西。的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时的Maven日志:java
[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] ------------------------------------------------------------------------
答案 0 :(得分:0)
我在一周前创建了这个相同的Web服务。我在Eclipse中创建了一个新项目并创建了包ch01.teams.service和ch01.teams.client(包名称与书中略有不同,但同样的想法)。这比处理Maven容易得多,而且对我有用。
如果你想继续使用Maven,你可能只需要将com.sun.istack.localization.Localizable放到你的类路径中(如果你有或没有,我不能从描述中得到确认)。如果你转到http://www.jarfinder.com/index.php/java/info/com.sun.istack.localization.Localizable,你可以看到它在jaxb-impl-2.1.8.jar中,所以如果你可以下载那个jar并把它放在你的类路径中,你可能会很高兴。