public void setProperties(String property_file) {
    Properties props = new Properties(System.getProperties());
    try {
      props.load(new BufferedInputStream(new FileInputStream(property_file)));
      System.setProperties(props);
    } catch (FileNotFoundException fne) {
      System.err.println("找不到文件:" + fne);
    } catch (Exception e) {
      ……
    }
  }以后就使用System.getProperty(String)

解决方案 »

  1.   

    vrmlman(vrmlman) 的方法是可以做到的!
    先把这些路径写在配置文件中XXX.properties文件中,如何写一个类进行实际与虚拟路径的转换。
    配制文件为:
    reportpath=D:/WebSphere/AppServer/installedApps/GMIMS.ear.ear/gmims.war/report/
    reporturl=/report/转换的类为:    public Config()
        {   
            reportPath = "c:/";
            reportURL = "http://localhost/report/";
            FileInputStream fio = null;
            Properties proInfo = new Properties(); try
            {
                fio = new FileInputStream("c:\\XXX.properties");
            }
            catch(FileNotFoundException ex)
            {
                System.out.println("FileNotFoundException While Open File:" + ex.toString());
            }
            catch(SecurityException ex)
            {
                System.out.println("SecurityException While Open File:" + ex.toString());
            }
            catch(Exception ex)
            {
    System.out.println("Exception While Open File:" + ex.toString());
            }
            try
            {
                proInfo.load(fio);
            }
            catch(IOException ex)
            {
                System.out.println("IOException While Load Info:" + ex.toString());
            }
            try
            {
                fio.close();
            }catch(Exception ex)
            {
                System.out.println("IOException While Close File:" + ex.toString());
            }setReportPath(proInfo.getProperty("reportpath", "c:/"));
            setReportURL(proInfo.getProperty("reporturl", "http://localhost/myapp/report/"));
    }public String getReportPath()
        {
            return reportPath;
        }    public String getReportURL()
        {
            return reportURL;
        }
     private void setReportPath(String newReportPath)
        {
            reportPath = newReportPath;
        }    private void setReportURL(String newReportURL)
        {
            reportURL = newReportURL;
        }
    }希望能给点帮助!
      

  2.   

    不是这个意思,我是想从程序中得到环境变量,比如JAVA_HOME之类的系统环境变量,需要类似于这样的函数String path=getPath("JAVA_HOME")来得到C:\j2sdk1.0.4
      

  3.   

    我以前试过,但是没有成功, 后来用的方法跟  huang_brid(如风--风之舞---水风轻吻) 说的差不多
      

  4.   

    如果依赖于环境变量,我所用的方法是针对不同的系统(没法子),在启动程序之前把环境变量的内容写入文件。直接从环境变量里面读取就不知道如何来做了,关注。Linux、FreeBSD 等:
    在shell脚本里 cat $MY_HOME > pathfile
    然后从pathfile 读取
    Dos 当然不能用cat了,可以用type,呵呵。
      

  5.   

    dos下用 echo %MY_HOME%,刚才说错了。