可以通过以下方法,获得类加载器的根路径(往往就是jar包的路径)
[color=red]String path = this.getClass().getClassLoader().getResource("");[/color]
getResource方法支持通过相对路径获取资源,例如classes文件夹(即类加载器路径)下有一个文件夹res,里面有一个文件log4j.xml,可以通过以下方法读取其url
[color=red]URL url=new URL(this.getClass().getClassLoader().getResource("")+"res"+File.separator+"log4j.properties");[/color]
[color=red]System.out.println(this.getClass().getClassLoader().getResource("res/log4j.properties").toString());[/color]
URL包含了资源的绝对路径信息,例如:
则会打印出:
[color=red]file:/E:/Workspaces/eclipse_j2ee/springTest/target/test-classes/res/log4j.properties[/color]
[color=red]String path = this.getClass().getClassLoader().getResource("");[/color]
getResource方法支持通过相对路径获取资源,例如classes文件夹(即类加载器路径)下有一个文件夹res,里面有一个文件log4j.xml,可以通过以下方法读取其url
[color=red]URL url=new URL(this.getClass().getClassLoader().getResource("")+"res"+File.separator+"log4j.properties");[/color]
[color=red]System.out.println(this.getClass().getClassLoader().getResource("res/log4j.properties").toString());[/color]
URL包含了资源的绝对路径信息,例如:
则会打印出:
[color=red]file:/E:/Workspaces/eclipse_j2ee/springTest/target/test-classes/res/log4j.properties[/color]