加载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!"

脚本命令加载外部配置文件夹conf的更多相关文章

  1. springboot加载外部配置文件

    网上搜集和整理如下(自己已验证过) 1. war包在tomcat中加载外部配置文件 war包运行在独立tomcat下时,如何加载war包外部配置application.properties,以达到每次 ...

  2. 19、属性赋值-@PropertySource加载外部配置文件

    19.属性赋值-@PropertySource加载外部配置文件 加载外部配置文件的注解 19.1 [xml] 在原先的xml 中需要 导入context:property-placeholder 声明 ...

  3. 【Spark】SparkStreaming-加载外部配置文件

    SparkStreaming-加载外部配置文件 spark加载配置文件_百度搜索 Spark加载外部配置文件 - CSDN博客 spark读取配置文件中的配置 - CSDN博客 spark加载prop ...

  4. SpringBoot系列——加载自定义配置文件

    前言 SpringBoot启动时默认加载bootstrap.properties或bootstrap.yml(这两个优先级最高).application.properties或application. ...

  5. 无阻塞加载外部js(动态脚本元素,XMLHttpRequest注入,LazyLoad)

    动态脚本元素即在js中去创建<script>标签加载外部js并执行,这样加载的好处是文件的下载和执行过程不会阻塞页面的其他进程.通过下面两个例子对比出效果 <!DOCTYPE htm ...

  6. 【Java Web开发学习】Spring加载外部properties配置文件

    [Java Web开发学习]Spring加载外部properties配置文件 转载:https://www.cnblogs.com/yangchongxing/p/9136505.html 1.声明属 ...

  7. Spark动态加载外部资源文件

    Spark动态加载外部资源文件 1.spark-submit --files 动态加载外部资源文件 之前做一个关于Spark的项目时,因项目中需要读取某个静态资源文件,然后在本地IDEA测试一切皆正常 ...

  8. selenium启动Chrome时,加载用户配置文件

    selenium启动Chrome时,加载用户配置文件   Selenium操作浏览器是不加载任何配置的,网上找了半天,关于Firefox加载配置的多点,Chrome资料很少,下面是关于加载Chrome ...

  9. UNITY_资源路径与加载外部文件

    UNITY_资源路径与加载外部文件 https://www.tuicool.com/articles/qMNnmm6https://blog.csdn.net/appppppen/article/de ...

随机推荐

  1. python 类中__init__,__new__,__class__的使用详解

    1.python中所有类默认继承object类,而object类提供了很多原始的内置属性和方法,所有用户定义的类在python 中也会继承这些内置属性.我们可以通过dir()进行查看.虽然python ...

  2. TypeError: $.ajaxFileUpload(…) is not a function

    今天做一个图片上传功能,用到了ajaxFileUpload,控制台报错TypeError: $.ajaxFileUpload(…) is not a function,都说是jQuery版本问题,也试 ...

  3. BZOJ3524 [Poi2014]Couriers 【主席树】

    题目 给一个长度为n的序列a.1≤a[i]≤n. m组询问,每次询问一个区间[l,r],是否存在一个数在[l,r]中出现的次数大于(r-l+1)/2.如果存在,输出这个数,否则输出0. 输入格式 第一 ...

  4. L#中 int.TryParse 有问题

    今天发现了一个 L# 的异常..因此记录一下 List<string> strList = new List<string>(); ; i<; ++i) { ; j< ...

  5. Js 中 == 与 === 的区别

    1.对于string,number等基础类型,==和===是有区别的 1)不同类型间比较,==之比较“转化成同一类型后的值”看“值”是否相等,===如果类型不同,其结果就是不等 2)同类型比较,直接进 ...

  6. Using AntiForgeryToken make it better

    原文发布时间为:2011-09-01 -- 来源于本人的百度文章 [由搬家工具导入] http://weblogs.asp.net/srkirkland/archive/2010/04/14/guar ...

  7. FCKEDITOR配置说明

    原文发布时间为:2009-10-12 -- 来源于本人的百度文章 [由搬家工具导入] fckeditor config.js配置2009-02-13 14:36 FCKConfig.CustomCon ...

  8. 在SQL Server中使用NewID()随机取得某行

    原文发布时间为:2008-09-24 -- 来源于本人的百度文章 [由搬家工具导入] 这里提供了另外一个更有用的函数:NewID(),它返回一个GUID(全局唯一标志符) select top 10 ...

  9. js 数组知识复习

    2.Array类型 2.1 创建数组 两种方式: 1.new Array(); //创建一个空数组 var arr1 = new Array(); //创建一个长度为10的空数组, var arr2 ...

  10. c语言命令行参数

    int main(int argc, char * argv[]) { ..... } argc: 代表启动程序时,命令行参数的个数.C和C++语言规定,可执行程序程序本身的文件名,也算是一个命令行参 ...