gpt4 book ai didi

java - 使用maven出现异常

转载 作者:太空宇宙 更新时间:2023-11-04 09:18:30 27 4
gpt4 key购买 nike

我正在实现 Google SpreadSheet 同步客户端,并在执行 mvn clean install 时遇到以下异常。

private static final String APPLICATION_NAME = "Data Synchronization";
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final String TOKENS_DIRECTORY_PATH = "tokens";
/**
* Global instance of the scopes required by this quickstart.
* If modifying these scopes, delete your previously saved tokens/ folder.
*/
private static final List<String> SCOPES = Collections.singletonList(SheetsScopes.SPREADSHEETS_READONLY);
private static final String CREDENTIALS_FILE_PATH = "/credentials.json";

/**
* Creates an authorized Credential object.
*
* @param httpTransport The network HTTP Transport.
* @return An authorized Credential object.
* @throws IOException If the credentials.json file cannot be found.
*/
private Credential getCredentials(final NetHttpTransport httpTransport) throws IOException {
// Load client secrets.
InputStream in = SpreadSheetImporter.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));

// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(new FileDataStoreFactory(new File(TOKENS_DIRECTORY_PATH)))
.setAccessType("offline")
.build();
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setPort(8888).build();
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user");
}

List<List<Object>> fetchData(String spreadsheetId, String range) {
System.out.println("Load data for " + spreadsheetId);
final NetHttpTransport httpTransport;
try {
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
Sheets service = new Sheets.Builder(httpTransport, JSON_FACTORY, getCredentials(httpTransport))
.setApplicationName(APPLICATION_NAME)
.build();
ValueRange response = service.spreadsheets().values()
.get(spreadsheetId, range)
.execute();

return response.getValues();

} catch (GeneralSecurityException | IOException e) {
throw new IllegalArgumentException("Error fetching data", e);
}
}

该代码可以使用 IntelliJ IDEA 正常编译,但遇到以下异常:

org.junit.jupiter.api.extension.TestInstantiationException: TestInstanceFactory [io.quarkus.test.junit.QuarkusTestExtension] failed to instantiate test class [test.business.journal.control.JournalTest]
Caused by: java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: Failed to start quarkus
Caused by: java.lang.VerifyError:
Bad type on operand stack
Exception Details:
Location:
/data_sync/boundary/SpreadSheetClient.fetchData(Ljava/lang/String;Ljava/lang/String;)Ljava/util/List; @94: invokespecial
Reason:
Type 'java/lang/Object' (current frame, stack[3]) is not assignable to 'java/lang/Throwable'
Current Frame:
bci: @94
flags: { }
locals: { '/data_sync/boundary/SpreadSheetClient', 'java/lang/String', 'java/lang/String', 'com/google/api/client/http/javanet/NetHttpTransport', 'java/lang/Object' }
stack: { uninitialized 86, uninitialized 86, 'java/lang/String', 'java/lang/Object' }
Bytecode:
0x0000000: b200 21bb 0022 59b7 0023 1224 b600 252b
0x0000010: b600 25b6 0026 b600 2701 4eb8 0028 4ebb
0x0000020: 0029 592d b200 092a 2db7 002a b700 2b12
0x0000030: 2cb6 002d b600 2e3a 0419 04b6 002f b600
0x0000040: 302b 2cb6 0031 b600 32c0 0033 3a05 1905
0x0000050: b600 34b0 3a04 bb00 3759 1238 1904 b700
0x0000060: 39bf
Exception Handler Table:
bci [27, 83] => handler: 84
bci [27, 83] => handler: 84
Stackmap Table:
full_frame(@84,{Object[#106],Object[#107],Object[#107],Object[#108]},{Object[#62]})

此代码来自 Google Sheets API 的官方 Quickstarers:https://developers.google.com/sheets/api/quickstart/java

我尝试了该示例,mvn clean install 给出了与我的改编代码相同的错误。

我猜,这是一个 Maven 依赖问题,但我不太确定。到底是什么原因导致了这个问题?

最佳答案

您正在使用ma​​ven执行此操作,因此您必须对代码进行一些更改,因为在快速入门示例中旨在与gradle一起使用。我会给您我使用的代码,它对我有用(也在 IntelliJ IDEA 中运行它)。

Java代码

package com.quickstart;

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;
import com.google.api.services.sheets.v4.SheetsScopes;
import com.google.api.services.sheets.v4.model.ValueRange;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.GeneralSecurityException;
import java.util.Collections;
import java.util.List;


public class SheetsQuickstart {

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 = "tokens";

/**
* Global instance of the scopes required by this quickstart.
* If modifying these scopes, delete your previously saved tokens/ folder.
*/
private static final List<String> SCOPES = Collections.singletonList(SheetsScopes.SPREADSHEETS_READONLY);
private static final String CREDENTIALS_FILE_PATH = "/credentials.json";

/**
* Creates an authorized Credential object.
* @param HTTP_TRANSPORT The network HTTP Transport.
* @return An authorized Credential object.
* @throws IOException If the credentials.json file cannot be found.
*/
private static Credential getCredentials(NetHttpTransport HTTP_TRANSPORT) throws Exception {
// Load client secrets.
InputStream in = SheetsQuickstart.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));

// Build flow and trigger user authorization request.
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 void main(String[] args) throws Exception {
// Build a new authorized API client service.
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
final String spreadsheetId = "YOUR SHEET ID";
// Choose any range you want to
final String range = "A2:E2";
Sheets service = new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, getCredentials(HTTP_TRANSPORT))
.setApplicationName(APPLICATION_NAME)
.build();
ValueRange response = service.spreadsheets().values()
.get(spreadsheetId, range)
.execute();
List<List<Object>> values = response.getValues();
if (values == null || values.isEmpty()) {
System.out.println("No data found.");
} else {
System.out.println("Name, Major");
for (List row : values) {
// Print columns A and E, which correspond to indices 0 and 4.
System.out.printf("%s, %s\n", row.get(0), row.get(4));
}
}
}

}

不要忘记在此处输入您的工作表 ID Final String SpreadsheetId = "YOUR SHEET ID";并将您的 credentials.json 文件放入 resources 文件夹中。

对于 Maven 文件,请使用这些依赖项

行家

 <!-- https://mvnrepository.com/artifact/com.google.apis/google-api-services-sheets -->
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-sheets</artifactId>
<version>v4-rev1-1.21.0</version>
</dependency>
<dependency>
<groupId>com.google.gdata</groupId>
<artifactId>core</artifactId>
<version>1.47.1</version>
</dependency>

关于java - 使用maven出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58660109/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com