3
我有一個主類罐子可以像執行:Java的罐子Test.jar的執行一個罐子與外部性文件
罐子裏面我有類似
public static void main(String[] args) throws IOException {
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("config.properties");
Properties prop = new Properties();
prop.load(is);
//then I wanna fetch all the properties in the config.properties file
}
我同時運行:
Java的罐子Test.jar的
Java的罐子Test.jar的-cp/TMP(其中共nfig.properties所在地)
Java的罐子Test.jar的-cp /tmp/config.properties(顯然這是行不通的,但給你什麼,我想在這裏實現)
代碼沒有工作,所有三個拋出NPE,但我把$ PATH和$ CLASSPATH下的config.properties文件的路徑。
問題是,從長遠來看,我會把配置文件放在$ {my-config-path}中,並正確地讀取/處理它。但暫時我只想要快速和骯髒的東西。
- 我不想在我的jar中包含屬性文件。
- 我想在外部保存在類路徑或路徑中,當我執行jar時,它找到它沒有問題。
'new FileInputStream(new File(「config.properties」))'? – Alexander 2012-04-12 16:53:11
你沒有文件的路徑,你會得到java.io.FileNotFoundException。 – Shengjie 2012-04-12 16:58:49
我想你可以在SO中找到如何使用'$ PATH'和我的評論來達到這個目的。 – Alexander 2012-04-12 17:06:52