---
title: " java获取路径的方式"
author: "hero213"
date: "2008-11-12T18:05:56Z"
tags: []
slug: "java"
draft: false
lastmod: "2008-11-12T18:07:44Z"
id: 266259
url: "https://hero213.iteye.com/blog/266259"
---

System.out.println(Thread.currentThread().getContextClassLoader().getResource(""));

 

 file:/F:/Tomcat%205.0/webapps/DOMTest/WEB-INF/classes/

 

System.out.println(DomFunction.class.getClassLoader().getResource("").toString());

 

file:/F:/Tomcat%205.0/webapps/DOMTest/WEB-INF/classes/

 

System.out.println(ClassLoader.getSystemResource(""));

 

null

 

System.out.println(DomFunction.class.getResource("/"));

 

file:/F:/Tomcat%205.0/webapps/DOMTest/WEB-INF/classes/

 

System.out.println(DomFunction.class.getResource("/")); //Class文件所在路径

 

file:/F:/Tomcat%205.0/webapps/DOMTest/WEB-INF/classes/

 

System.out.println(new File("/").getAbsolutePath());

 

F:\\ System.out.println(System.getProperty("user.dir"));

 

F:\\Tomcat 5.0\\bin

路径当中出现的是百分号是由于文件夹或文件名中的空格造成的。

如 ：tomcat 5.0则变成了Tomcat%205.0，其中%20即是空格.

String path = getClass().getProtectionDomain().getCodeSource().getLocation().getPath(); 必须是在非静态方法中才可以使用