Heroku部署构建失败Java Spring项目:package java.net.http不存在。

huangapple go评论76阅读模式
英文:

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] -&gt; [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&#39;re sorry this build is failing! If you can&#39;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 = &quot;https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv&quot;;
private static String VirusDataURL2= &quot;https://raw.githubusercontent.com/nytimes/covid-19-data/master/us.csv&quot;;
private List&lt;Stats&gt; allStats = new ArrayList&lt;&gt;();
public List&lt;Stats&gt; getAllStats() {
return allStats;
}
private List&lt;Stats2&gt; allStats2 = new ArrayList&lt;&gt;();
public List&lt;Stats2&gt; getAllStats2() {
return allStats2;
}
@PostConstruct
@Scheduled(cron = &quot;* * 1 * * *&quot;)
public void GetCornonaData() throws IOException, InterruptedException {
List&lt;Stats&gt; newStats = new ArrayList&lt;&gt;();
List&lt;Stats2&gt; newStats2 = new ArrayList&lt;&gt;();
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&lt;String&gt; httpResponse=client.send(request, HttpResponse.BodyHandlers.ofString());
HttpResponse&lt;String&gt; httpResponse2=client.send(request2, HttpResponse.BodyHandlers.ofString());
StringReader  csvBody2 = new StringReader(httpResponse2.body());
Iterable&lt;CSVRecord&gt; 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&lt;CSVRecord&gt; records = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(csvBody);
for (CSVRecord record : records) {
Stats locationStat = new Stats();
locationStat.setDate(record.get(&quot;date&quot;));
locationStat.setState(record.get(&quot;state&quot;));
locationStat.setCases(record.get(&quot;cases&quot;));
locationStat.setDeaths(record.get(&quot;deaths&quot;));
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

huangapple
  • 本文由 发表于 2020年9月20日 14:51:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63976289.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定