java中如何获得运行中的.class文件的存放目录的绝对路径?

比如运行一个.class,想让他输出自身存储位置的绝对路径,该怎么写代码??
其实我要的是system.property.("user.dir") 。不过你们也提示了我。

1、资源文件放在服务器下是完全没问题的,一个网站发布后也不会随便更新的。
2、如果资源文件过多、或都过大,是建议放到服务器下的,会占用服务器过大的空间,你可以在tomcat中再配置一个虚拟路径,指向一个盘符下一个文件夹(如:d:/images),在
tomcat

server.xml
中设置,然后你就可以用你的服务器地址+/img/+资源路径(注意:这个资源路径是相对d:/images的相对路径)去访问资源了
具体设置方法:
在conf目录下的server.xml文件里的里面加入
例如:
<
context
path=”test”
docbase=”f:\webroot\”/>
温馨提示:内容为网友见解,仅供参考
第1个回答  2015-12-11

类名.class.getResource("");

java.lang.Class.getResource() 查找给定名字的资源

import java.net.URL;import java.lang.*;public class ClassDemo {

  public static void main(String[] args) throws Exception {
   
    ClassDemo c = new ClassDemo();
    Class cls = c.getClass();

    // finds resource relative to the class location
    URL url = cls.getResource("file.txt");
    System.out.println("Value = " + url);

    // finds resource relative to the class location
    url = cls.getResource("newfolder/a.txt");
    System.out.println("Value = " + url);
  }}
结果:
Value = file:/C:/Program%20Files/Java/jdk1.6.0_06/bin/file.txt
Value = null

本回答被网友采纳
第2个回答  2008-09-08
System.out.println(YourClass.class.getResource(YourClass.class.getSimpleName()+".class").toString().substring(6));
第3个回答  2008-09-08
类名.class.getResource("");本回答被提问者采纳
第4个回答  2008-09-08
找一下javaAPI中的ClassLoader
相似回答