当配置位置是Windows绝对路径时,Spring Boot独立jar崩溃



我构建了一个依赖Spring Boot的应用程序。当我们即将部署它时,我开始在Eclipse和Gradle之外测试应用程序。这就是我们在Spring Boot的早期生命周期中遇到崩溃问题的地方。该应用程序在Eclipse和gradlew run中运行良好。但是,它在java -jar <App>.jar上有问题。

下面是我得到的异常。

java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
        at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.IllegalArgumentException: name
        at sun.misc.URLClassPath$Loader.findResource(URLClassPath.java:494)
        at sun.misc.URLClassPath.findResource(URLClassPath.java:176)
        at java.net.URLClassLoader$2.run(URLClassLoader.java:551)
        at java.net.URLClassLoader$2.run(URLClassLoader.java:549)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findResource(URLClassLoader.java:548)
        at org.springframework.boot.loader.LaunchedURLClassLoader.getResource(LaunchedURLClassLoader.java:63)
        at org.springframework.core.io.ClassPathResource.exists(ClassPathResource.java:138)
        at org.springframework.boot.env.PropertySourcesLoader.isFile(PropertySourcesLoader.java:88)
        at org.springframework.boot.env.PropertySourcesLoader.load(PropertySourcesLoader.java:74)
        at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:316)
        at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:295)
        at org.springframework.boot.context.config.ConfigFileApplicationListener$Loader.load(ConfigFileApplicationListener.java:283)
        at org.springframework.boot.context.config.ConfigFileApplicationListener.addProperySources(ConfigFileApplicationListener.java:153)
        at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEnvironmentPreparedEvent(ConfigFileApplicationListener.java:135)
        at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEnvironmentPreparedEvent(ConfigFileApplicationListener.java:128)
        at org.springframework.boot.context.config.ConfigFileApplicationListener.onApplicationEvent(ConfigFileApplicationListener.java:117)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:98)
        at org.springframework.boot.context.event.EventPublishingRunListener.publishEvent(EventPublishingRunListener.java:92)
        at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:58)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:275)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:880)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:869)
        at hello.SampleController.main(SampleController.java:18)
        ... 6 more

我们使用Spring Boot 1.0.0.RC4。为了隔离这个问题,我从教程(http://projects.spring.io/spring-boot/#quick-start)中复制了代码,并启动了一个新的Eclipse Gradle项目。只有一个类文件。这是相当标准的,除了我用@RestController代替@Controller和删除@ResponseBody。因为这是我的应用程序所使用的。

Hello.java .

package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
public class SampleController {
    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }
    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleController.class, args);
    }
}

我有一个非常标准的build.gradle文件

apply plugin: 'java'
apply plugin: 'eclipse'
buildscript {
    repositories {
        maven { url 'http://repo.spring.io/libs-snapshot' }
        mavenLocal()
    }
    dependencies { classpath('org.springframework.boot:spring-boot-gradle-plugin:1.0.0.RC4') }
}
apply plugin: 'spring-boot'
repositories {
    mavenCentral()
    maven { url 'http://repo.spring.io/milestone' }
    maven { url "http://repo.spring.io/libs-snapshot" }
}
dependencies { compile 'org.springframework.boot:spring-boot-starter-web:1.0.0.RC4' }
task wrapper(type: Wrapper) { gradleVersion = '1.11' }
在这个测试hello应用程序中,我做了gradlew clean build,然后尝试了java -jar Hello.jar。想象一下,当我收到与上面相同的异常时,我有多么惊讶。也许我仍然遗漏了一些东西,但是这个结果似乎表明可能有一个问题与Spring Boot?如果你能给我一个解决方案,我将非常感激。任何解决方案(即使它是一个肮脏的变通方法)。

编辑1:

根据要求,我正在测试独立jar的机器是Windows(8.1)。

java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)

编辑2:

根据要求,我还测试了spring-boot-sample-web-ui。同样的异常仍然发生。

EDIT 3:致敬@DaveSyer

我可能发现了一个潜在的原因。环境变量SPRING_CONFIG_LOCATION似乎是导致异常的原因。当没有设置时,测试Hello应用程序和spring-boot-sample-web-ui应用程序按预期工作。

编辑4:

对于其他感兴趣的人,我已经将测试应用程序上传到GitHub (https://github.com/ChrisZhong/spring-boot-jar-sample)

我知道这个问题很老了,但是如果有人在Windows上遇到与SPRING_CONFIG_LOCATION设置相同的问题:

设置应用程序的路径。属性(或任何其他名称),您应该使用以下命令

set SPRING_CONFIG_LOCATION=file:C:/path/application.properties

(注意"file:"后面缺少双斜杠)您也可以将此路径设置为Windows环境变量。在Spring boot 1.1.2中成功测试了该设置。它可能也适用于旧版本。

如果你写

set SPRING_CONFIG_LOCATION=C:/path/application.properties

Spring启动不会查找属性文件并忽略它的设置。

或者如果你写

set SPRING_CONFIG_LOCATION=file://C:/path/application.properties

Spring boot将抛出一个UnknownHostException对于驱动器号"C"

RESOLUTION:您在SPRING_CONFIG_LOCATION环境变量中有一个(Windows)绝对文件路径,因此Boot试图将其解析为类路径资源并失败。解决方法是对任何配置位置使用"file://"前缀,它是一个文件(不在类路径上)。

异常是神秘的,不应该真的是必要的,所以我提出了一个票来解决这个问题:https://github.com/spring-projects/spring-boot/issues/486

原文:适用于我:

$ cd spring-boot-samples/spring-boot-sample-web-ui
$ gvm use gradle 1.9
$ gradle clean build
$ java -version
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)
$ java -jar build/libs/*.jar
...
<happy app>

最新更新