Java-设置非常基本的系统属性

Java-设置非常基本的系统属性,java,Java,我正在使用Spring Boot应用程序,并试图通过编程方式设置系统属性我不想设置硬编码的URL,它必须从类路径加载 问题是我的代码在IDE上运行良好,但当我尝试执行jar文件时,我得到: Exception in thread "main" java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at

我正在使用Spring Boot应用程序,并试图通过编程方式设置系统属性我不想设置硬编码的URL,它必须从类路径加载

问题是我的代码在IDE上运行良好,但当我尝试执行jar文件时,我得到:

 Exception in thread "main" java.lang.reflect.InvocationTargetException
         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
         at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
         at java.lang.reflect.Method.invoke(Unknown Source)
         at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
         at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
         at org.springframework.boot.loader.Launcher.launch(Launcher.java:51)
         at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:52)
 Caused by: java.nio.file.FileSystemNotFoundException
         at com.sun.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:171)
         at com.sun.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:157)
         at java.nio.file.Paths.get(Unknown Source)
我的代码:

public static void main(String[] args) {
    try {
        setSystemProperties();

        } catch (IOException e) {
            log.error("ERROR: Could not find my.properties file in classpath", e);
        }
    }

private static void setSystemProperties() throws IOException {
    ClassPathResource resource = new ClassPathResource("my.properties");
    System.setProperty("my.file", Paths.get(resource.getURI()).toString());
}

InvocationTargetException
在调用方法时出现一些底层异常时发生。在代码中添加下面的catch块以查看实际原因

catch (InvocationTargetException e) {
        // the real cause
        e.getCause().printStackTrace();
}

InvocationTargetException
在调用方法时出现一些底层异常时发生。在代码中添加下面的catch块以查看实际原因

catch (InvocationTargetException e) {
        // the real cause
        e.getCause().printStackTrace();
}

path.get(resource.getURI())
这就是问题所在。你能比较一下从IDE调用和从jar调用时,
resource.getURI()的结果吗?考虑到它在IDE中工作,这可能与它试图加载的文件的位置有关。根据,如果ZipFileSystemProvider在尝试内部调用toRealPath()时获得IOException,则可能会抛出FileSystemNotFoundException。如果要定义相对定义的文件的路径,请确保它位于执行代码的正确相对位置。或者,将路径更改为规范路径。
path.get(resource.getURI())
这就是问题所在。你能比较一下从IDE调用和从jar调用时,
resource.getURI()的结果吗?考虑到它在IDE中工作,这可能与它试图加载的文件的位置有关。根据,如果ZipFileSystemProvider在尝试内部调用toRealPath()时获得IOException,则可能会抛出FileSystemNotFoundException。如果要定义相对定义的文件的路径,请确保它位于执行代码的正确相对位置。或者,将路径更改为规范。
InvocationTargetException的不可访问捕获块。从不从try语句体引发此异常
InvocationTargetException不会告诉确切的错误,通过添加此代码,您将获得要修复的确切错误。有关更多详细信息,您可以参考此问题:您可以引发泛型异常,而不是引发IOException。
InvocationTargetException的不可访问捕获块。从不从try语句体引发此异常
InvocationTargetException不会告诉确切的错误,通过添加此代码,您将得到要修复的确切错误。您可以参考此问题了解更多详细信息:您可以引发泛型异常,而不是引发IOException。