[英]Spring boot loader cannot find resource
我在使用spring-boot-maven-plugin
构建jar文件时遇到问题,在运行java -jar my_file_name.jar <args>
后,出现以下错误:
Java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:的java.base / jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)处的线程“ main”中的异常。 62)在org.springframework.boot在java.base / java.lang.reflect.Method.invoke(Method.java:567)在java.base / jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) org.springframework.boot.loader.Launcher.launch(Launcher.java:87)的.loader.MainMethodRunner.run(MainMethodRunner.java:48)在org.springframework.boot.loader.Launcher.launch(Launcher.java:50) )在org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)的原因是:com.ryankshah.pdp.server.PDP.setConfig(PDP.java:39)处的java.lang.NullPointerException 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;
}
}
收到NullPointerException
主要原因是,它从未实例化resourceLoader
变量,因此可以使用默认资源加载器实例化。
protected static ResourceLoader resourceLoader = new DefaultResourceLoader();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.