Java IntelliJ中的NoSuchMethodError,但不是Eclipse
我正在尝试使用Jetty服务器运行Jersey REST服务。当我在IntelliJ中运行时,出现以下错误:Java IntelliJ中的NoSuchMethodError,但不是Eclipse,java,eclipse,intellij-idea,gradle,Java,Eclipse,Intellij Idea,Gradle,我正在尝试使用Jetty服务器运行Jersey REST服务。当我在IntelliJ中运行时,出现以下错误: java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map; at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:309) at org
java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map;
at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:309)
at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:338)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:171)
at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:363)
at javax.servlet.GenericServlet.init(GenericServlet.java:244)
etc...
at service.WebserviceRunner.start(WebserviceRunner.java:37)
build.gradle
apply plugin: 'java'
apply plugin: 'jetty'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'war'
repositories {
mavenCentral()
}
dependencies {
compile 'org.glassfish.jersey.core:jersey-server:2.17'
compile 'org.glassfish.jersey.containers:jersey-container-servlet-core:2.17'
compile 'org.javassist:javassist:3.15.0-GA'
compile 'javax.ws.rs:javax.ws.rs-api:2.0.1'
compile 'javax.inject:javax.inject:1'
compile 'com.google.code.gson:gson:2.2.2'
compile 'com.google.guava:guava:10.0'
compile 'com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.3.0'
testCompile group: 'junit', name: 'junit', version: '4.11'
def jettyVersion = '7.2.2.v20101205';
compile "org.eclipse.jetty:jetty-server:${jettyVersion}"
compile "org.eclipse.jetty:jetty-webapp:${jettyVersion}"
compile "org.eclipse.jetty:jetty-servlet:${jettyVersion}"
compile "org.eclipse.jetty:jetty-servlets:${jettyVersion}"
compile "org.eclipse.jetty:jetty-annotations:${jettyVersion}"
compile 'org.mortbay.jetty:jsp-2.1-glassfish:2.1.v20100127'
compile 'javax.servlet:jstl:1.2'
compile 'javax.servlet:javax.servlet-api:3.0.1'
}
注意:我认为这与在我的类路径上有相同应用程序类的两个不同版本有关。为什么我只导入一个
我昨天遇到了这个问题,使用的是Maven,而不是Gradle。 您是正确的,问题是由于在同一个包中有两个具有相同类名的JAR:java.ws.rs.core.Application 我可以通过搜索依赖关系树,找到jersey版本的应用程序中的jar依赖关系,并使用Maven的排除标记排除它来修复它 在您的例子中,这是非常明显的:类加载器看到了java.ws.rs.core.Application的两个版本,并从org.glassfish.jersey.core中选择了您不想要的版本
至于为什么它在Eclipse中工作而不是在IDEA中工作,类装入器不能保证类的装入顺序,使用两个不同的类加载器可能会以不同的顺序加载类,如果存在会导致不同行为的重复,则会加载不同的类。您知道其中一个IDE还是另一个IDE使用Proguard进行代码混淆吗?这可能会有所不同……@Trinimon我在两个IDE上都运行默认设置,我不知道如何找到答案。我认为这与在同一类路径上有两个版本的类有关,Eclipse似乎选择了正确的版本,但不是IntelliJ。通常,您会在项目中的某个地方找到一个
proguard
文件。导入库的公共方法不应该被混淆,因此您通常会添加类似于-keep Public class*extends javax.ws.rs.core.*
的内容。但是看着“失踪”的班级我有另一个想法。。。(看下面的答案)@Trinimon我添加了一个屏幕截图,当我在IntelliJ中进行类型搜索时(很抱歉它太小了!),它似乎认为有几个应用程序类,javax.ws.rs-api-2.0.1有一个getProperties()方法,jsr311-api-1.1.1版本没有。是的,也可以看到:。。。。嗯-不知道:(
apply plugin: 'java'
apply plugin: 'jetty'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'war'
repositories {
mavenCentral()
}
dependencies {
compile 'org.glassfish.jersey.core:jersey-server:2.17'
compile 'org.glassfish.jersey.containers:jersey-container-servlet-core:2.17'
compile 'org.javassist:javassist:3.15.0-GA'
compile 'javax.ws.rs:javax.ws.rs-api:2.0.1'
compile 'javax.inject:javax.inject:1'
compile 'com.google.code.gson:gson:2.2.2'
compile 'com.google.guava:guava:10.0'
compile 'com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.3.0'
testCompile group: 'junit', name: 'junit', version: '4.11'
def jettyVersion = '7.2.2.v20101205';
compile "org.eclipse.jetty:jetty-server:${jettyVersion}"
compile "org.eclipse.jetty:jetty-webapp:${jettyVersion}"
compile "org.eclipse.jetty:jetty-servlet:${jettyVersion}"
compile "org.eclipse.jetty:jetty-servlets:${jettyVersion}"
compile "org.eclipse.jetty:jetty-annotations:${jettyVersion}"
compile 'org.mortbay.jetty:jsp-2.1-glassfish:2.1.v20100127'
compile 'javax.servlet:jstl:1.2'
compile 'javax.servlet:javax.servlet-api:3.0.1'
}