如何更改spring boot应用程序启动目录?

how to change springboot application start directory?

我在 CENTOS7 上 运行 作为服务的 springboot 应用程序。默认的起始目录是“/”,现在我想改成配置文件所在的“/home/centos/fpcyproxy”
怎么改?

日志文件:

2018-08-28 14:38:21.645  INFO 2075 --- [main] ljis.ykk.fpcyproxy.Application           : Starting Application v1.0.0 on ip-172-31-26-206.cn-northwest-1.compute.internal with PID 2075 (/home/centos/fpcyproxy/fpcyproxy-1.0.0.jar started by root in /)
2018-08-28 14:38:21.662  INFO 2075 --- [main] ljis.ykk.fpcyproxy.Application           : No active profile set, falling back to default profiles: default
2018-08-28 14:38:21.828  INFO 2075 --- [main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@19e1023e: startup date [Tue Aug 28 14:38:21 CST 2018]; root of context hierarchy
2018-08-28 14:38:24.673  INFO 2075 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8443 (https)
2018-08-28 14:38:24.739  INFO 2075 --- [main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-08-28 14:38:24.739  INFO 2075 --- [main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.31
2018-08-28 14:38:24.773  INFO 2075 --- [localhost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib]
2018-08-28 14:38:24.928  INFO 2075 --- [localhost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-08-28 14:38:24.928  INFO 2075 --- [localhost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3100 ms
2018-08-28 14:38:25.186  INFO 2075 --- [localhost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-08-28 14:38:25.189  INFO 2075 --- [localhost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-08-28 14:38:25.190  INFO 2075 --- [localhost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-08-28 14:38:25.190  INFO 2075 --- [localhost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-08-28 14:38:25.190  INFO 2075 --- [localhost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-08-28 14:38:25.493  INFO 2075 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-08-28 14:38:25.918  INFO 2075 --- [main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@19e1023e: startup date [Tue Aug 28 14:38:21 CST 2018]; root of context hierarchy
2018-08-28 14:38:26.114  INFO 2075 --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/fpcy],methods=[POST]}" onto public ljis.ykk.fpcyproxy.domain.FpcyResult ljis.ykk.fpcyproxy.MainController.fpcy(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)
2018-08-28 14:38:26.135  INFO 2075 --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-08-28 14:38:26.136  INFO 2075 --- [main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-08-28 14:38:26.176  INFO 2075 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-08-28 14:38:26.176  INFO 2075 --- [main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-08-28 14:38:26.487  INFO 2075 --- [main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-08-28 14:38:26.879  INFO 2075 --- [main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8443 (https) with context path ''
2018-08-28 14:38:26.886  INFO 2075 --- [main] ljis.ykk.fpcyproxy.Application           : Started Application in 6.306 seconds (JVM running for 7.104)

服务配置文件内容:

[Unit]
Description=fpcy proxy
After=multi-user.target

[Service]
Type=simple
ExecStart=/usr/bin/java -jar /home/centos/fpcyproxy/fpcyproxy-1.0.0.jar

[Install]
WantedBy=multi-user.target

OS版本:

CentOS Linux release 7.5.1804 (Core)
Linux version 3.10.0-862.3.2.el7.x86_64 (builder@kbuilder.dev.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) ) #1 SMP Mon May 21 23:36:36 UTC 2018

Java版本:

openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)

如果您正在谈论应用程序的主目录 - 尝试设置 WorkingDirectory 属性:

[Service]
WorkingDirectory=/home/centos/fpcyproxy

但是如果你说的是加载位于外部目录中的 application.properties,请尝试设置下一个 属性

ExactStart=/usr/bin/java -jar /home/centos/fpcyproxy/fpcyproxy-1.0.0.jar --spring.config.location=/home/centos/fpcyproxy/

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files