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

时间:2014-03-11 21:44:08

标签: java eclipse spring gradle spring-boot

我构建了一个依赖于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项目。只有一个类文件。这是非常标准的,除了我用@Controller替换@RestController并删除了@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' }

使用此测试问候语应用程序,我执行了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。同样的例外仍然存在。

编辑3:对@DaveSyer的致敬

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

编辑4:

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

2 个答案:

答案 0 :(得分:1)

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

要设置application.properties(或任何其他名称)的路径,您应该使用此命令

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

(记住&#34后丢失的双斜线;文件:&#34;) 您还可以将此路径设置为Windows环境变量。使用Spring boot 1.1.2成功测试了此设置。它也适用于旧版本。

如果你写了

set SPRING_CONFIG_LOCATION=C:/path/application.properties

Spring boot无法查找属性文件并忽略它的设置。

或者如果你写

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

Spring boot会为驱动器号&#34; C&#34;抛出一个UnknownHostException。

答案 1 :(得分:0)

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

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

ORIGINAL:适合我:

$ 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>