无法在jar文件中执行CommandLineRunner

时间:2019-01-25 10:05:10

标签: java spring spring-boot jar

我在.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);
            }
        };
    }
}

TemperatureService:

@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进行映射时,不会发生此问题。

1 个答案:

答案 0 :(得分:0)

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

它显示DECLARE @Filtro int, @SQL NVARCHAR(MAX); SET @Filtro = 1; SET @SQL ='SELECT * FROM OpenQuery(MACPAC, ''SELECT FET001.ET0109, count(FET001.ET0101) FROM AUTO.D805DATPOR.FET001 FET001 WHERE (FET001.ET0104=''''POE'''') AND (FET001.ET0105=''''DIS'''') GROUP BY FET001.ET0109 HAVING COUNT (FET001.ET0101) > ''''' + CONVERT(NVARCHAR(10), @Filtro) + ''''''')'; EXEC sp_executesql @SQL; 异常。 (看看您发布的堆栈跟踪)。

404 Not Found

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

现在进入这一行代码(您尝试执行的操作)。

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]

在上面的代码中,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。因此,异常必须由jvm的默认处理程序处理。

有关错误响应代码404 Click Here

的更多信息