【问题标题】:maven "cannot find symbol" jackson2.JacksonFactorymaven“找不到符号”jackson2.JacksonFactory
【发布时间】:2018-05-31 13:56:37
【问题描述】:

我正在尝试通过 maven 访问 google sheet api, 不幸的是,我在编译 maven 时出错

这是我的 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>

<groupId>BotSheets</groupId>
<artifactId>b</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>b</name>
<url>http://maven.apache.org</url>

<build>
    <plugins>
        <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>BotSheets.b.GoogleSheetAPI</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>


<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit-dep</artifactId>
        <version>4.11</version>
    </dependency>
    <dependency>
        <groupId>com.google.api-client</groupId>
        <artifactId>google-api-client</artifactId>
        <version>1.22.0</version>
    </dependency>
    <dependency>
        <groupId>com.google.oauth-client</groupId>
        <artifactId>google-oauth-client-jetty</artifactId>
        <version>1.22.0</version>
    </dependency>
    <dependency>
        <groupId>com.google.apis</groupId>
        <artifactId>google-api-services-sheets</artifactId>
        <version>v4-rev483-1.22.0</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.10</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.4.0</version>
    </dependency>
</dependencies>

我的树文件:

    ├── pom.xml
├── src
│   ├── main
│   │   └── java
│   │       └── BotSheets
│   │           └── b
│   │               ├── client_secret.json
│   │               └── GoogleSheetAPI.java
│   └── test
│       └── java
│           └── BotSheets
│               └── b
│                   └── AppTest.java

我的代码:

package BotSheets.b;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.SheetsScopes;
import com.google.api.services.sheets.v4.model.ValueRange;

    public class GoogleSheetAPI {
        /** Application name. */
        private static final String APPLICATION_NAME = "Google Sheets API Java Quickstart";

        /** Directory to store user credentials for this application. */
        private static final java.io.File DATA_STORE_DIR = new java.io.File(
            System.getProperty("user.home"), ".credentials/sheets.googleapis.com-java-quickstart");

        /** Global instance of the {@link FileDataStoreFactory}. */
        private static FileDataStoreFactory DATA_STORE_FACTORY;

        /** Global instance of the JSON factory. */
        private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

        /** Global instance of the HTTP transport. */
        private static HttpTransport HTTP_TRANSPORT;

        /** Global instance of the scopes required by this quickstart.
         *
         * If modifying these scopes, delete your previously saved credentials
         * at ~/.credentials/sheets.googleapis.com-java-quickstart
         */
        private static final List<String> SCOPES = Arrays.asList(SheetsScopes.SPREADSHEETS_READONLY);

        static {
            try {
                HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
                DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
            } catch (Throwable t) {
                t.printStackTrace();
                System.exit(1);
            }
        }

        /**
         * Creates an authorized Credential object.
         * @return an authorized Credential object.
         * @throws IOException
         */
        public static Credential authorize() throws IOException {
            // Load client secrets.
            InputStream in =
            GoogleSheetAPI.class.getResourceAsStream("/client_secret.json");
            GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

            // Build flow and trigger user authorization request.
            GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                    .setDataStoreFactory(DATA_STORE_FACTORY)
                    .setAccessType("offline")
                    .build();
            Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
            System.out.println("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
            return credential;
        }

        /**
         * Build and return an authorized Sheets API client service.
         * @return an authorized Sheets API client service
         * @throws IOException
         */
        public static Sheets getSheetsService() throws IOException {
            Credential credential = authorize();
            return new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                    .setApplicationName(APPLICATION_NAME)
                    .build();
        }

        public List<List<Object>> getSpreadSheetRecords(String spreadsheetId, String range) throws IOException {
            Sheets service = getSheetsService();        
            ValueRange response = service.spreadsheets().values()
                    .get(spreadsheetId, range)
                    .execute();
            List<List<Object>> values = response.getValues();
            if (values != null && values.size() != 0) {
                return values;
            } else {
                System.out.println("No data found.");
                return null;
            }
        }
    }

我很乐意提供帮助, 这就是我得到的错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project b: Compilation failure: Compilation failure: 
[ERROR] /home/*/eclipse-workspace/b/src/main/java/BotSheets/b/GoogleSheetAPI.java:[17,43] package com.google.api.client.json.jackson2 does not exist
[ERROR] /home/*/eclipse-workspace/b/src/main/java/BotSheets/b/GoogleSheetAPI.java:[35,53] cannot find symbol
[ERROR]   symbol:   variable JacksonFactory
[ERROR]   location: class BotSheets.b.GoogleSheetAPI

Java 版本为 10.0.1

我尝试了一些东西,但没有取得很大的成功。 我从http://www.seleniumeasy.com/selenium-tutorials/read-data-from-google-spreadsheet-using-api复制的这段代码

【问题讨论】:

    标签: java maven google-api google-sheets-api


    【解决方案1】:

    如果您使用 IDE,IDE 是否没有显示任何错误?从外观上看,您没有 Jackson 2 扩展的 maven 依赖项。尝试在 Maven 中添加依赖项。

    <dependency>
    <groupId>com.google.http-client</groupId>
    <artifactId>google-http-client-jackson2</artifactId>
    <version></version> //Add the version you want to use. 
    </dependency>
    

    【讨论】:

    • 我做到了,maven 能够编译代码。但是当我想要代码时出现错误:错误:无法初始化主类 BotSheets.b.GoogleSheetAPI 原因:java.lang.NoClassDefFoundError: com/google/api/client/util/store/DataStoreFactory
    • 您在使用像 Eclipse 这样的 IDE 吗?听起来并非所有依赖项都已加载,否则依赖项版本冲突。
    • 我尝试使用 shell maven 和 eclipse 运行配置,但没有成功
    • 我尝试使用您的 pom.xml 和类制作一个简单的项目。它在 Eclipse 中为我编译,没有任何问题。甚至没有添加我上面提到的依赖项。我再次检查一下,您是否尝试在不添加 jacson2 依赖项的情况下使用 Eclipse 构建它?
    • 我没有 DATA_STORE_DIR 文件。你能检查文件是否存在吗?我相信作为一个示例,唯一可以给出错误的是配置或版本冲突。
    猜你喜欢
    • 2014-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 2014-01-25
    • 2015-07-21
    • 2017-05-10
    • 2015-07-10
    相关资源
    最近更新 更多