搜尋
首頁Javajava教程springboot怎麼運行jar包讀取外部設定檔

方法一:相對路徑設定設定檔
(1)在jar包同級目錄建立設定檔conf.properties並寫入組態資料:

##confData=data


(2)開始寫入自動化測試程式碼

//from www.fhadmin.cn
public class Test{
    public String getData() throws IOException {
        //读取配置文件
        Properties properties = new Properties();
        File file = new File("conf.properties");
        FileInputStream fis = new FileInputStream(file);
        properties.load(fis);
        fis.close();

        //获取配置文件数据
        String confData = properties.getProperty("confData");
        System.out.println(confData);
    }
}

(3)執行jar套件

java -jar jarNanexxx

#方法二:絕對路徑設定設定檔

解決問題:使用相對路徑的方法在jar包同級目錄手動執行jar包時沒有問題,但使用linux系統的crontab檔定時調度時報錯,原因:因為我們手動執行某個腳本時,是在目前shell環境下進行的,程式能找到環境變數;而係統自動執行任務調度時,除了預設的環境,是不會載入任何其他環境變數的。因此就需要在crontab檔案中指定任務執行所需的所有環境變量,或在程式中使用絕對路徑。
(1)在jar包同級目錄建立設定檔conf.properties並寫入組態資料:

#confData=data

##(2)開始寫入自動化測試程式碼
//from www.fhadmin.cn
public class Test{
    public String getData() throws IOException {
       //获取jar包同级目录
        String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
        String[] pathSplit = path.split("/");
        String jarName = pathSplit[pathSplit.length - 1];
        String jarPath = path.replace(jarName, "");
        String pathName=jarPath+"minhang.properties";
        System.out.println("配置文件路径:"+jarPath);

        //读取配置文件
        Properties properties = new Properties();
        File file = new File(pathName);
        FileInputStream fis = new FileInputStream(file);
        properties.load(fis);
        fis.close();

        //获取配置文件数据
        String confData = properties.getProperty("confData");
        System.out.println(confData);
    }
}

(3)執行jar套件

java -jar jarNanexxx

以上是springboot怎麼運行jar包讀取外部設定檔的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:亿速云。如有侵權,請聯絡admin@php.cn刪除

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前By尊渡假赌尊渡假赌尊渡假赌
威爾R.E.P.O.有交叉遊戲嗎?
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器