java.lang.NoSuchMethodError when compiling jar

huangapple go评论62阅读模式
英文:

java.lang.NoSuchMethodError when compiling jar

问题

翻译好的内容如下:

大家好!
我使用 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)
    ...

我看到说这可能是由于依赖问题,但我对 Maven 不熟悉,不明白为什么在 IntelliJ 中可以工作,但创建 JAR 文件后不行。

我的 pom.xml:

(你的 POM 文件的内容)

我删除了原来的清单并插入了以下内容:

(你修改后的 POM 文件内容)

但问题仍未解决。

可能的问题是什么?
为什么代码在 IntelliJ 中工作,在创建 JAR 文件后却不工作?

请注意,以上内容是对你提供的问题和代码片段的翻译,没有包含任何额外的回答或解释。如果你有更多问题或需要进一步的帮助,请随时提问。

英文:

Good day to all!
I work with google vision API and when I run my program in the IntelliJ Idea, it works great, but when I compile a jar file, it gives an error when processing photos

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.&lt;init&gt;(Metadata.java:637)
        at io.grpc.Metadata$Key.&lt;init&gt;(Metadata.java:567)
        at io.grpc.Metadata$AsciiKey.&lt;init&gt;(Metadata.java:742)
        at io.grpc.Metadata$AsciiKey.&lt;init&gt;(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.&lt;init&gt;(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.&lt;init&gt;(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)

I read that this may be due to dependencies, but in maven I am new to and can not understand why it works in intellij and does not work when creating a jar file

My pom :

&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.1.2&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;archive&gt;
                        &lt;manifestFile&gt;src/main/resources/META-INF/MANIFEST.MF&lt;/manifestFile&gt;
                    &lt;/archive&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.8.1&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;source&gt;8&lt;/source&gt;
                    &lt;target&gt;8&lt;/target&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;

    &lt;properties&gt;
        &lt;poi.version&gt;3.15&lt;/poi.version&gt;
    &lt;/properties&gt;

    &lt;dependencies&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;com.google.apis&lt;/groupId&gt;
            &lt;artifactId&gt;google-api-services-drive&lt;/artifactId&gt;
            &lt;version&gt;v3-rev110-1.23.0&lt;/version&gt;
        &lt;/dependency&gt;


        &lt;dependency&gt;
            &lt;groupId&gt;com.google.guava&lt;/groupId&gt;
            &lt;artifactId&gt;guava&lt;/artifactId&gt;
            &lt;version&gt;28.2-jre&lt;/version&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.httpcomponents&lt;/groupId&gt;
            &lt;artifactId&gt;httpcore&lt;/artifactId&gt;
            &lt;version&gt;4.4.8&lt;/version&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;com.google.api-client&lt;/groupId&gt;
            &lt;artifactId&gt;google-api-client&lt;/artifactId&gt;
            &lt;version&gt;1.23.0&lt;/version&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
            &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
            &lt;version&gt;2.8.7&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
            &lt;artifactId&gt;jackson-annotations&lt;/artifactId&gt;
            &lt;version&gt;2.8.7&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
            &lt;artifactId&gt;jackson-core&lt;/artifactId&gt;
            &lt;version&gt;2.8.7&lt;/version&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;com.auth0&lt;/groupId&gt;
            &lt;artifactId&gt;java-jwt&lt;/artifactId&gt;
            &lt;version&gt;3.10.2&lt;/version&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;com.google.oauth-client&lt;/groupId&gt;
            &lt;artifactId&gt;google-oauth-client-jetty&lt;/artifactId&gt;
            &lt;version&gt;1.23.0&lt;/version&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;com.mailjet&lt;/groupId&gt;
            &lt;artifactId&gt;mailjet-client&lt;/artifactId&gt;
            &lt;version&gt;4.2.1&lt;/version&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;com.sun.mail&lt;/groupId&gt;
            &lt;artifactId&gt;javax.mail&lt;/artifactId&gt;
            &lt;version&gt;1.6.2&lt;/version&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;com.google.cloud&lt;/groupId&gt;
            &lt;artifactId&gt;libraries-bom&lt;/artifactId&gt;
            &lt;version&gt;3.2.0&lt;/version&gt;
            &lt;type&gt;pom&lt;/type&gt;
            &lt;scope&gt;import&lt;/scope&gt;
        &lt;/dependency&gt;


        &lt;dependency&gt;
            &lt;groupId&gt;com.google.cloud&lt;/groupId&gt;
            &lt;artifactId&gt;google-cloud-vision&lt;/artifactId&gt;
            &lt;version&gt;1.84.0&lt;/version&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;com.squareup.okhttp3&lt;/groupId&gt;
            &lt;artifactId&gt;okhttp&lt;/artifactId&gt;
            &lt;version&gt;3.9.0&lt;/version&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.poi&lt;/groupId&gt;
            &lt;artifactId&gt;poi&lt;/artifactId&gt;
            &lt;version&gt;3.9&lt;/version&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.poi&lt;/groupId&gt;
            &lt;artifactId&gt;poi-scratchpad&lt;/artifactId&gt;
            &lt;version&gt;3.9&lt;/version&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.poi&lt;/groupId&gt;
            &lt;artifactId&gt;poi-ooxml&lt;/artifactId&gt;
            &lt;version&gt;3.9&lt;/version&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;org.apache.commons&lt;/groupId&gt;
            &lt;artifactId&gt;commons-lang3&lt;/artifactId&gt;
            &lt;version&gt;3.9&lt;/version&gt;
        &lt;/dependency&gt;

        &lt;dependency&gt;
            &lt;groupId&gt;com.mashape.unirest&lt;/groupId&gt;
            &lt;artifactId&gt;unirest-java&lt;/artifactId&gt;
            &lt;version&gt;1.4.9&lt;/version&gt;
        &lt;/dependency&gt;

    &lt;/dependencies&gt;

    &lt;groupId&gt;org.example&lt;/groupId&gt;
    &lt;artifactId&gt;MyProject&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;

&lt;/project&gt;

I deleted the manifest and inserted this:

 &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.2.0&lt;/version&gt;
                &lt;executions&gt;
                    &lt;execution&gt;
                        &lt;phase&gt;package&lt;/phase&gt;
                        &lt;goals&gt;
                            &lt;goal&gt;single&lt;/goal&gt;
                        &lt;/goals&gt;
                    &lt;/execution&gt;
                &lt;/executions&gt;
                &lt;configuration&gt;
                    &lt;descriptorRefs&gt;
                        &lt;descriptorRef&gt;jar-with-dependencies&lt;/descriptorRef&gt;
                    &lt;/descriptorRefs&gt;
                    &lt;archive&gt;
                        &lt;manifest&gt;
                            &lt;mainClass&gt;allClasses.MainClass&lt;/mainClass&gt;
                            &lt;addDefaultImplementationEntries&gt;true&lt;/addDefaultImplementationEntries&gt;
                            &lt;addDefaultSpecificationEntries&gt;true&lt;/addDefaultSpecificationEntries&gt;
                        &lt;/manifest&gt;
                    &lt;/archive&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
                &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
                &lt;configuration&gt;
                    &lt;source&gt;8&lt;/source&gt;
                    &lt;target&gt;8&lt;/target&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;

but nothing has changed

What could be the problem?
Why code works in intellij and don't work after creating jar file?

答案1

得分: 1

我看到您提到了一个精确的 MANIFEST 文件,了解其内部写了什么内容将会很有用,因为正如您所知,在 MANIFEST 文件内部既包含启动类,也包含依赖项及其位置的列表。
POM 文件没有明确地构建 "jar-with-dependency",因此我会假设您构建的包不包含必要的依赖项,无法使用它。

因此,您有两个选择:

  1. 创建一个 "all-in-one" 包,其中包含了所有必要的依赖项,通过 Maven 插件实现,将您的 "maven-jar-plugin" 替换为 "maven-assembly-plugin":
<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>{主类的完全限定名}</mainClass>		
                <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
            </manifest>
        </archive>
    </configuration>
</plugin>
  1. 通过参数 -Djava.library.path、-cp 或类似方式引用外部依赖项。
英文:

I see that you refer to a precise MANIFEST file, it would be useful to know what is written inside it because, as I think you know, inside the MANIFEST file there are both startup class and the list of dependencies and their location.
The POM file does not explicitly construct a "jar-with-dependency", therefore I would assume that the package you have built does not contain the necessary dependencies to use it

As a result, you have two choices:

  1. create an "all-in-one" package, that contains all the dependencies necessary for its use, via maven plugin, replacing your "maven-jar-plugin" with "maven-assembly-plugin":

    <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. referencing the external dependencies via the parameter -Djava.library.path, -cp or similar

答案2

得分: 0

当您编译JAR文件时,需要将所需的依赖项与JAR文件一起打包。您可以尝试探索以下配置。它的作用是将所需的依赖项添加到JAR本身中,当您尝试运行JAR文件时,它会将所有依赖项与JAR文件本身一起找到。在这里,api-all是我想要与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 .:<file_1_name>.jar:<file_2_name>.jar <prog_name>为依赖的JAR文件提供类路径。

进一步阅读:https://stackoverflow.com/questions/9395207/how-to-include-jar-files-with-java-file-and-compile-in-command-prompt

英文:

When you compile the jar file you would need to package the required dependency with jar file. You can try to explore following configuration. What it does is that it adds required dependency with jar itself and when you try to run the jar it finds all the dependency with jar itself. Here api-all is dependency which I would like to include with jar itself. Which has maven dependency

       &lt;dependency&gt;
            &lt;groupId&gt;antlr&lt;/groupId&gt;
            &lt;artifactId&gt;antlr&lt;/artifactId&gt;
            &lt;version&gt;2.7.7&lt;/version&gt;
            &lt;scope&gt;provided&lt;/scope&gt;
        &lt;/dependency&gt;




    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.apache.felix&lt;/groupId&gt;
                &lt;artifactId&gt;maven-bundle-plugin&lt;/artifactId&gt;
                &lt;extensions&gt;true&lt;/extensions&gt;
                &lt;configuration&gt;
                    &lt;instructions&gt;
                        &lt;Bundle-SymbolicName&gt;com.your.package&lt;/Bundle-SymbolicName&gt;
                        &lt;Export-Package&gt;com.your.package.*;org.apache.commons.beanutils.*&lt;/Export-Package&gt;
                        &lt;Embed-Dependency&gt;api-all,commons-pool,mina-core,antlr&lt;/Embed-Dependency&gt;
                        &lt;Embed-Transitive&gt;true&lt;/Embed-Transitive&gt;
                        &lt;DynamicImport-Package&gt;
                            org.dom4j, org.dom4j.io, org.jdom, org.jdom.input
                        &lt;/DynamicImport-Package&gt;
                        &lt;Import-Package&gt;
                            !org.dom4j.*, !org.xmlpull.v1, !sun.net.util, *
                        &lt;/Import-Package&gt;
                    &lt;/instructions&gt;
                &lt;/configuration&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;

Alternatively you try to provide classpath to dependent jar in command line itself using java -cp .:&lt;file_1_name&gt;.jar:&lt;file_2_name&gt;.jar &lt;prog_name&gt;

Further reading https://stackoverflow.com/questions/9395207/how-to-include-jar-files-with-java-file-and-compile-in-command-prompt

答案3

得分: 0

我只是在Eclipse中创建了一个JAR文件 =)

英文:

I'm just create jar in eclipse =)

huangapple
  • 本文由 发表于 2020年4月6日 21:08:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/61060591.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定