英文:
Heroku deployment build failure Java Spring project : package java.net.http does not exist
问题
这是错误信息:
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[15,21] package java.net.http does not exist
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[16,21] package java.net.http does not exist
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[17,21] package java.net.http does not exist
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[46,9] cannot find symbol
symbol: class HttpClient
location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[46,29] cannot find symbol
symbol: variable HttpClient
location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[47,9] cannot find symbol
symbol: class HttpClient
location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[47,30] cannot find symbol
symbol: variable HttpClient
location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[48,9] cannot find symbol
symbol: class HttpRequest
location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[48,30] cannot find symbol
symbol: variable HttpRequest
location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[49,9] cannot find symbol
symbol: class HttpRequest
location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[49,31] cannot find symbol
symbol: variable HttpRequest
location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[51,9] cannot find symbol
symbol: class HttpResponse
location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[51,76] package HttpResponse does not exist
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[52,9] cannot find symbol
symbol: class HttpResponse
location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[52,78] package HttpResponse does not exist
[INFO] 15 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
这是代码中出现问题的位置:
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(VirusDataURL)).build();
HttpResponse<String> httpResponse = client.send(request, HttpResponse.BodyHandlers.ofString());
这是你的代码中可能引发问题的部分。如果你的代码在本地运行正常,但在部署到Heroku时出现错误,可能是因为在Heroku环境中缺少了 java.net.http
包。你可以检查你的项目的构建配置,确保它能够正确地解决这些依赖关系。
英文:
I have been trying to deploy a java spring project on Heroku but I keep getting this error. The project is a tracker that uses some data from an online csv file and displays data using the information. It also updates the information daily. when I host it locally using http://localhost:8080/ it works fine. Here is the error message.
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[15,21] package java.net.http does not exist
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[16,21] package java.net.http does not exist
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[17,21] package java.net.http does not exist
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[46,9] cannot find symbol
symbol: class HttpClient
location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[46,29] cannot find symbol
symbol: variable HttpClient
location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[47,9] cannot find symbol
symbol: class HttpClient
location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[47,30] cannot find symbol
symbol: variable HttpClient
location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[48,9] cannot find symbol
symbol: class HttpRequest
location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[48,30] cannot find symbol
symbol: variable HttpRequest
location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[49,9] cannot find symbol
symbol: class HttpRequest
location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[49,31] cannot find symbol
symbol: variable HttpRequest
location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[51,9] cannot find symbol
symbol: class HttpResponse
location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[51,76] package HttpResponse does not exist
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[52,9] cannot find symbol
symbol: class HttpResponse
location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[52,78] package HttpResponse does not exist
[INFO] 15 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.179 s
[INFO] Finished at: 2020-09-20T05:56:34Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project Corona-Track: Compilation failure: Compilation failure:
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[15,21] package java.net.http does not exist
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[16,21] package java.net.http does not exist
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[17,21] package java.net.http does not exist
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[46,9] cannot find symbol
[ERROR] symbol: class HttpClient
[ERROR] location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[46,29] cannot find symbol
[ERROR] symbol: variable HttpClient
[ERROR] location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[47,9] cannot find symbol
[ERROR] symbol: class HttpClient
[ERROR] location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[47,30] cannot find symbol
[ERROR] symbol: variable HttpClient
[ERROR] location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[48,9] cannot find symbol
[ERROR] symbol: class HttpRequest
[ERROR] location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[48,30] cannot find symbol
[ERROR] symbol: variable HttpRequest
[ERROR] location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[49,9] cannot find symbol
[ERROR] symbol: class HttpRequest
[ERROR] location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[49,31] cannot find symbol
[ERROR] symbol: variable HttpRequest
[ERROR] location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[51,9] cannot find symbol
[ERROR] symbol: class HttpResponse
[ERROR] location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[51,76] package HttpResponse does not exist
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[52,9] cannot find symbol
[ERROR] symbol: class HttpResponse
[ERROR] location: class com.Corona.CoronaTrack.services.CoronaDataService
[ERROR] /tmp/build_764c076e_/src/main/java/com/Corona/CoronaTrack/services/CoronaDataService.java:[52,78] package HttpResponse does not exist
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
! ERROR: Failed to build app with Maven
We're sorry this build is failing! If you can't find the issue in application code,
please submit a ticket so we can help: https://help.heroku.com/
! Push rejected, failed to compile Java app.
! Push failed
Here is the location it keeps saying there is a problem
package com.Corona.CoronaTrack.services;
import com.Corona.CoronaTrack.models.Stats;
import com.Corona.CoronaTrack.models.Stats2;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.io.StringReader;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.List;
@Service
public class CoronaDataService {
private static String VirusDataURL = "https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv";
private static String VirusDataURL2= "https://raw.githubusercontent.com/nytimes/covid-19-data/master/us.csv";
private List<Stats> allStats = new ArrayList<>();
public List<Stats> getAllStats() {
return allStats;
}
private List<Stats2> allStats2 = new ArrayList<>();
public List<Stats2> getAllStats2() {
return allStats2;
}
@PostConstruct
@Scheduled(cron = "* * 1 * * *")
public void GetCornonaData() throws IOException, InterruptedException {
List<Stats> newStats = new ArrayList<>();
List<Stats2> newStats2 = new ArrayList<>();
HttpClient client = HttpClient.newHttpClient();
HttpClient client2 = HttpClient.newHttpClient();
HttpRequest request= HttpRequest.newBuilder().uri(URI.create(VirusDataURL)).build();
HttpRequest request2= HttpRequest.newBuilder().uri(URI.create(VirusDataURL2)).build();
HttpResponse<String> httpResponse=client.send(request, HttpResponse.BodyHandlers.ofString());
HttpResponse<String> httpResponse2=client.send(request2, HttpResponse.BodyHandlers.ofString());
StringReader csvBody2 = new StringReader(httpResponse2.body());
Iterable<CSVRecord> records2 = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(csvBody2);
for (CSVRecord record2 : records2) {
Stats2 locationStat2 = new Stats2();
locationStat2.setTotalAmountOfCases(record2.get(record2.size()-2));
locationStat2.setTotalAmountOfDeaths(record2.get(record2.size()-1));
newStats2.add(locationStat2);
}
this.allStats2 = newStats2;
StringReader csvBody = new StringReader(httpResponse.body());
Iterable<CSVRecord> records = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(csvBody);
for (CSVRecord record : records) {
Stats locationStat = new Stats();
locationStat.setDate(record.get("date"));
locationStat.setState(record.get("state"));
locationStat.setCases(record.get("cases"));
locationStat.setDeaths(record.get("deaths"));
newStats.add(locationStat);
}
this.allStats = newStats;
}
}
I know it’s not the best code ever. This is my first project using spring. If you have any suggestions on a better way to deploy my project I would appreciate the advice. I appreciate any help given and thank you.
答案1
得分: 5
java.net.http
在 Java 11 中被引入,您可能在本地使用。
Heroku 默认提供 Java 8 运行时,但您可以设置所需的 Java 版本。在项目根目录下创建一个名为 system.properties
的文件,其中包含:
java.runtime.version=11
英文:
java.net.http
has been introduced in Java 11 which you probably use locally.
Heroku by default provides a Java 8 runtime, you can however set your desired Java version. Create a file system.properties
in the root of your project which contains:
java.runtime.version=11
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论