gpt4 book ai didi

java - Spring Boot加载程序找不到资源

转载 作者:行者123 更新时间:2023-12-02 09:33:44 25 4
gpt4 key购买 nike

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

Exception in thread "main" java.lang.reflect.InvocationTargetException at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:567) 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:50) at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) Caused by: java.lang.NullPointerException at com.ryankshah.pdp.server.PDP.setConfig(PDP.java:39) at com.ryankshah.pdp.server.PDP.(PDP.java:25) at com.ryankshah.pdp.server.PDP.main(PDP.java:71) ... 8 more

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

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

enter image description here

这是我的 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;
}
}

最佳答案

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

protected static ResourceLoader resourceLoader = new DefaultResourceLoader();

关于java - Spring Boot加载程序找不到资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57727534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com