[英]Why is ! in the properties file path in spring-boot application on the docker?
I developed the rest API. 我开发了其余的API。 So, I tried to run it on the docker.
因此,我尝试在docker上运行它。 However, when I run it, it gives me file not found error.
但是,当我运行它时,它给了我文件找不到错误。
This is file list in the app.jar. 这是app.jar中的文件列表。
This is error message. 这是错误消息。
Caused by: java.io.FileNotFoundException: file:app.jar!/BOOT-INF/classes!/application_variable.properties (No such file or directory)
at java.io.FileInputStream.open0(Native Method) ~[na:1.8.0_111]
at java.io.FileInputStream.open(FileInputStream.java:195) ~[na:1.8.0_111]
at java.io.FileInputStream.<init>(FileInputStream.java:138) ~[na:1.8.0_111]
at java.io.FileInputStream.<init>(FileInputStream.java:93) ~[na:1.8.0_111]
at com.t3q.userManage.utils.SSOProperties.<init>(SSOProperties.java:31) ~[classes!/:0.0.1-SNAPSHOT]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_111]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_111]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_111]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_111]
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:172) ~[spring-beans-5.1.6.RELEASE.jar!/:5.1.6.RELEASE]
... 53 common frames omitted
How can I solve this problem? 我怎么解决这个问题? ps.
PS。 It works well in the eclipse!
在日食中效果很好!
ADDED. 添加。 This is my Dockerfile.
这是我的Dockerfile。
FROM java:8
VOLUME /tmp
EXPOSE 8080
ADD target/userManageWithRest-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
This is SSOProperties. 这是SSOProperties。
public class SSOProperties {
private static String servicePayloadDefault = "ID";
private static final Logger logger = LoggerFactory.getLogger(SSOProperties.class);
private String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
private String propertiesPath = "application_variable.properties";
private Properties props;
public SSOProperties() throws FileNotFoundException, IOException {
props = new Properties();
rootPath = rootPath.replaceAll("%20", " ");
rootPath = rootPath.replaceFirst("/", "");
props.load(new FileInputStream(rootPath + propertiesPath));
logger.info("rootPath="+rootPath);
}
...
}
Caused by: java.io.FileNotFoundException: file:app.jar!/BOOT-INF/classes!/application_variable.properties (No such file or directory)
引起原因:java.io.FileNotFoundException:file:app.jar!/ BOOT-INF / classes!/application_variable.properties(无此类文件或目录)
That is standard way of Java to convey classloading of resources from archives. 这是Java传达档案中资源类加载的标准方法。
What is after the !
之后是什么
!
is inside the app.jar
file. 在
app.jar
文件中。
Same thing for classes!
classes!
but that is inside a directory in an archive (the jar). 但这位于归档文件(jar)中的目录内。
Your problem is there : 您的问题在那里:
private String rootPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
//...
props.load(new FileInputStream(rootPath + propertiesPath));
You pass rootPath
that is String
path but that is included in a archive (the jar
), the FileInputStream
cannot access to it. 您传递的
rootPath
是String
路径,但包含在存档( jar
)中, FileInputStream
无法访问它。
What you need is getting the inputstream from the archive and to pass it to the Properties.load()
method: 您需要从档案中获取输入流,并将其传递给
Properties.load()
方法:
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(propertiesPath);
props.load(is);
Note that as you use Spring you could also just do : 请注意,当您使用Spring时,您也可以这样做:
ClassPathResource resource = new ClassPathResource(propertiesPath);
props.load(resource.getInputStream());
And you could even automate the properties loading thanks to @PropertySource
and @ConfigurationProperties
: 通过
@PropertySource
和@ConfigurationProperties
甚至可以自动加载属性:
@Configuration
@PropertySource("classpath:application_variable.properties")
@ConfigurationProperties
public class VariableProperties {
private String foo;
private String bar;
// getters and setters
}
And inject it wherever you need it. 并在您需要的任何地方注入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.