脚本命令加载外部配置文件夹conf

本文介绍了一种在Java应用程序中初始化Log4j并加载配置文件的方法。通过使用Log4iConfigurer类,确保了日志系统的正确配置。此外,还提供了一个Util工具类用于加载mapping.properties配置文件,并给出了Windows和Linux下的启动脚本示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

加载log4j配置文件 Log4iConfigurer类

public class Log4iConfigurer {
    
    private static boolean INITIALIZED = false;
    
    public static void initLogger(){
        if(!INITIALIZED&&!isConfigured()){
            InputStream is =Log4iConfigurer.class.getClassLoader().getResourceAsStream("log4j.properties");
            PropertyConfigurator.configure(is);
            IOUtils.closeQuietly(is);
        }
    }

    private static boolean isConfigured() {
        if(LogManager.getRootLogger().getAllAppenders().hasMoreElements()){
            return true;
        }else{
            Enumeration<?> loggers =LogManager.getCurrentLoggers();
            while(loggers.hasMoreElements()){
                Logger logger= (Logger)loggers.nextElement();
                if(logger.getAllAppenders().hasMoreElements()){
                    return true;
                }
            }
        }
        return false;
    }
    
}

读取配置文件类Util

public class Util {
    
    private static final Logger logger = LoggerFactory.getLogger(Util.class);
    
    /**mapping.properties file name*/
    public static final Properties props = new Properties();
    public static final String CONF_PROPERTIES_FILE= "mapping.properties";
    public static final String CONF = "RUN_CONF";
    
    static{
        Log4iConfigurer.initLogger();
        getProperties();
    }
    
    public static String GetString(String key){
        String value = null;
        value = props.getProperty(key);
        return value;
    }
    
    //读取配置文件
    private static File getConfProperties(){
        String confHome = System.getProperty(CONF);
        if(!StringUtils.isEmpty(confHome)){
            logger.info("Use CONF="+confHome);
            return getPropertiesFile(confHome);
        }
        
        logger.warn("Conf property was not set ,will seek conf env variable");
        
        String runHome = getRunHome();
        if(StringUtils.isEmpty(runHome)){
            throw new RuntimeException("didn't find project runpath,please set");
        }
        String path = runHome+File.separator+"conf";
        return getPropertiesFile(path);
    }
    
    public static void getProperties(){
        File propFile = getConfProperties();
        if(propFile == null||!propFile.exists()){
            logger.info("fail to load properties");
            throw new RuntimeException("fail to load properties");
        }
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(propFile);
            props.load(fis);
        } catch (IOException e) {
             throw new RuntimeException(e);
        } finally {
             IOUtils.closeQuietly(fis);
        }
    }
    
    private static File getPropertiesFile(String path){
        if(path == null){
            return null;
        }
        return new File(path,CONF_PROPERTIES_FILE);
    }
    
    private static String getRunHome(){
        String runHome = System.getProperty("runpath");
        if(StringUtils.isEmpty(runHome)){
            logger.warn("run home was not set");
        }
        return runHome;
    }
    
    
    public static void main(String[] args) {
        System.out.println(System.getenv("PATH"));
        System.out.println(System.getProperty("conf"));
        System.setProperty("run.home", "/home/dinpay/mappinghome");
        System.out.println(System.getProperty("run.home"));
    }
    
}

打包导出

   

另外附上window和linux的启动脚本命令

startup.cmd

echo start elasticsearch-mapping
java -Drunpath=C:\Users\ll-t150\Desktop\esmap -Dlog4j.configuration=conf/log4j.properties -cp lib/* com.dinpay.bdp.rcp.service.Start 
pause 

startup.sh

echo start elasticsearch-mapping
java -Drunpath=/home/dinpay/esmap -Dlog4j.configuration=conf/log4j.properties -classpath .:lib/* com.dinpay.bdp.rcp.service.Start
echo "create elasticsearch mapping successfully!"

 

转载于:https://www.cnblogs.com/atomicbomb/p/6625130.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值