如何在java中获取当前目录? [英] how to get current directory in java?

查看:126
本文介绍了如何在java中获取当前目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从java获取我的Project的当前目录。我使用以下代码行来获取路径详细信息。



类型1:

 文件目录= new File(。); 
try {
System.out.println(当前目录的规范路径
+ directory.getCanonicalPath());
System.out.println(当前目录的绝对路径:
+ directory.getAbsolutePath());
} catch(Exception e){
System.out.println(Exceptione is =+ e.getMessage());
}

类型2:

  String currentDir = System.getProperty(user.dir); 
System.out.println(当前目录使用System:+ currentDir);

从主类执行上述代码时,我正在获取Project目录。当我从服务器端执行时,获取为当前目录使用System:D:\Apache Tomcat 6.0.16 \bin。但我的项目位于 D:\Apache Tomcat 6.0.16\wepapps\SampleStructs



请给我任何建议,帮助我解决这个问题。

解决方案

因为当你从主课执行一切都很好,但是这个代码在服务器上运行,它看起来当前的目录,目前的目录结构是从你已经启动服务器(run.bat)的Apache'bin'。


I am trying to get current directory of my Project from java. I am using the following lines of codes to get the path details.

Type 1:

File directory = new File (".");
try {
    System.out.println ("Current directory's canonical path: " 
            + directory.getCanonicalPath()); 
    System.out.println ("Current directory's absolute  path: " 
                + directory.getAbsolutePath());
}catch(Exception e) {
    System.out.println("Exceptione is ="+e.getMessage());
}

Type 2:

String currentDir = System.getProperty("user.dir");
System.out.println("Current dir using System:" +currentDir);

While executing the above codes from main class i am getting Project directory. When i executes from server side, gets as, "Current dir using System:D:\Apache Tomcat 6.0.16\bin". But my project is located in D:\Apache Tomcat 6.0.16\wepapps\SampleStructs.

Please give me any suggestions for this and help me out of this.

解决方案

It because when you execute from main class everything is fine, but this code runs on server it looks into current directory and current the directory structure is Apache 'bin' from where you have started the server(run.bat).

这篇关于如何在java中获取当前目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆