java获取当前路径及加载配置文件(报错:java.lang.NullPointerException java.util.Properties$LineReader.readLine(Prop)

4 篇文章 0 订阅
2 篇文章 0 订阅

近期需要编写一个java的客户端插件,需要打成jar包运行,原本开发期好用的程序达成jar包因为路径问题就不好用了,所以研究了一下,发出程序以供参考。

目录结构如下:


达成jar包后目录结构如下:



测试程序如下:


  1. public class ClassLoaderTest {
  2. public static void main(String[] args) {
  3. // 通过classLoader 获取资源
  4. URL url1 = ClassLoaderTest.class.getClassLoader().getResource(
  5. "net/csdn/ClassLoaderTest.class");
  6. System.out
  7. .println("ClassLoaderTest.class.getClassLoader().getResource("
  8. + "\"net/csdn/ClassLoaderTest.class\") = [" + url1
  9. + "]");
  10. // 通过Class 获取资源
  11. URL url2 = ClassLoaderTest.class.getResource("ClassLoaderTest.class");
  12. System.out.println("ClassLoaderTest.class.getResource("
  13. + "\"ClassLoaderTest.class\") = [" + url2 + "]");
  14. // 通过user.dir获取路径
  15. String path = System.getProperty("user.dir");
  16. System.out.println("System.getProperty(\"user.dir\") = [" + path + "]");
  17. Properties prop = new Properties();
  18. // 通过File方式获取conf.properties
  19. String confPath = path.concat("src")
  20. .concat(File.separator).concat("java").concat(File.separator)
  21. .concat("resource").concat(File.separator).concat("conf")
  22. .concat(File.separator).concat("conf.properties");
  23. System.out.println(confPath);
  24. File file = new File(confPath);
  25. InputStream is = null;
  26. if (file.exists() && !file.isDirectory()) {
  27. try {
  28. is = new FileInputStream(file);
  29. prop.load(is);
  30. System.out.println("File Load Successful. Name is: ["
  31. + prop.getProperty("name") + "]");
  32. prop.clear();
  33. } catch (FileNotFoundException e) {
  34. e.printStackTrace();
  35. System.out.println("File not Found Exception.");
  36. } catch (Exception e) {
  37. e.printStackTrace();
  38. System.out.println("Properties load Exception.");
  39. } finally {
  40. if (is != null) {
  41. try {
  42. is.close();
  43. } catch (IOException e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. }
  48. } else {
  49. System.out.println("Can not find conf.properties from [" + confPath
  50. + "]");
  51. }
  52. // 通过ClassPath获取
  53. is = ClassLoaderTest.class.getClassLoader().getResourceAsStream(
  54. "conf/conf.properties");
  55. try {
  56. prop.load(is);
  57. System.out
  58. .println("ClassPath \"conf/conf.properties\" Load Successful. Name is: ["
  59. + prop.getProperty("name") + "]");
  60. prop.clear();
  61. } catch (Exception e) {
  62. e.printStackTrace();
  63. System.out.println("Properties load Exception.");
  64. } finally {
  65. if (is != null) {
  66. try {
  67. is.close();
  68. } catch (IOException e) {
  69. e.printStackTrace();
  70. }
  71. }
  72. }
  73. is = ClassLoaderTest.class.getClassLoader().getResourceAsStream(
  74. "conf\\conf.properties");
  75. try {
  76. prop.load(is);
  77. System.out
  78. .println("ClassPath \"conf\\conf.properties\" Load Successful. Name is: ["
  79. + prop.getProperty("name") + "]");
  80. prop.clear();
  81. } catch (Exception e) {
  82. e.printStackTrace();
  83. System.out.println("Properties load Exception.");
  84. } finally {
  85. if (is != null) {
  86. try {
  87. is.close();
  88. } catch (IOException e) {
  89. e.printStackTrace();
  90. }
  91. }
  92. }
  93. }
  94. }
public class ClassLoaderTest {

	public static void main(String[] args) {

		// 通过classLoader 获取资源
		URL url1 = ClassLoaderTest.class.getClassLoader().getResource(
				"net/csdn/ClassLoaderTest.class");
		System.out
				.println("ClassLoaderTest.class.getClassLoader().getResource("
						+ "\"net/csdn/ClassLoaderTest.class\") = [" + url1
						+ "]");

		// 通过Class 获取资源
		URL url2 = ClassLoaderTest.class.getResource("ClassLoaderTest.class");
		System.out.println("ClassLoaderTest.class.getResource("
				+ "\"ClassLoaderTest.class\") = [" + url2 + "]");

		// 通过user.dir获取路径
		String path = System.getProperty("user.dir");
		System.out.println("System.getProperty(\"user.dir\") = [" + path + "]");

		Properties prop = new Properties();

		// 通过File方式获取conf.properties
		String confPath = path.concat("src")
				.concat(File.separator).concat("java").concat(File.separator)
				.concat("resource").concat(File.separator).concat("conf")
				.concat(File.separator).concat("conf.properties");
		System.out.println(confPath);

		File file = new File(confPath);
		InputStream is = null;
		if (file.exists() && !file.isDirectory()) {
			try {
				is = new FileInputStream(file);
				prop.load(is);
				System.out.println("File Load Successful. Name is: ["
						+ prop.getProperty("name") + "]");
				prop.clear();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				System.out.println("File not Found Exception.");
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("Properties load Exception.");
			} finally {
				if (is != null) {
					try {
						is.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		} else {
			System.out.println("Can not find conf.properties from [" + confPath
					+ "]");
		}

		// 通过ClassPath获取

		is = ClassLoaderTest.class.getClassLoader().getResourceAsStream(
				"conf/conf.properties");
		try {
			prop.load(is);
			System.out
					.println("ClassPath \"conf/conf.properties\" Load Successful. Name is: ["
							+ prop.getProperty("name") + "]");
			prop.clear();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("Properties load Exception.");
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}

		is = ClassLoaderTest.class.getClassLoader().getResourceAsStream(
				"conf\\conf.properties");
		try {
			prop.load(is);
			System.out
					.println("ClassPath \"conf\\conf.properties\" Load Successful. Name is: ["
							+ prop.getProperty("name") + "]");
			prop.clear();
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("Properties load Exception.");
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}

	}
}


开发期程序运行结果如下:


ClassLoaderTest.class.getClassLoader().getResource("net/csdn/ClassLoaderTest.class") = [file:/E:/workspace/test/bin/net/csdn/ClassLoaderTest.class]


ClassLoaderTest.class.getResource("ClassLoaderTest.class") = [file:/E:/workspace/test/bin/net/csdn/ClassLoaderTest.class]


System.getProperty("user.dir") = [E:\workspace\test]


E:\workspace\testsrc\java\resource\conf\conf.properties


Can not find conf.properties from [E:\workspace\testsrc\java\resource\conf\conf.properties]


ClassPath "conf/conf.properties" Load Successful. Name is: [csdn]


ClassPath "conf\conf.properties" Load Successful. Name is: [csdn]


达成jar包以后运行结果如下:


ClassLoaderTest.class.getClassLoader().getResource("net/csdn/ClassLoaderTest.cla
ss") = [jar:file:/C:/test.jar!/net/csdn/ClassLoaderTest.class]


ClassLoaderTest.class.getResource("ClassLoaderTest.class") = [jar:file:/C:/test.
jar!/net/csdn/ClassLoaderTest.class]


System.getProperty("user.dir") = [C:\]


C:\src\java\resource\conf\conf.properties


Can not find conf.properties from [C:\src\java\resource\conf\conf.properties]
ClassPath "conf/conf.properties" Load Successful. Name is: [csdn]


java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Unknown Source)
at java.util.Properties.load0(Unknown Source)
at java.util.Properties.load(Unknown Source)
at net.csdn.ClassLoaderTest.main(ClassLoaderTest.java:97)
Properties load Exception.


可见,达成jar包以后,jar包中的文件路径已经和开发期不同,不能再使用绝对路径。

即使使用jar包的路径,因为jar包是以zip文件格式存在,不能作为路径读取,所以url显示为test.jar!,表示不可以通过文件流进行读取。

如果需要开发期和jar包运行同时有效,需要使用Classpath的方式,而且不可以使用File.seperator作为路径分隔符,因为貌似从程序中不识别“\”作为相对路径分隔符,

所以建议在读取文件时,采用Classpath的方式读取,使用相对路径,路径分隔符采用“/”。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值