Linux 如何在java中获取当前目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14050131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:08:07  来源:igfitidea点击:

how to get current directory in java?

javalinuxjakarta-eestruts2directory

提问by Raghupathiraja

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.

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

Type 1:

类型 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:

类型 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.

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

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

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

采纳答案by Vallabh Patade

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).

这是因为当您从主类执行时一切都很好,但是此代码在服务器上运行,它会查看当前目录,当前目录结构是 Apache 'bin' 从您启动服务器的位置(run.bat)。

回答by goravine

you can use this code

你可以使用这个代码

String absolutePath = getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
        absolutePath = absolutePath.substring(0, absolutePath.lastIndexOf("/"));

this code is working before for me! it will return the full path of folder in windows or linux.

这段代码以前对我有用!它将返回 windows 或 linux 中文件夹的完整路径。

回答by pratikch

There are different context we are talking about here. 1. Running the application in standalone mode. 2. Running the application in container on server side. In #1, The application is run from the directory it is invoked.

我们在这里谈论的是不同的上下文。1. 以独立模式运行应用程序。2. 在服务器端的容器中运行应用程序。在#1 中,应用程序从它被调用的目录中运行。

But #2 case, the application is run relative to the container, so you see the location of server directory. This also shields the application code.

但是 #2 情况下,应用程序是相对于容器运行的,因此您可以看到服务器目录的位置。这也屏蔽了应用程序代码。

回答by Goran Jovic

First of all, the main cause of your problem is the difference between current working directory and the location of your executable. You should know that current working directory in Linux is not the directory where the executable is, but instead the current directory where the program was started from.

首先,问题的主要原因是当前工作目录和可执行文件的位置之间的差异。你应该知道Linux中的当前工作目录不是可执行文件所在的目录,而是程序启动的当前目录。

As an example, let's say you have a program currentwhich prints out the current directory and it is located in /home/user/scripts/.

例如,假设您有一个程序current打印出当前目录,它位于/home/user/scripts/.

If you do this:

如果你这样做:

cd /home/user/scripts
./current

It will print out: /home/user/scripts/But, if you do this:

它会打印出:/home/user/scripts/但是,如果你这样做:

cd /home/user/
scripts/current

The output will be: /home/user/

输出将是: /home/user/

As to the possible solutions, some of them I found useful are:

至于可能的解决方案,我发现其中一些有用的是:

  • Refer to your project resources relative to the classpath, see ClassLoader.getResourceAsStream()for more info
  • Refer to your configuration resources, like properties files and such, relative to the user home directory.
  • Put all other locations, such as media directory path and similar to the configuration file from the point above.
  • If all other options are not available or practical use getClass().getProtectionDomain().getCodeSource().getLocation().getPath(). See more about this approach and some possible issues here: How to get the path of a running JAR file?
  • 请参阅与类路径相关的项目资源,ClassLoader.getResourceAsStream()有关更多信息,请参阅
  • 请参阅相对于用户主目录的配置资源,如属性文件等。
  • 放置所有其他位置,例如媒体目录路径和类似于上面的配置文件。
  • 如果所有其他选项都不可用或不实用getClass().getProtectionDomain().getCodeSource().getLocation().getPath()。在此处查看有关此方法的更多信息以及一些可能的问题:如何获取正在运行的 JAR 文件的路径?