首先,Java的main方法有個初始化入參args,以下所示:java
public static void main(String[] args) {}
而後,在linux下執行jar包引入外部配置文件的命令(window下好比進入d: 一樣的道理,java -jar XXX.jar config.properties):linux
java -jar db2Util-uberjar.jar /opt/pasier/config.properties
其中的args就能夠用來加載上面紅色部分的配置文件路徑。spa
直接上代碼吧:code
1 public static void loadConf(String path) throws Exception { 2 Properties props = new Properties(); 3 InputStream in = new FileInputStream(path); 4 props.load(in); 5 fromDB = props.getProperty("fromDB"); 6 fromDBUser = props.getProperty("fromDBUser"); 7 fromDBPassword = props.getProperty("fromDBPassword"); 8 if (StringUtils.isEmpty(fromDB)) { 9 String errmsg = "fromDB or tables is null"; 10 logger.error(errmsg); 11 throw new Exception(errmsg); 12 } 13 }
1 public static void main(String[] args) { 2 loadConf(args[0]); 3 }