编译jar时发生java.lang.NoSuchMethodError

编译jar时发生java.lang.NoSuchMethodError,java,maven,google-vision,Java,Maven,Google Vision,大家好! 我使用google vision API,当我用IntelliJ Idea运行我的程序时,它工作得很好,但当我编译jar文件时,它在处理照片时会出错 java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;CLjava/lang/Object;)V at io.grpc.Metadata$Key.validateName(Meta

大家好! 我使用google vision API,当我用IntelliJ Idea运行我的程序时,它工作得很好,但当我编译jar文件时,它在处理照片时会出错

java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;CLjava/lang/Object;)V
        at io.grpc.Metadata$Key.validateName(Metadata.java:629)
        at io.grpc.Metadata$Key.<init>(Metadata.java:637)
        at io.grpc.Metadata$Key.<init>(Metadata.java:567)
        at io.grpc.Metadata$AsciiKey.<init>(Metadata.java:742)
        at io.grpc.Metadata$AsciiKey.<init>(Metadata.java:737)
        at io.grpc.Metadata$Key.of(Metadata.java:593)
        at io.grpc.Metadata$Key.of(Metadata.java:589)
        at com.google.api.gax.grpc.GrpcHeaderInterceptor.<init>(GrpcHeaderInterceptor.java:60)
        at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createSingleChannel(InstantiatingGrpcChannelProvider.java:212)
        at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.createChannel(InstantiatingGrpcChannelProvider.java:185)
        at com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.getTransportChannel(InstantiatingGrpcChannelProvider.java:177)
        at com.google.api.gax.rpc.ClientContext.create(ClientContext.java:160)
        at com.google.cloud.vision.v1.stub.GrpcImageAnnotatorStub.create(GrpcImageAnnotatorStub.java:114)
        at com.google.cloud.vision.v1.stub.ImageAnnotatorStubSettings.createStub(ImageAnnotatorStubSettings.java:151)
        at com.google.cloud.vision.v1.ImageAnnotatorClient.<init>(ImageAnnotatorClient.java:136)
        at com.google.cloud.vision.v1.ImageAnnotatorClient.create(ImageAnnotatorClient.java:117)
        at allClasses.GoogleAPI.detectText(GoogleAPI.java:26)
        at allClasses.MainClass.addMarkingPhoto(MainClass.java:129)
        at allClasses.MainClass.dir(MainClass.java:95)
        at allClasses.MainClass.dir(MainClass.java:86)
        at allClasses.MainClass.main(MainClass.java:50)
我了解到这可能是由于依赖关系造成的,但在maven中我是新手,无法理解为什么它在intellij中工作,在创建jar文件时不工作

我的pom:

<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>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.2</version>
                <configuration>
                    <archive>
                        <manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
                    </archive>
                </configuration>
            </plugin>
            <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>
        </plugins>
    </build>

    <properties>
        <poi.version>3.15</poi.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>com.google.apis</groupId>
            <artifactId>google-api-services-drive</artifactId>
            <version>v3-rev110-1.23.0</version>
        </dependency>


        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>28.2-jre</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.8</version>
        </dependency>

        <dependency>
            <groupId>com.google.api-client</groupId>
            <artifactId>google-api-client</artifactId>
            <version>1.23.0</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.8.7</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.8.7</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.8.7</version>
        </dependency>

        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
            <version>3.10.2</version>
        </dependency>

        <dependency>
            <groupId>com.google.oauth-client</groupId>
            <artifactId>google-oauth-client-jetty</artifactId>
            <version>1.23.0</version>
        </dependency>

        <dependency>
            <groupId>com.mailjet</groupId>
            <artifactId>mailjet-client</artifactId>
            <version>4.2.1</version>
        </dependency>

        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.6.2</version>
        </dependency>

        <dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>libraries-bom</artifactId>
            <version>3.2.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>


        <dependency>
            <groupId>com.google.cloud</groupId>
            <artifactId>google-cloud-vision</artifactId>
            <version>1.84.0</version>
        </dependency>

        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>3.9.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.9</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.9</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.9</version>
        </dependency>

        <dependency>
            <groupId>com.mashape.unirest</groupId>
            <artifactId>unirest-java</artifactId>
            <version>1.4.9</version>
        </dependency>

    </dependencies>

    <groupId>org.example</groupId>
    <artifactId>MyProject</artifactId>
    <version>1.0-SNAPSHOT</version>

</project>
我删除了清单并插入了以下内容:

 <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>allClasses.MainClass</mainClass>
                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                            <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>8</source>
                    <target>8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
但一切都没有改变

有什么问题吗?
为什么代码在intellij中工作而在创建jar文件后不工作?

编译jar文件时,需要用jar文件打包所需的依赖项。您可以尝试探索以下配置。它所做的是将所需的依赖项添加到jar本身,当您尝试运行jar时,它会找到所有与jar本身的依赖项。这里的api都是依赖项,我想将其包含在jar本身中。它具有maven依赖性

       <dependency>
            <groupId>antlr</groupId>
            <artifactId>antlr</artifactId>
            <version>2.7.7</version>
            <scope>provided</scope>
        </dependency>




    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Bundle-SymbolicName>com.your.package</Bundle-SymbolicName>
                        <Export-Package>com.your.package.*;org.apache.commons.beanutils.*</Export-Package>
                        <Embed-Dependency>api-all,commons-pool,mina-core,antlr</Embed-Dependency>
                        <Embed-Transitive>true</Embed-Transitive>
                        <DynamicImport-Package>
                            org.dom4j, org.dom4j.io, org.jdom, org.jdom.input
                        </DynamicImport-Package>
                        <Import-Package>
                            !org.dom4j.*, !org.xmlpull.v1, !sun.net.util, *
                        </Import-Package>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>
或者,您尝试使用java-cp.:.jar:.jar在命令行本身中提供依赖jar的类路径


进一步阅读

我看到您引用的是一个精确的清单文件,了解其中写入的内容将非常有用,因为我想您知道,清单文件中既有启动类,也有依赖项列表及其位置。 POM文件没有显式地构造具有依赖关系的jar,因此我假设您构建的包不包含使用它所必需的依赖关系

因此,您有两种选择:

1通过maven plugin创建一个多功能包,其中包含使用所需的所有依赖项,用maven assembly plugin替换maven jar插件:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <mainClass>{full qualified name of main class}</mainClass>      
                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
            </manifest>
        </archive>
    </configuration>
</plugin>

2通过参数-Djava.library.path,-cp或类似的参数引用外部依赖项

我只是在eclipse中创建jar=

请将stacktrace添加为文本而不是图像。查看maven依赖项层次结构,查看是否有任何依赖项覆盖了包含com.google的主依赖项。common@Lino很抱歉我是编辑post@reedb89我只看到com.google.api:api common:1.8.1和com.google.api.grpc:photo google common protos:1.16.0My manifest:manifest Version:1.0 Main Class:allClasses.MainClass我添加了你上面写的东西,而不是maven jar插件,但是错误remained@Zeo好的,我的建议仍然有效。您的清单是无用的,请删除它并让它通过maven生成。请注意,通过这种方式,您将在目标文件夹中找到2个jar文件:1个原始文件,1个具有依赖项;obvs你必须运行大的一个!