• main函数读取jar包外部的配置文件properties


    首先,Java的main方法有个初始化入参args,如下所示:

    public static void main(String[] args) {} 

     然后,在linux下执行jar包引入外部配置文件的命令(window下比如进入d: 同样的道理,java -jar XXX.jar config.properties):

    java -jar db2Util-uberjar.jar /opt/pasier/config.properties

    其中的args就可以用来加载上面红色部分的配置文件路径。

    直接上代码吧:

     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 }
  • 相关阅读:
    expandafter
    又回到了kde
    朗读软件
    tex bookmarks
    vim命令执行时间
    vim,tex的编译
    utorrent
    火狐的扩展
    linux 无线指示灯闪
    tex溢出报警
  • 原文地址:https://www.cnblogs.com/enshrineZither/p/3449861.html
Copyright © 2020-2023  润新知