【问题标题】:Failed to execute CommandLineRunner in jar file无法在 jar 文件中执行 CommandLineRunner
【发布时间】:2019-06-19 03:41:12
【问题描述】:

我在 .jar 文件中运行我的应用程序时遇到问题。当我在 IntelliJ 中运行它时,我没有任何问题。这是通过 .jar 运行我的应用程序的结果我该如何解决?我正在使用 JsonNode 和 restTemplate 从 json 获取信息并创建对象。

Application run failed

java.lang.IllegalStateException: Failed to execute CommandLineRunner
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:816) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:324) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        at com.rafalpodgorski.weatherclient.WeatherClientApplication.main(WeatherClientApplication.java:17) ~[classes!/:0.0.1-SNAPSHOT]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
        at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) ~[weatherclient-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) ~[weatherclient-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) ~[weatherclient-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) ~[weatherclient-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
Caused by: org.springframework.web.client.HttpClientErrorException$NotFound: 404 Not Found
        at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:85) ~[spring-web-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
        at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:122) ~[spring-web-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
        at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:102) ~[spring-web-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
        at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
        at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:777) ~[spring-web-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
        at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:735) ~[spring-web-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
        at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:669) ~[spring-web-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
        at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:310) ~[spring-web-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
        at com.rafalpodgorski.weatherclient.service.TemperatureService.getTemperature(TemperatureService.java:42) ~[classes!/:0.0.1-SNAPSHOT]
        at com.rafalpodgorski.weatherclient.service.TemperatureService.getTemperatureByCityName(TemperatureService.java:31) ~[classes!/:0.0.1-SNAPSHOT]
        at com.rafalpodgorski.weatherclient.WeatherClientApplication.getTemperatureForCity(WeatherClientApplication.java:24) ~[classes!/:0.0.1-SNAPSHOT]
        at com.rafalpodgorski.weatherclient.WeatherClientApplication.lambda$run$0(WeatherClientApplication.java:37) ~[classes!/:0.0.1-SNAPSHOT]
        at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813) ~[spring-boot-2.1.2.RELEASE.jar!/:2.1.2.RELEASE]
        ... 13 common frames omitted

我的跑步课:

@SpringBootApplication(scanBasePackages = "com.application")
public class WeatherClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(WeatherClientApplication.class, args);
    }

    private static Temperature getTemperatureForCity(TemperatureService temperatureService, String city) {
        if (city.equals("auto")) {
            return temperatureService.getTemperatureByGeolocation();
        } else {
            return temperatureService.getTemperatureByCityName(city);
        }
    }

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder builder) {
        return builder.build();
    }

    @Bean
    public CommandLineRunner run(TemperatureService temperatureService) {
        return args -> {
            for (String city : ConfigReader.readConfig()) {
                Temperature temperature = getTemperatureForCity(temperatureService, city);
                System.out.println(temperature);
                temperatureService.saveTemperature(temperature);
            }
        };
    }
}

温度服务:

@Service
public class TemperatureService {

    private RestTemplate restTemplate;
    private GeolocationService geolocationService;

    public TemperatureService(RestTemplate restTemplate, GeolocationService geolocationService) {
        this.restTemplate = restTemplate;
        this.geolocationService = geolocationService;
    }

    public Temperature getTemperatureByCityName(String cityName) {
        String url = String.format("http://api.openweathermap.org/data/2.5/weather?q=%s&APPID=%s&units=metric", cityName, API_KEY);
        return getTemperature(url);
    }

    public Temperature getTemperatureByGeolocation() {
        Location location = geolocationService.getCoords();
        double lat = location.getLat();
        double lng = location.getLng();
        String url = String.format("http://api.openweathermap.org/data/2.5/weather?lat=%.2f&lon=%.2f&APPID=%s&units=metric", lat, lng, API_KEY);
        return getTemperature(url);

    }

    private Temperature getTemperature(String url) {
        JsonNode info = restTemplate.getForObject(url, JsonNode.class);
        String cityName = info.get("name").asText();
        int temperature = info.get("main").get("temp").asInt();
        return new Temperature(cityName, temperature);
    }


}

我认为 JsonNode 有问题,因为当我使用 map 进行映射时,不会出现此问题。

【问题讨论】:

    标签: java spring spring-boot jar


    【解决方案1】:

    如果您查看错误堆栈跟踪..

    它显示404 Not Found 异常。 (查看您发布的堆栈跟踪)。

    Caused by: org.springframework.web.client.HttpClientErrorException$NotFound: 404 Not Found
            at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:85) ~[spring-web-5.1.4.RELEASE.jar!/:5.1.4.RELEASE]
    

    这意味着您尝试点击的 REST-Api 不可用。

    现在来看这行代码(你想做什么)..

    private Temperature getTemperature(String url) {
            JsonNode info = restTemplate.getForObject(url, JsonNode.class);  //url not Found.
            String cityName = info.get("name").asText();
            int temperature = info.get("main").get("temp").asInt();
            return new Temperature(cityName, temperature);
        }
    

    在上面的代码中,url 没有响应(原因:不可用)。 得到null 作为响应后,您尝试解析。

    这里没有能够处理404 Exception 的异常处理程序。因此,异常必须由 jvm 的默认处理程序处理。

    更多关于错误响应代码 404 Click Here

    【讨论】:

    • 我知道,但为什么呢?当我在 IntelliJ 中运行它时,它工作正常。
    • 我应该用它做什么?
    猜你喜欢
    • 2018-10-15
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    • 2013-09-09
    相关资源
    最近更新 更多