`
newslxw
  • 浏览: 208268 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

java运行时获取java类所在目录的properties文件

阅读更多
public final class Config
{

    private static String fileSeparator = System.getProperty("file.separator");
    static
	{
		try
		{
			InputStream in = null;
			String currentJarPath = URLDecoder.decode(Config.class.getProtectionDomain().getCodeSource().getLocation().getFile(), "UTF-8"); //获取当前Jar文件名
			String path = Config.class.getResource("").getPath();			
			if(path.indexOf("!")!=-1)
			{//在jar中
				JarFile currentJar = new JarFile(currentJarPath);
				JarEntry dbEntry = currentJar.getJarEntry("conf/server.properties");
				if(dbEntry != null)
					in = currentJar.getInputStream(dbEntry);
			}
			if(in == null)
			{
				//String loaderPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
				String loaderPath = currentJarPath;
				String packPath = Config.class.getPackage().toString();
				packPath = packPath.replace("package ", "");
				packPath = "/" + packPath.replace(".", "/");
				if(!loaderPath.endsWith("/"))
					loaderPath = loaderPath.substring(0, loaderPath.lastIndexOf("/")) + fileSeparator;
				loaderPath = loaderPath.replace(packPath, "");
				String tPath = loaderPath+"conf"+fileSeparator+"server.properties";
				logger.info("Config class load server.properties, the path =" + tPath);
				in =new FileInputStream(tPath);
			}
			Properties prop = new Properties();
			prop.load(in);
}
分享到:
评论

相关推荐

    Java项目开发与毕业设计指导

    如果不能运行,将ch07文件夹下的swt.jar加入构建路径,然后在运行中的JVM参数中加入-Djava.library.path="dll所在目录的绝对路径"即可。 也可以在命令行下直接运行该程序,假设ch07文件夹放在D盘跟目录。则在cmd下...

    java 面试题 总结

    java编译器要求方法必须声明抛出可能发生的非运行时异常,但是并不要求必须声明抛出未被捕获的运行时异常。 6、说出Servlet的生命周期,并说出Servlet和CGI的区别。 Servlet被服务器实例化后,容器运行其init方法,...

    超级有影响力霸气的Java面试题大全文档

    java编译器要求方法必须声明抛出可能发生的非运行时异常,但是并不要求必须声明抛出未被捕获的运行时异常。 9、说出Servlet的生命周期,并说出Servlet和CGI的区别。  Servlet被服务器实例化后,容器运行其init方法...

    ant1.9资源

    若要在外部引入某文件,例如build.properties文件,可以通过如下内容将其引入:” build.properties”/> property元素可用作task的属性值。在task中是通过将属性名放在“${”和“}”之间,并放在task属性值的位置来...

    day020-继承加强和设计模式代码和笔记.rar

    它负责将 用户类路径(java -classpath或-Djava.class.path变量所指的目录, 即当前类所在路径及其引用的第三方类库的路径,如第四节中的问题6所述)下的类库 加载到内存中。 开发者可以直接使用系统...

    深入浅出Struts 2 .pdf(原书扫描版) part 1

    2.4.2 struts.properties文件 26 2.5 Struts应用程序示例 26 2.5.1 部署描述文件和Struts配置文件 27 2.5.2 动作类 28 2.5.3 运行app02a程序 29 2.6 依赖注入 29 2.6.1 概述 29 2.6.2 依赖注入的几种方式 31 2.7 小...

    zookeeper淘宝实现的监控

    目前开源世界中暂没有一个比较成熟的zookeeper-monitor,公司内部的各个zookeeper运行也都是无监控,无报表状态。于是开始zookeeper监控这块工作。 目前zookeeper-monitor能做哪些事情,讲到这个,首先来看看哪些...

    Log4j日志管理系统简单使用说明

    在实际编程时,要使Log4j真正在系统中运行事先还要对配置文件进行定义。定义步骤就是对Logger、Appender及Layout的分别使用,具体如下:    1、 配置根Logger,其语法为:  log4j.rootLogger = [ level ] , ...

Global site tag (gtag.js) - Google Analytics