new File(".").getAbsolutePath();
new File(".").getPath();
new File(application.getRealPath(request.getRequestURI())).getParent()
得到jboss运行目录 path=C:\jboss-4.0.5.GA\bin\
new FileInputStream("yourconfig/yourconf.properties");
使用相对路径:相对路径的根目录就是你的webapplication的根路径,即WEB-INF的上一级目录,将你的参数文件放在yourwebappyourconfigyourconf.properties,
new FileInputStream("./yourconfig/yourconf.properties");
b.Tomcat
在类中输出System.getProperty("user.dir");显示的是%Tomcat_Home%/bin
c.Resin
不是你的JSP放的相对路径,是JSP引擎执行这个JSP编译成SERVLET的路径为根.比如用新建文件法测试File f = new File("a.htm");
这个a.htm在resin的安装目录下
Java中取当前项目的根目录的方法
//因为类名为"Application",因此" Application.class"一定能找到
String result = Application.class.getResource("Application.class").toString();
int index = result.indexOf("WEB-INF");
if(index == -1)...{
index = result.indexOf("bin");
}
result = result.substring(0,index);
if(result.startsWith("jar"))...{
// 当class文件在jar文件中时,返回"jar:file:/F:/ ..."样的路径
result = result.substring(10);
}else if(result.startsWith("file"))...{
// 当class文件在class文件中时,返回"file:/F:/ ..."样的路径
result = result.substring(6);
}
if(result.endsWith("/"))result = result.substring(0,result.length()-1);//不包含最后的"/"
return result; 9.JavaScript获取项目根目录
//js获取项目根路径,如: http://localhost:8083/uimcardprj
function getRootPath(){
//获取当前网址,如: http://localhost:8083/uimcardprj/share/meun.jsp
var curWwwPath=window.document.location.href;
//获取主机地址之后的目录,如: uimcardprj/share/meun.jsp
var pathName=window.document.location.pathname;
var pos=curWwwPath.indexOf(pathName);
//获取主机地址,如: http://localhost:8083
var localhostPaht=curWwwPath.substring(0,pos);
//获取带"/"的项目名,如:/uimcardprj
var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
return(localhostPaht+projectName);
} 10.对ClassLoader的一点理解
在任意class中使用到了getClassLoader():取得该Class对象的类装载器,对于classLoader 的认识还是在后续工作中接触后不断建立起来。
装载类的过程非常简单:查找类所在位置,并将找到的Java类的字节码装入内存,生成对应的Class对象。
Java的类装载器专门用来实现这样的过程,JVM并不止有一个类装载器,事实上,如果你愿意的话,你可以让JVM拥有无数个类装载器.
类装载器自身也是一个类,它也需要被装载到内存中来,那么这些类装载器由谁来装载呢,总得有个根吧?
确实存在这样的根,它就是神龙见首不见尾的Bootstrap ClassLoader. 为什么说它神龙见首不见尾呢?
因为你根本无法在Java代码中抓住哪怕是它的一点点的尾巴,
尽管你能时时刻刻体会到它的存在,因为java的运行环境所需要的所有类库,都由它来装载,而它本身是C++写的程序,可以独立运行,可以说是JVM的运行起点,颤抖吧,骚年!
在Bootstrap完成它的任务后,会生成一个AppClassLoader(实际上之前系统还会使用扩展类装载器ExtClassLoader,它用于装载Java运行环境扩展包中的类)。
这个类装载器才是我们经常使用的,可以调用ClassLoader.getSystemClassLoader() 来获得。
我们假定程序中没有使用类装载器相关操作设定或者自定义新的类装载器,那么我们编写的所有java类通通会由它来装载,值得尊敬吧。
AppClassLoader查找类的区域就是耳熟能详的Classpath,也是初学者必须跨过的门槛,有没有灵光一闪的感觉,我们按照它的类查找范围给它取名为类路径类装载器。