0

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

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 :

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

I deleted the manifest and inserted this:

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

but nothing has changed

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

Zeo
  • 53
  • 2
  • 10

3 Answers3

1

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

  • My manifest : Manifest-Version: 1.0 Main-Class: allClasses.MainClass I added what you wrote above instead of maven-jar-plugin but the error remained – Zeo Apr 06 '20 at 14:55
  • @Zeo ok, my suggestion remains. your manifest is useless, delete it and let generate it via maven. pay attention that in this way you'll find 2 jar files in your target folder: 1 "original" and 1 "with-dependencies"; obvs you have to run the big one ! – Stefano Riffaldi Apr 06 '20 at 15:04
0

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

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

Alternatively you try to provide classpath to dependent jar in command line itself using java -cp .:<file_1_name>.jar:<file_2_name>.jar <prog_name>

Further reading How to include jar files with java file and compile in command prompt

Rupesh
  • 2,627
  • 1
  • 28
  • 42
0

I'm just create jar in eclipse =)

Zeo
  • 53
  • 2
  • 10