JavaDemo——一些获取路径的方法

1.在eclipse中测试

Demo:

/**
 * 2020年2月25日上午10:33:41
 */
package testGetPaths;

/**
 * @author XWF
 *
 */
public class TestGetPaths {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//通过系统属性集合获得用户当前工作目录
		String userDir = System.getProperty("user.dir");
		System.out.println("System获取路径:" + userDir);
		//通过class的classLoader
		String loaderPath = TestGetPaths.class.getClassLoader().getResource("").getPath();
		System.out.println("classLoader获取路径:" + loaderPath);
		//通过class的resource
		String resourcePath = TestGetPaths.class.getResource("").getPath();
		System.out.println("resourcePath获取路径:" + resourcePath);
		//通过Thread
		String threadPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
		System.out.println("thread获取路径:" + threadPath);
	}

}

eclipse执行结果:

  

2.打成jar包测试

编写java文件TestGetPaths.java

public class TestGetPaths{
	public static void main(String[] args){
		//通过系统属性集合获得用户当前工作目录
		String userDir = System.getProperty("user.dir");
		System.out.println("System获取路径:" + userDir);
		//通过class的classLoader
		String loaderPath = TestGetPaths.class.getClassLoader().getResource("").getPath();
		System.out.println("classLoader获取路径:" + loaderPath);
		//通过class的resource
		String resourcePath = TestGetPaths.class.getResource("").getPath();
		System.out.println("resourcePath获取路径:" + resourcePath);
		//通过Thread
		String threadPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
		System.out.println("thread获取路径:" + threadPath);
	}
}

编写manifest.mf文件(注意Main-Class下面要空一行)

Manifest-Version: 1.0
Class-Path: .
Main-Class: TestGetPaths

 编译:

javac -encoding UTF-8 .\TestGetPaths.java

打包:

jar cvfm test.jar .\manifest.mf .\TestGetPaths.class

执行:

java -jar .\test.jar

结果:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值