简体   繁体   English

“mvn spring-boot:run”vs“java jar target / xxx.jar”

[英]“mvn spring-boot:run” vs “java jar target/xxx.jar”

I have two jks files under src/main/resources in my Spring Boot rest app 我的Spring Boot rest应用程序中的src / main / resources下有两个jks文件

  1. Test1.jks to make outbound call another rest api over https. Test1.jks通过https拨打另一个休息api。
  2. Test2.jks to enable https in this rest api. Test2.jks在这个rest api中启用https。

I am setting path to resource file test1.jks using relative like below in Spring Boot entry point which has been working fine. 我正在设置资源文件test1.jks的路径使用相似如下的Spring Boot入口点,它已经正常工作。 These jks files is used to call an https service. 这些jks文件用于调用https服务。

public class TestApp {

    static
    {
        System.setProperty("javax.net.ssl.trustStore", "TestApp.class.getClassLoader().getResource("test1.jks").getFile()");
        System.setProperty("javax.net.ssl.trustStorePassword", password);
        System.setProperty("javax.net.ssl.keyStore",  "TestApp.class.getClassLoader().getResource("test1.jks").getFile()");
        System.setProperty("javax.net.ssl.keyStorePassword", password);
    }

    public static void main(String[] args) {
        SpringApplication.run(TestApp.class, args);
    }
}

application.properties application.properties

server.port=8443
server.ssl.key-store=test2.jks
server.ssl.key-store-password=test
server.ssl.trust-store=classpath:test2.jks
server.ssl.trust-store-password=test
server.ssl.key-password=test

When I run using mvn spring-boot:run everything works fine, however when I run it as executable jar java -jar target/xxx-xxx-service-0.1.1-SNAPSHOT.jar I am getting the following error. 当我使用mvn spring-boot:runmvn spring-boot:run一切正常,但是当我将其作为可执行jar运行时java -jar target/xxx-xxx-service-0.1.1-SNAPSHOT.jar我收到以下错误。

Caused by: org.springframework.boot.context.embedded.EmbeddedServletContainerException: Could not find key store classpath:test2.jks
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.configureSslKeyStore(TomcatEmbeddedServletContainerFactory.java:292)
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.configureSsl(TomcatEmbeddedServletContainerFactory.java:271)
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.customizeConnector(TomcatEmbeddedServletContainerFactory.java:248)
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:147)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:157)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130)
        ... 13 more
Caused by: java.io.FileNotFoundException: class path resource [test2.jks] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/Users/xxx/xxx/Himalay/xxx/xxx-xxx-service/target/xxx-xxx-service-0.1.17-SNAPSHOT.jar!/test2.jks
        at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:212)
        at org.springframework.util.ResourceUtils.getFile(ResourceUtils.java:175)
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.configureSslKeyStore(TomcatEmbeddedServletContainerFactory.java:288)
        ... 18 more

After commenting application.properties I found it cannot read even test1.jks when run using the executable jar command but runs fine with mvn spring-boot:run , how can I make it run using executable jar command? 在评论application.properties之后,我发现在使用可执行jar命令运行时它甚至无法读取test1.jks但是使用mvn spring-boot运行正常:运行 ,如何使用executable jar命令运行它?

Note: If I use absolute path to the jks files, it works fine with both the commands. 注意:如果我使用jks文件的绝对路径,它可以正常使用这两个命令。

Try this : String filePath= Thread.currentThread().getContextClassLoader().getResource("your-trust-store.jks").getFile(); System.setProperty("javax.net.ssl.keyStore", filepath); 试试这个: String filePath= Thread.currentThread().getContextClassLoader().getResource("your-trust-store.jks").getFile(); System.setProperty("javax.net.ssl.keyStore", filepath); String filePath= Thread.currentThread().getContextClassLoader().getResource("your-trust-store.jks").getFile(); System.setProperty("javax.net.ssl.keyStore", filepath);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 mvn spring-boot:与 java -jar xxx.jar 相比运行缓慢 - mvn spring-boot:run slow compared to java -jar xxx.jar Spring Boot应用程序 - 使用“mvn spring-boot:run”和“java -jar”启动时间的差异 - Spring Boot application - difference in startup time with “mvn spring-boot:run” and “java -jar” 如何在类路径上运行带有额外文件的Spring Boot应用程序而不修改应用程序(xxx.jar) - how to run Spring Boot application with an extra file on classpath without modifying the application (the xxx.jar) Spring Boot+Java 9 Modules+Intellij Communicaty Edn:运行配置的行为不同于 jar 和 mvn spring-boot:运行 - Spring Boot+Java 9 Modules+Intellij Communicaty Edn: Run configuration behaves differently to jar and mvn spring-boot:run `java -Dspring.profiles.active -jar xxx.jar` 和 `java -jar xxx.jar --spring.profiles.active` 有什么区别 - What is the difference between `java -Dspring.profiles.active -jar xxx.jar` and `java -jar xxx.jar --spring.profiles.active` Spring-boot:运行正常,但java-jar不运行 - Spring-boot:run works but java-jar doesn't 如何执行 Spring 引导配置文件并使用 java -jar 命令以及 mvn spring-boot:run 命令传入命令行参数? - How to execute a Spring Boot profile and also pass in a command line argument using java -jar command and also with mvn spring-boot:run command? 使用 jar 文件运行时出现 Whitelabel 错误,但使用 war 文件以及使用命令 mvn spring-boot:run 运行时工作正常 - Whitelabel error when running with jar file, but works fine with war file and when run with command mvn spring-boot:run 缺少Codebase清单属性:xxx.jar - Missing Codebase manifest attribute for:xxx.jar 在Linux上运行时Spring-boot jar返回whitelabel错误 - Spring-boot jar return whitelabel error when run on Linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM