在eclipse中运行独立的java可执行文件导致NoClassDefFound [英] running standalone java executable in eclipse results in NoClassDefFound
问题描述
main()
。它在Eclipse中编译得很好,但是当我尝试以java应用程序运行它时,我得到: 引起:java.lang .ClassNotFoundException:org.glassfish.jersey.client.JerseyClientBuilder
任何人都可以告诉我我在做什么错误?这是我的pom.xml:
< project xmlns =http://maven.apache.org/POM/4.0。 0xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation =http://maven.apache.org/POM/4.0.0 http:/ /maven.apache.org/maven-v4_0_0.xsd\">
< modelVersion> 4.0.0< / modelVersion>
< groupId> ca.ubc.iamwsClient< / groupId>
< artifactId> iamwsClient< / artifactId>
< packaging> jar< / packaging>
< version> 1.0-SNAPSHOT< / version>
< name> iamwsClient< / name>
<属性>
< jersey.version> 2.5.1< /jersey.version>
< target.dir> target< /target.dir>
< project.build.directory> target< /project.build.directory>
< / properties>
< repositories>
< repository>
< id> JBoss Repository< / id>
< url> https://repository.jboss.org/nexus/content/groups/public< / url>
< / repository>
< / repositories>
<依赖关系>对于pojo / json支持
<! - jackson依赖项 - >
<依赖关系>
< groupId> org.glassfish.jersey.media< / groupId>
< artifactId> jersey-media-json-jackson< / artifactId>
< version> $ {jersey.version}< / version>
< / dependency>
<依赖关系>
< groupId> org.glassfish.jersey.media< / groupId>
< artifactId> jersey-media-json-processing< / artifactId>
< version> $ {jersey.version}< / version>
< / dependency>
<! - end jackson deps - >
<依赖关系>
< groupId> junit< / groupId>
< artifactId> junit< / artifactId>
< version> 4.9< / version>
< scope> test< / scope>
< / dependency>
<依赖关系>
< groupId> org.glassfish.jersey.core< / groupId>
< artifactId> jersey-client< / artifactId>
< version> $ {jersey.version}< / version>
< / dependency>
<! - < dependency> <&的groupId GT; com.sun.jersey< /&的groupId GT; < artifactId的> Jersey的客户机LT; / artifactId的>
< version> 1.8< / version> < /依赖性> - >
<依赖关系>
< groupId> org.glassfish.jersey.connectors< / groupId>
< artifactId> jersey-apache-connector< / artifactId>
< version> $ {jersey.version}< / version>
< / dependency>
<依赖关系>
< groupId> javax.ws.rs< / groupId>
< artifactId> javax.ws.rs-api< / artifactId>
< version> 2.0< / version>
< / dependency>
<! - < dependency> <&的groupId GT; javax.ws.rs< /&的groupId GT; < artifactId的> JSR311-API< / artifactId的>
< version> 1.1< / version> < /依赖性> - >
< / dependencies>
< build>
< finalName> iamwsClient< / finalName>
< outputDirectory> $ {basedir} / $ {target.dir} / classes< / outputDirectory>
< plugins>
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-compiler-plugin< / artifactId>
< version> 2.5.1< / version>
< inherited> true< / inherited>
< configuration>
< source> 1.7< / source>
< target> 1.7< / target>
< / configuration>
< / plugin>
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-dependency-plugin< / artifactId>
<执行>
< execution>
< id> copy-dependencies< / id>
< phase> prepare-package< / phase>
< goals>
< goal> copy-dependencies< / goal>
< / goals>
< configuration>
< outputDirectory> $ {project.build.directory} / classes / lib< / outputDirectory>
< overWriteReleases> false< / overWriteReleases>
< overWriteSnapshots> false< / overWriteSnapshots>
< overWriteIfNewer> true< / overWriteIfNewer>
< / configuration>
< / execution>
< / executions>
< / plugin>
< plugin>
< groupId> org.eclipse.m2e< / groupId>
< artifactId>生命周期映射< / artifactId>
< configuration>
< lifecycleMappingMetadata>
< pluginExecutions>
<! - 拷贝依赖插件 - >
< pluginExecution>
< pluginExecutionFilter>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-dependency-plugin< / artifactId>
< versionRange> [0.0,)< / versionRange>
< goals>
< goal> copy-dependencies< / goal>
< / goals>
< / pluginExecutionFilter>
< action>
< ignore />
< / action>
< / pluginExecution>
< / pluginExecutions>
< / lifecycleMappingMetadata>
< / configuration>
< / plugin>
<! - < plugin>
< groupId> org.codehaus.mojo< / groupId>
< artifactId> exec-maven-plugin< / artifactId>
< version> 1.2.1< / version>
<执行>
< execution>
< goals>
< goal> java< / goal>
< / goals>
< / execution>
< / executions>
< configuration>
< mainClass> ca.ubc.iamwsClient.IamwsClient< / mainClass>
< / configuration>
< / plugin> - >
< plugin>
< artifactId> maven-assembly-plugin< / artifactId>
< configuration>
< archive>
< manifest>
< mainClass> ca.ubc.iamwsClient.IamwsClient< / mainClass>
< / manifest>
< / archive>
< descriptorRefs>
< descriptorRef> jar-with-dependencies< / descriptorRef>
< / descriptorRefs>
< / configuration>
< / plugin>
< / plugins>
< pluginManagement>
< plugins>
<! - 此插件的配置用于仅存储Eclipse m2e设置
。它对Maven构建本身没有影响。 - >
< plugin>
< groupId> org.eclipse.m2e< / groupId>
< artifactId>生命周期映射< / artifactId>
< version> 1.0.0< / version>
< configuration>
< lifecycleMappingMetadata>
< pluginExecutions>
< pluginExecution>
< pluginExecutionFilter>
< groupId>
org.apache.maven.plugins
< / groupId>
< artifactId>
maven-dependency-plugin
< / artifactId>
< versionRange>
[2.1,)
< / versionRange>
< goals>
< goal>
copy-dependencies
< / goal>
< / goals>
< / pluginExecutionFilter>
< action>
< ignore>< / ignore>
< / action>
< / pluginExecution>
< / pluginExecutions>
< / lifecycleMappingMetadata>
< / configuration>
< / plugin>
< / plugins>
< / pluginManagement>
< / build>
< / project>
我要运行的客户端:
包ca.ubc.iamwsClient;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.jackson.JacksonFeature;
import ca.ubc.iamwsClient.json.CreateEmployeeBean;
import ca.ubc.iamwsClient.json.CreateEmployeeResponseBean;
/ **
*主类。
*
* /
public class IamwsClient {
private WebTarget getTarget(String targetUrl)throws异常{
//创建客户端
客户端c = ClientBuilder.newClient()。register(JacksonFeature.class);
return c.target(targetUrl);
}
public CreateEmployeeResponseBean sendCreateEmployeeRequest(CreateEmployeeBean createBean,String url){
CreateEmployeeResponseBean rb = null;
try {
WebTarget target = getTarget(url);
WebTarget wt = target.path(employeeAutoCreate);
WebTarget generateTarget = wt.path(generate);
rb = generateTarget.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.entity(createBean,MediaType.APPLICATION_FORM_URLENCODED_TYPE),
CreateEmployeeResponseBean.class);
System.out.println(CreateEmployee:generate response:success =+ rb.getSuccess()+message =+ rb.getMessage());
} catch(Throwable t){
t.printStackTrace();
System.out.println(********* EXCEPTION THROWN:+ t.getMessage());
}
return rb;
}
public static void main(String [] args){
String testUrl =http:// localhost:9090 / iamws;
CreateEmployeeBean bean = new CreateEmployeeBean();
// ...在bean上设置一些细节....
IamwsClient client = new IamwsClient();
CreateEmployeeResponseBean response = client.sendCreateEmployeeRequest(bean,testUrl);
if(response!= null){
System.out.println(********* server has with flag:+ response.getSuccess()+消息:+ response.getMessage());
}
}
}
您需要确保包含 org.glassfish.jersey.client.JerseyClientBuilder
类的JAR依赖项在运行期间在应用程序的类路径中可用时间。对于 NoClassDefFound
错误,真的没有其他解释。
从我可以在这个类上的google,它应该是可用的在
<依赖关系>
< groupId> org.glassfish.jersey.core< / groupId>
< artifactId> jersey-client< / artifactId>
< version> 2.0-m04< / version>
< / dependency>
换句话说,你的 com.sun.jersey
依赖可能会丢失。
编辑
这是实际的答案OP后加上问题:
< jersey.version> 2.5.1< /jersey.version>
<依赖关系>
< groupId> org.glassfish.jersey.media< / groupId>
< artifactId> jersey-media-json-processing< / artifactId>
< version> $ {jersey.version}< / version>
< / dependency>
<依赖关系>
< groupId> javax.ws.rs< / groupId>
< artifactId> javax.ws.rs-api< / artifactId>
< version> 2.0< / version>
< / dependency>
他已经有$ code> org.glassfish.jersey.core 和 org.glassfish.jersey.connectors
。他删除了 com.sun.jersey
依赖关系。
我将尽快尝试这个。
Hi I've got a basic Jersey Client that I'm trying to run with a main()
in my "main" java class. It compiles fine in Eclipse, but when I try to run it as java application, I get:
Caused by: java.lang.ClassNotFoundException: org.glassfish.jersey.client.JerseyClientBuilder
Can anyone tell me what I'm doing wrong? Here's my 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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ca.ubc.iamwsClient</groupId>
<artifactId>iamwsClient</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>iamwsClient</name>
<properties>
<jersey.version>2.5.1</jersey.version>
<target.dir>target</target.dir>
<project.build.directory>target</project.build.directory>
</properties>
<repositories>
<repository>
<id>JBoss Repository</id>
<url>https://repository.jboss.org/nexus/content/groups/public</url>
</repository>
</repositories>
<dependencies>
<!-- jackson dependencies for pojo/json support -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-processing</artifactId>
<version>${jersey.version}</version>
</dependency>
<!-- end jackson deps -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
</dependency>
<!-- <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-client</artifactId>
<version>1.8</version> </dependency> -->
<dependency>
<groupId>org.glassfish.jersey.connectors</groupId>
<artifactId>jersey-apache-connector</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
<!-- <dependency> <groupId>javax.ws.rs</groupId> <artifactId>jsr311-api</artifactId>
<version>1.1</version> </dependency> -->
</dependencies>
<build>
<finalName>iamwsClient</finalName>
<outputDirectory>${basedir}/${target.dir}/classes</outputDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<!-- copy-dependency plugin -->
<pluginExecution>
<pluginExecutionFilter>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<versionRange>[0.0,)</versionRange>
<goals>
<goal>copy-dependencies</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
<!-- <plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>ca.ubc.iamwsClient.IamwsClient</mainClass>
</configuration>
</plugin> -->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>ca.ubc.iamwsClient.IamwsClient</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</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.apache.maven.plugins
</groupId>
<artifactId>
maven-dependency-plugin
</artifactId>
<versionRange>
[2.1,)
</versionRange>
<goals>
<goal>
copy-dependencies
</goal>
</goals>
</pluginExecutionFilter>
<action>
<ignore></ignore>
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Client that I'm trying to run:
package ca.ubc.iamwsClient;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.jackson.JacksonFeature;
import ca.ubc.iamwsClient.json.CreateEmployeeBean;
import ca.ubc.iamwsClient.json.CreateEmployeeResponseBean;
/**
* Main class.
*
*/
public class IamwsClient {
private WebTarget getTarget(String targetUrl) throws Exception {
// create the client
Client c = ClientBuilder.newClient().register(JacksonFeature.class);
return c.target(targetUrl);
}
public CreateEmployeeResponseBean sendCreateEmployeeRequest(CreateEmployeeBean createBean, String url) {
CreateEmployeeResponseBean rb = null;
try {
WebTarget target = getTarget(url);
WebTarget wt = target.path("employeeAutoCreate");
WebTarget generateTarget = wt.path("generate");
rb = generateTarget.request(MediaType.APPLICATION_JSON_TYPE).post(Entity.entity(createBean,MediaType.APPLICATION_FORM_URLENCODED_TYPE),
CreateEmployeeResponseBean.class);
System.out.println("CreateEmployee: generate response: success=" + rb.getSuccess() + " message=" + rb.getMessage());
} catch (Throwable t) {
t.printStackTrace();
System.out.println("*********EXCEPTION THROWN: " + t.getMessage());
}
return rb;
}
public static void main(String[] args) {
String testUrl="http://localhost:9090/iamws";
CreateEmployeeBean bean = new CreateEmployeeBean();
// ... set some details on bean....
IamwsClient client = new IamwsClient();
CreateEmployeeResponseBean response = client.sendCreateEmployeeRequest(bean, testUrl);
if (response != null) {
System.out.println("*********server responded with flag: " + response.getSuccess() + " message: " + response.getMessage());
}
}
}
You need to make sure that the JAR dependency containing org.glassfish.jersey.client.JerseyClientBuilder
class is available on your application's classpath during the run time. There's really no other explanation to NoClassDefFound
error.
From what I can google on this class, it should be available in
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.0-m04</version>
</dependency>
In other words your com.sun.jersey
dependency may be missing it.
EDIT
This is the actual answer which the OP added to the Question afterwards:
<jersey.version>2.5.1</jersey.version>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-processing</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
He already had org.glassfish.jersey.core
and org.glassfish.jersey.connectors
. He removed com.sun.jersey
dependency.
I am going to try this myself shortly.
这篇关于在eclipse中运行独立的java可执行文件导致NoClassDefFound的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!