论坛首页 Java企业应用论坛

有关取得当前文件路径的问题

浏览 8962 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2004-09-28  
如果我想取得当前文件的路径,我用过很多方法,但是始终没有找到一个比较好的方法。
另外,在eclipse环境下,在两个不同的project中,都调用System.getProperty("user.dir")期待输出当前的工作路径,出来的是不同的结果。其中一个project是简单的Application程序,另一个是J2EE工程,并部署到JBOSS下执行。
请教System.getProperty("user.dir")是如何判别执行的?
   发表时间:2004-09-28  
downpour 写道
如果我想取得当前文件的路径,我用过很多方法,但是始终没有找到一个比较好的方法。
另外,在eclipse环境下,在两个不同的project中,都调用System.getProperty("user.dir")期待输出当前的工作路径,出来的是不同的结果。其中一个project是简单的Application程序,另一个是J2EE工程,并部署到JBOSS下执行。
请教System.getProperty("user.dir")是如何判别执行的?


可以在你的jvm参数添加一个路径参数,如-Dmy.dir= "(这里写上文件的绝对路径)",然后程序里可以通过System.getProperty("my.dir")来获取引号里的值
0 请登录后投票
   发表时间:2004-09-28  
我试过这一方法,但是感觉无法从根本上解决问题。只能在调试上起到一定得作用,毕竟开发完了以后,就不是运行在我所调试得机器上面了,那我又如何知道最终的路径情况呢?
0 请登录后投票
   发表时间:2004-09-28  
IBM DW上面有一个JWhich工具,可以根据Class给出在硬盘的绝对路径,其实他的原理很简单的,你也可以拿过来应用于任何资源文件,你可以自己去看看,我不想都讲出来,让别人没有思考的机会,呵呵。
0 请登录后投票
   发表时间:2004-09-28  
谢谢robbin的回复,通过使用JWhich,我已经能够驾驭文件的路径了,其实我发觉我原先用的方法和它很相似,只不过我对类的装载过程不理解,所以虽然我原本也用了Object.class.getResource方法,却没有得到正确的路径,读了你介绍的那篇文章,对这方面的知识稍有了点了解。

我把相关的两篇文章的链接直接给出,有类似问题的朋友可以不用费心找了:
http://www-900.ibm.com/developerWorks/cn/java/l-secureclass/index.shtml

http://www-900.cn.ibm.com/developerWorks/cn/java/jw-tips/tip105/index.shtml
0 请登录后投票
   发表时间:2004-10-08  
看了一下JWhich,觉得有点肤浅,还是看看OpenSymphony公司的这个东东,好东西。稍微修改一下,可以用到很多地方,呵呵

这个还不够,可以去看看spring的org.springframework.core.io,很全,不过就是太多了点。

/*
 * Copyright (c); 2002-2003 by OpenSymphony
 * All rights reserved.
 */
package com.opensymphony.oscache.util;


/**
 * <p>This code is borrowed directly from OSCore, but is duplicated
 * here to avoid having to add a dependency on the entire OSCore jar.</p>
 *
 * <p>If much more code from OSCore is needed then it might be wiser to
 * bite the bullet and add a dependency.</p>
 */
public class ClassLoaderUtil {
    /**
     * Load a class with a given name.
     *
     * It will try to load the class in the following order:
     * <ul>
     *  <li>From Thread.currentThread();.getContextClassLoader();
     *  <li>Using the basic Class.forName();
     *  <li>From ClassLoaderUtil.class.getClassLoader();
     *  <li>From the callingClass.getClassLoader();
     * </ul>
     *
     * @param className The name of the class to load
     * @param callingClass The Class object of the calling object
     * @throws ClassNotFoundException If the class cannot be found anywhere.
     */
    public static Class loadClass(String className, Class callingClass); throws ClassNotFoundException {
        try {
            return Thread.currentThread();.getContextClassLoader();.loadClass(className);;
        } catch (ClassNotFoundException e); {
            try {
                return Class.forName(className);;
            } catch (ClassNotFoundException ex); {
                try {
                    return ClassLoaderUtil.class.getClassLoader();.loadClass(className);;
                } catch (ClassNotFoundException exc); {
                    return callingClass.getClassLoader();.loadClass(className);;
                }
            }
        }
    }
}
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics