Spring Boot loader 找不到资源

Posted

技术标签:

【中文标题】Spring Boot loader 找不到资源【英文标题】:Spring boot loader cannot find resource 【发布时间】:2020-01-03 18:07:11 【问题描述】:

我在使用spring-boot-maven-plugin 构建jar 文件时遇到问题,运行java -jar my_file_name.jar <args> 后出现以下错误:

线程“main”中的异常 java.lang.reflect.InvocationTargetException 在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native 方法) 在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.base/java.lang.reflect.Method.invoke(Method.java:567) 在 org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) 在 org.springframework.boot.loader.Launcher.launch(Launcher.java:87) 在 org.springframework.boot.loader.Launcher.launch(Launcher.java:50) 在 org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) 引起:java.lang.NullPointerException 在 com.ryankshah.pdp.server.PDP.setConfig(PDP.java:39) 在 com.ryankshah.pdp.server.PDP.(PDP.java:25) 在 com.ryankshah.pdp.server.PDP.main(PDP.java:71) ... 8 更多

编辑:我相信是因为我没有设置resourceLoader 变量——这应该被实例化成什么?

首先,这是我的项目结构:

这是我的 pom.xml(我运行 maven clean 然后安装来构建 jar):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ryankshah.pdp.server</groupId>
    <artifactId>pdp_server</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <main.class>com.ryankshah.pdp.server.PDP</main.class>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.1.5.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.ryankshah.pdp.server.PDP</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.ow2.authzforce</groupId>
            <artifactId>authzforce-ce-core-pdp-engine</artifactId>
            <version>13.3.1</version>
        </dependency>
        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-core</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.2.11</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>activation</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20180813</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.6</version>
        </dependency>
    </dependencies>
</project>

这是我加载资源的主文件:

package com.ryankshah.pdp.server;

import org.apache.commons.io.IOUtils;
import org.json.JSONObject;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class PDP

    // Server socket
    protected ServerSocket serverSocket;
    // Policy file loaded in config
    protected Resource configFile;
    // PDP Engine Object
    protected PDPEngine pdpEngine;

    protected static ResourceLoader resourceLoader;

    public PDP(int port, String config) throws IOException 
        setConfig(config);
        pdpEngine = new PDPEngine(configFile);
        System.out.println("Starting PDP Server...");
        this.serverSocket = new ServerSocket(port);
        System.out.println("PDP Server Started!");
    

    public ServerSocket getSocket() 
        return serverSocket;
    

    private void setConfig(String model) 
        switch(model) 
            case "RBIBA":
                configFile = resourceLoader.getResource("classpath:config/rbiba_pdp_conf.xml");
                break;
            case "BLP":
                configFile = resourceLoader.getResource("classpath:config/blp_pdp_conf.xml");
                break;
            case "CWall":
                configFile = resourceLoader.getResource("classpath:config/cwall_pdp_conf.xml");
                break;
            case "RBIBA_BLP":
                configFile = resourceLoader.getResource("classpath:config/rbiba_blp_pdp_conf.xml");
                break;
            case "RBIBA_CWall":
                configFile = resourceLoader.getResource("classpath:config/rbiba_cwall_pdp_conf.xml");
                break;
            case "BLP_CWall":
                configFile = resourceLoader.getResource("classpath:config/blp_cwall_pdp_conf.xml");
                break;
            case "RBIBA_BLP_CWall":
                configFile = resourceLoader.getResource("classpath:config/rbiba_blp_cwall_pdp_conf.xml");
                break;
        
    

    public static JSONObject readReport(String deviceID) throws IOException 
        Resource r = resourceLoader.getResource("classpath:reports/"+deviceID+".json");
        return new JSONObject(IOUtils.toString(r.getInputStream(), StandardCharsets.UTF_8));
    

    public static void main(String[] args) 
        try 
            final int PORT = Integer.parseInt(args[0]);
            final String CONFIG = args[1];
            PDP server = new PDP(PORT, CONFIG);

            while(true) 
                // Get client socket
                Socket socket = server.getSocket().accept();
                //clientList.add(socket);
                new ClientThread(socket, server).start();
            
         catch (IOException e) 
            e.printStackTrace();
        
    

编辑:有关更多信息,当我实例化 PDPEngine 时,我使用以下内容:

package com.ryankshah.pdp.server;

import org.apache.commons.io.FileUtils;
import org.ow2.authzforce.core.pdp.impl.BasePdpEngine;
import org.ow2.authzforce.core.pdp.impl.PdpEngineConfiguration;
import org.springframework.core.io.Resource;

import java.io.File;
import java.io.IOException;

public class PDPEngine

    private PdpEngineConfiguration pdpEngineConf;
    private BasePdpEngine pdp;

    public PDPEngine(Resource configResource) throws IOException 
        File f = File.createTempFile("config", ".tmp");
        FileUtils.copyInputStreamToFile(configResource.getInputStream(), f);
        pdpEngineConf = PdpEngineConfiguration.getInstance(f, (String)null, (String)null);
        pdp = new BasePdpEngine(pdpEngineConf);
    

    public BasePdpEngine getEngine() 
        return pdp;
    

    public PdpEngineConfiguration getConfig() 
        return pdpEngineConf;
    

【问题讨论】:

你的代码中没有mainfunction? @HarryCoder 是的,显然有:) 【参考方案1】:

您获得NullPointerException 的主要原因是它从未实例化您的resourceLoader 变量,您可以使用默认资源加载器进行实例化。

protected static ResourceLoader resourceLoader = new DefaultResourceLoader();

【讨论】:

以上是关于Spring Boot loader 找不到资源的主要内容,如果未能解决你的问题,请参考以下文章

使用可执行 Jar 时找不到 Spring-Boot 资源

Spring-Boot ResourceLocations 在资源/静态/css 中找不到 css 文件

带有 Spring Boot 的 Thymeleaf - 找不到静态资产 (CSS)

spring boot项目打成war包传到服务器(centos7.0)static下的资源丢失,找不到路径问题

spring boot 找不到实体

Spring Boot 找不到模板