如何在java中获取执行目录路径 [英] How to get the execution directory path in java

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

问题描述

我制作了一个纯java应用程序,它告诉我给定目录中的文件数量。现在我使用以下代码设置当前目录:

I have made a pure java app which tells me about number of files in a given directory. Now I set current directory by using the following code:

`File f = new File(".");`

之后我用它的jar文件安装了一个安装程序并将其安装在我的Windows 8中然后我将其添加到Windows右键单击下拉菜单(上下文菜单)。当我从上下文菜单启动它时,它总是告诉我实际安装它的目录中的文件数量,但是我想知道从我执行它的那个目录的文件数量。

After that I made an installer with its jar file and installed it in my windows 8 and then I add it to the windows right click drop down menu (context menu). When I launch it from the context menu it always tells me about the number of files in the directory where it is actually installed in however I want to know the number of files of that directory from where I am executing it.

所以请帮助我。我是这个领域的新手,我不希望你把我混淆在当前目录和当前执行目录中。所以我写了这么久,并希望用非常简单的话来得到一个干净的答案。

So plz help me. I am a novice in this field and I don't want you to confuse me in current directory and the current execution directory. So I write this so long and hoping for a clean answer in very simple words.

谢谢

推荐答案

正如Jarrod Roberson在他的答案中所述:

As Jarrod Roberson states in his answer here:


一种方法是使用系统属性
System.getProperty(user.dir); 这将为您提供初始化属性时当前的
工作目录。这可能是你想要的
。找出 java 命令在
发出的位置,在你的情况下在包含要处理的文件的目录中,即使是实际的.jar文件也是
可能会驻留在
机器上的其他位置。在大多数情况下,拥有实际.jar文件的目录不是

One way would be to use the system property System.getProperty("user.dir"); this will give you "The current working directory when the properties were initialized". This is probably what you want. to find out where the java command was issued, in your case in the directory with the files to process, even though the actual .jar file might reside somewhere else on the machine. Having the directory of the actual .jar file isn't that useful in most cases.

以下将打印出$ b的当前目录无论
.class文件所在的.class或.jar文件位于何处,都会调用$ b命令。

The following will print out the current directory from where the command was invoked regardless where the .class or .jar file the .class file is in.

public class Test
{
    public static void main(final String[] args)
    {
        final String dir = System.getProperty("user.dir");
        System.out.println("current dir = " + dir);
    }
}  

如果你在 / User / me / 和包含上述代码
的.jar文件位于 / opt / some / nested / dir / 命令中 java -jar
/opt/some/nested/dir/test.jar测试
将输出当前目录=
/用户/ me

if you are in /User/me/ and your .jar file containing the above code is in /opt/some/nested/dir/ the command java -jar /opt/some/nested/dir/test.jar Test will output current dir = /User/me.

您还应该使用面向对象的
命令行参数解析器。我强烈推荐 JSAP ,即Java
Simple Argument Parser。这将允许你使用
System.getProperty(user.dir),或者传递一些
else来覆盖这种行为。一个更易于维护的解决方案。
这将使得在目录中传递非常容易,
并且能够回退到 user.dir 如果没有传递任何内容。

You should also as a bonus look at using a good object oriented command line argument parser. I highly recommend JSAP, the Java Simple Argument Parser. This would let you use System.getProperty("user.dir") and alternatively pass in something else to over-ride the behavior. A much more maintainable solution. This would make passing in the directory to process very easy to do, and be able to fall back on user.dir if nothing was passed in.

示例:GetExecutionPath

Example : GetExecutionPath

import java.util.*;
import java.lang.*;

public class GetExecutionPath
{
  public static void main(String args[]) {
    try{
      String executionPath = System.getProperty("user.dir");
      System.out.print("Executing at =>"+executionPath.replace("\\", "/"));
    }catch (Exception e){
      System.out.println("Exception caught ="+e.getMessage());
    }
  }
}

以上输出将是喜欢

C:\javaexamples>javac GetExecutionPath.jav

C:\javaexamples>java GetExecutionPath
Executing at =>C:/javaexamples

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

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