java获取当前路径及加载配置文件

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

目录结构如下:


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



测试程序如下:


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的方式读取,使用相对路径,路径分隔符采用“/”。

你可能感兴趣的:(JAVA)