首页 > 解决方案 > NoSuchMethodError 使用 maven

问题描述

当我运行我的 jar 时,它会引发异常。我反编译了jar,发现方法存在。我尝试在我自己的 pom 中添加 Gson 和改造依赖项,但没有成功。我错过了什么?

java.lang.NoSuchMethodError: com.google.gson.Gson.newJsonReader(Ljava/io/Reader;)Lcom/google/gson/stream/JsonReader;
    at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:38) ~[?:?]
    at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:27) ~[?:?]
    at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:243) ~[?:?]
    at retrofit2.OkHttpCall.execute(OkHttpCall.java:204) ~[?:?]
    at com.jaredsburrows.retrofit2.adapter.synchronous.SynchronousBodyCallAdapter.adapt(SynchronousBodyCallAdapter.java:27) ~[?:?]

我已经阅读了无数关于相同/相似问题的线程,但没有一个有助于解决这个问题。这是我现在的pom:

...

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>org.spigotmc</groupId>
        <artifactId>spigot</artifactId>
        <version>1.8.8-R0.1-SNAPSHOT</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.github.theminecoder</groupId>
        <artifactId>agones-java-sdk</artifactId>
        <version>1.0.2</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>8</source>
                <target>8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.4</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/</exclude>
                                </excludes>
                            </filter>
                        </filters>
                        <keepDependenciesWithProvidedScope>false</keepDependenciesWithProvidedScope>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Maven 依赖:树输出:

[INFO] com.mycompany:artifact:jar:1.0-SNAPSHOT
[INFO] +- org.spigotmc:spigot:jar:1.8.8-R0.1-SNAPSHOT:provided
[INFO] \- com.github.theminecoder:agones-java-sdk:jar:1.0.2:compile
[INFO]    +- com.squareup.retrofit2:retrofit:jar:2.8.2:compile
[INFO]    |  \- com.squareup.okhttp3:okhttp:jar:3.14.9:compile
[INFO]    |     \- com.squareup.okio:okio:jar:1.17.2:compile
[INFO]    +- com.jaredsburrows.retrofit:retrofit2-synchronousadapter:jar:0.5.0:compile
[INFO]    \- com.squareup.retrofit2:converter-gson:jar:2.8.2:compile
[INFO]       \- com.google.code.gson:gson:jar:2.8.5:compile
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

标签: javamavengson

解决方案


推荐阅读