简体   繁体   中英

com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;CLjava/lang/Object;)V

I am using the google BETA API: AutoML Natural Language, after training the API, I did tests in a Java SE project obtaining a satisfactory result, however, when I migrated it to a Java EE project I got different problems. About:

-Java -version: 1.8.0_201
-Payara Version: 5.191
-google-cloud-automl dependecy: 0.97.0-beta

When I try to run the entity extraction service (PredictionServiceClient class) it returns the error java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument (ZLjava / lang / String; CLjava / lang / Object;) V , according to different sources this error is caused by conflicts between different versions of the guava library. I think my particular error is caused because guava's version is 19.0.0 in payara and google-cloud-automl requires a version > 20.

To solve this error payara suggests here modifying the file glassfish-application.xml, "With this, the libraries included in the EAR's lib / directory will take precedence", however this did not work for me.

The code was written with the help of this documentation

glassfish-application.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-application PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Java EE Application 6.0//EN" "http://glassfish.org/dtds/glassfish-application_6_0-1.dtd">  
<glassfish-application>
    <classloading-delegate>false</classloading-delegate>    
</glassfish-application>

pom[EJB].xml

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <parent>
        <artifactId>AutoMLTest</artifactId>
        <groupId>co.com.group.automl</groupId>
        <version>1.0</version>
    </parent>

    <groupId>co.com.group.automl</groupId>
    <artifactId>AutoMLTest-ejb</artifactId>
    <version>1.0</version>
    <packaging>ejb</packaging>

    <name>AutoMLTest-ejb</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

   <dependencies>
      <dependency>
          <groupId>javax</groupId>
          <artifactId>javaee-api</artifactId>
          <version>7.0</version>
          <scope>provided</scope>
      </dependency>
      <dependency>
          <groupId>com.google.cloud</groupId>
          <artifactId>google-cloud-automl</artifactId>
          <version>0.97.0-beta</version>
      </dependency>     
   </dependencies>

   <build>
        <resources>
            <resource>
                <targetPath>META-INF</targetPath>
                <directory>src</directory>
                <includes>
                    <include>jax-ws-catalog.xml</include>
                    <include>wsdl/**</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ejb-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <ejbVersion>3.1</ejbVersion>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
       </plugins>
    </build>    
</project>

Method AutoML Extraction.xml

public void extracNL(String content) throws Exception {
   String googleCredentials = "/path/credentials.json"
   GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(googleCredentials)).createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));

   PredictionServiceSettings settings = 
PredictionServiceSettings.newBuilder().setCredentialsProvider(FixedCredentialsProvider.create(credentials)).build();

   //The problem is presented in this line of code
   PredictionServiceClient serviceClient = 
   PredictionServiceClient.create(settings);

   ModelName modelName = ModelName.of("projectId", "computeRegion", "modelId");
        TextSnippet snippet = TextSnippet.newBuilder().setContent(content).setMimeType("text/plain").build();
        ExamplePayload payload = ExamplePayload.newBuilder().setTextSnippet(snippet).build();

        Map<String, String> params = new HashMap<>();
        PredictResponse response = serviceClient.predict(modelName, payload, params);
        List<PredictGoogleDTO> predictionsTmp = new ArrayList<>();

        for (AnnotationPayload annotationPayload : response.getPayloadList()) {
            if (annotationPayload.getDisplayName().equals("Person")) {
                System.out.println("DisplayName="+annotationPayload.getDisplayName());
                System.out.println("Content="+annotationPayload.getTextExtraction().getTextSegment().getContent());
            }
        }
}

Is there another way to do this? is it really impossible for my application to use the guava version in the pom and not the payara server? after testing the command line in maven mvn dependecy; tree -Dverbose in the path of my project I do not get any dependencies error. can then guava be the cause of this error really? I feel that I have tried everything and I can not find a possible solution to my problem, in advance thank you.

probably many of them will be presented with this and other errors when using the AutoML Natural Language API in their BETA version, this is normal and we must assume that as a BETA version this type of problem will be present. My solution was to isolate the consumption of the API from my JAVA EE application, implementing for the first time a lambda function in AWS (Amazon Web Service) and consuming the service through a REST call to this function. This worked perfect for me, and it is a clean solution (from my perspective). Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question NoSuchMethodError: com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;CLjava/lang/Object;)V Google Cloud KMS java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;CLjava/lang/Object;)V Error:Cause: com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;Ljava/lang/Object;)V Error :: java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;Ljava/lang/Object;)V NoSuchMethodError: com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;J)V NoSuchMethodError: com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;Ljava/lang/Object;Ljava/lang/Object;) java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkArgument(ZLjava/lang/String;I)V with IE and Selenium through Java Maven - NoSuchMethodError: void com.google.common.base.Preconditions.checkArgument(boolean, java.lang.String, java.lang.Object) java.lang.NoSuchMethodError: 'void com.google.common.base.Preconditions.checkArgument(boolean, java.lang.String, java.lang.Object) “main” java.lang.NoSuchMethodError: 'void com.google.common.base.Preconditions.checkArgument(boolean, java.lang.String, char, java.lang.Object)'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM