String appPath=WHMSAPApp.class.getProtectionDomain().getCodeSource().getLocation().getFile(); S.echo("原始appPath:"+appPath); //这样处理是为了兼顾开发和部署,开发环境得到的appPath和部署得到的不同: //开发环境下得到的形如:/D:/WorkSpace/MyEclipseWorkbench/whnbi_gwtt/bin/ //部署环境下得到的形如:/E:/whnbi_gwtt/lib/whnbi_gwtt.jar //如果直接在上面的路径下用相对路径导航,会进入jar内的目录(也就是以程序包路径解析相对路径)而不是以jar文件为单元 //如下处理后,都能得到正确的“main所在文件夹路径”,从而正确得到conf文件夹 appPath = appPath.substring(0,appPath.lastIndexOf("/")+1); S.echo("解析appPath:"+appPath); String log4jFile=appPath+"../conf/log4j.properties"; String configFile=appPath+"../conf/config.xml"; String log4jPath=null; String configPath=null; try { log4jPath = new File(log4jFile).getCanonicalPath(); configPath = new File(configFile).getCanonicalPath(); } catch (IOException e) { S.echo("未正确取得相关配置文件,请修正错误!"); e.printStackTrace(); return; } S.echo("日志文件路径:"+log4jPath); S.echo("配置文件路径:"+configPath); //配置日志文件 System.out.println("1.配置日志信息..."); PropertyConfigurator.configure(log4jPath); Logger logger=Logger.getLogger(WHMSAPApp.class); //读取配置文件 System.out.println("2.读取配置文件信息..."); ConfigUtil config = new ConfigUtil(configPath); if (!config.init()) { logger.error("读取配置文件错误!"); return; }