Java 守護程式由於其 jar 套件之外的設定檔出現 FileInputStreamException 而不斷失敗

Java 守護程式由於其 jar 套件之外的設定檔出現 FileInputStreamException 而不斷失敗

我想實作一個 Java 應用程式作為守護程式/服務,在我的 Raspberry Pi 上使用標準 Debian Stretch(核心版本 4.9)運行。

java 應用程式啟動,但隨後拋出異常,因為它無法讀取駐留在 jar 外部的重要設定檔。這是設計使然。我想將設定檔保留在 jar 之外。

我透過將設定檔放入 jar 並透過 InputStream 讀取檔案來運行它。但要求是設定檔不在 jar 內。透過終端手動啟動 jar 也可以。檔案權限應該很好,如工作手冊以 root 啟動所示。

我的預感是工作目錄在服務啟動期間會變得混亂。另一個預感是透過 FileInputStream 導航檔案系統會導致問題。

這是我的服務文件:

[Unit]
Description=collector

[Service]
User=root
CHDIR=/opt/servicedir/

#application.properties:
ExecStart=/usr/bin/java -jar /opt/servicedir/javaapp.jar
SuccessExitStatus=143
TimeoutStopSec=10
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target

/opt/servicedir/ 中所有內容的檔案權限: sudo chmod -R 770 /opt/servicedir/

系統日誌異常:

Aug 31 14:38:58 raspberrypi systemd[1]: myservice.service: Main process exited, code=exited, status=1/FAILURE
Aug 31 14:38:58 raspberrypi systemd[1]: myservice.service: Unit entered failed state.
Aug 31 14:38:58 raspberrypi systemd[1]: myservice.service: Failed with result 'exit-code'.
Aug 31 14:39:03 raspberrypi systemd[1]: myservice.service: Service hold-off time over, scheduling restart.
Aug 31 14:39:03 raspberrypi systemd[1]: Stopped service
Aug 31 14:39:03 raspberrypi systemd[1]: Started service
Aug 31 14:39:04 raspberrypi java[7982]: Exception in thread "main" java.lang.ExceptionInInitializerError
Aug 31 14:39:04 raspberrypi java[7982]: #011at foo.bar.Application.<clinit>(Application.java:20)
Aug 31 14:39:04 raspberrypi java[7982]: #011at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Aug 31 14:39:04 raspberrypi java[7982]: #011at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
Aug 31 14:39:04 raspberrypi java[7982]: #011at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
Aug 31 14:39:04 raspberrypi java[7982]: #011at java.lang.reflect.Method.invoke(Method.java:497)
Aug 31 14:39:04 raspberrypi java[7982]: #011at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
Aug 31 14:39:04 raspberrypi java[7982]: #011at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
Aug 31 14:39:04 raspberrypi java[7982]: #011at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
Aug 31 14:39:04 raspberrypi java[7982]: #011at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Aug 31 14:39:04 raspberrypi java[7982]: Caused by: java.lang.RuntimeException: Failed to load credentials.properties.
Aug 31 14:39:04 raspberrypi java[7982]: #011at foo.bar.Config.<clinit>(Config.java:60)
Aug 31 14:39:04 raspberrypi java[7982]: #011... 9 more
Aug 31 14:39:04 raspberrypi java[7982]: Caused by: java.io.FileNotFoundException: ./res/credentials.properties (Datei oder Verzeichnis nicht gefunden)
Aug 31 14:39:04 raspberrypi java[7982]: #011at java.io.FileInputStream.open0(Native Method)
Aug 31 14:39:04 raspberrypi java[7982]: #011at java.io.FileInputStream.open(FileInputStream.java:195)
Aug 31 14:39:04 raspberrypi java[7982]: #011at java.io.FileInputStream.<init>(FileInputStream.java:138)
Aug 31 14:39:04 raspberrypi java[7982]: #011at java.io.FileInputStream.<init>(FileInputStream.java:93)
Aug 31 14:39:04 raspberrypi java[7982]: #011at foo.bar.Application.Config.<clinit>(Config.java:44)
Aug 31 14:39:04 raspberrypi java[7982]: #011... 9 more
Aug 31 14:39:04 raspberrypi systemd[1]: myservice.service: Main process exited, code=exited, status=1/FAILURE
Aug 31 14:39:04 raspberrypi systemd[1]: myservice.service: Unit entered failed state.
Aug 31 14:39:04 raspberrypi systemd[1]: myservice.service: Failed with result 'exit-code'.

這是java程式碼中的程式碼行:

    //  this does not work :
    String credentialsFilePath = "./res/credentials.properties"
    try (FileInputStream in = new FileInputStream(credentialsFilePath)) {


    // this line works with the config file inside the jar
    String credentialsInJar = "credentials.properties"
    try (InputStream in = Config.class.getResourceAsStream(credentialsInJar)) {

答案1

您尚未在 systemd 單元中設定工作目錄。看起來像你故意的不過,要設定一個。

我懂了:

CHDIR=/opt/servicedir/

但沒有這樣的配置選項。

若要設定服務啟動的工作目錄,請使用WorkingDirectory=。例如:

WorkingDirectory=/opt/servicedir

相關內容