【问题标题】:Imports unresolved when manually configuring build path手动配置构建路径时导入未解决
【发布时间】:2021-06-01 05:23:28
【问题描述】:

在尝试使用 Maven 创建我的第一个可执行文件时遇到了问题,现在我尝试使用 install4j 代替。为了保留我的依赖关系,我下载了我的 pom.xml 中列出的 jar,将它们添加到构建路径,并删除了 pom.xml。现在,我的一些罐子似乎没有正确导入。我已经尝试了最新版本和在我的 maven 项目中运行良好的旧版本,但都不起作用。

我要解决的最终问题是,当我使用 install4j 构建时,我的 exe 无法运行,我认为这是由于我的 pom.xml 中的依赖项没有被包含(因为我没有构建使用 Maven)

这些是我的罐子:

  • google-api-client-1.3.2.jar
  • google-api-services-sheets-v4-rev614-1.18.0-rc.jar
  • google-oauth-client-jetty-1.31.4.jar
  • commons-io-1.3.2.jar(不要 认为这是使用的)

这些是有效的导入:

  • 导入 com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
  • 导入 com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
  • 导入 com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
  • 导入 com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
  • 导入 com.google.api.services.sheets.v4.Sheets;

这些是无效的导入:

  • 导入 com.google.api.client.auth.oauth2.Credential;
  • 导入 com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
  • 导入 com.google.api.client.http.javanet.NetHttpTransport;
  • 导入 com.google.api.client.json.JsonFactory;
  • 导入 com.google.api.client.json.jackson2.JacksonFactory;
  • 导入 com.google.api.client.util.store.FileDataStoreFactory;

这是我的代码的 MRE:

package misc;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collections;

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.javanet.NetHttpTransport;
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;

public class GoogleSheetsStuff {
    
    private static final String APPLICATION_NAME = "Google Sheets API Java Quickstart";
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
    private static final String TOKENS_DIRECTORY_PATH = "tokens2";

    private static final List<String> SCOPES = Collections.singletonList(SheetsScopes.SPREADSHEETS);
    private static final String CREDENTIALS_FILE_PATH = "/credentials.json";

    private static Credential getCredentials(final NetHttpTransport HTTP_TRANSPORT) throws IOException {
        InputStream in = GoogleSheetsStuff.class.getResourceAsStream(CREDENTIALS_FILE_PATH);
        if (in == null) {
            throw new FileNotFoundException("Resource not found: " + CREDENTIALS_FILE_PATH);
        }
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                .setDataStoreFactory(new FileDataStoreFactory(new java.io.File(TOKENS_DIRECTORY_PATH)))
                .setAccessType("offline")
                .build();
        LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
        return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
    }

    public static Sheets buildService () throws GeneralSecurityException, IOException {
        final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
        Sheets service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
                .setApplicationName(APPLICATION_NAME)
                .build();
        return service;
    }
}

【问题讨论】:

    标签: java build google-api


    【解决方案1】:

    您不必手动下载 jar 并将其添加到您的类路径中以包含在您的项目运行时中。所有你需要的是一个胖罐。这是一个关于如何创建一个的示例:-

    http://tutorials.jenkov.com/maven/maven-build-fat-jar.html

    【讨论】:

    • 使用 maven 程序集插件(创建可执行 jar)是我遇到原始问题时尝试做的事情。罐子不会运行。除非这里的想法是将 fat jar 添加到我的构建路径然后使用 install4j 创建可执行文件?我要解决的最终问题是,当我使用 install4j 构建时,我的 exe 无法运行,我认为这是由于我的 pom.xml 中的依赖项不包括在内(因为我没有使用 maven 构建)
    猜你喜欢
    • 2010-11-21
    • 2023-04-09
    • 2013-10-21
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    • 2018-10-08
    • 2019-08-08
    • 2013-03-13
    相关资源
    最近更新 更多