如果从Windows环境变量path启动,如何获取Java中可执行文件的完整路径?

如果从Windows环境变量path启动,如何获取Java中可执行文件的完整路径?,java,windows,environment-variables,Java,Windows,Environment Variables,如果可执行文件保存在Windows环境变量path的一部分,我想获取用Java启动的可执行文件的路径 例如,我们使用下面的代码片段在windows中启动NOTEPAD。此处notepad.exe保存在Windows文件夹下,该文件夹是Windows环境变量PATH的一部分。所以这里不需要给出可执行文件的完整路径 Runtime runtime = Runtime.getRuntime(); Process process = runtime.exec("notepad

如果可执行文件保存在Windows环境变量
path
的一部分,我想获取用Java启动的可执行文件的路径

例如,我们使用下面的代码片段在windows中启动
NOTEPAD
。此处
notepad.exe
保存在
Windows
文件夹下,该文件夹是Windows环境变量
PATH
的一部分。所以这里不需要给出可执行文件的完整路径

Runtime runtime = Runtime.getRuntime();         
Process process = runtime.exec("notepad.exe");

所以我的问题是,如何获取Java程序中可执行文件/文件的绝对位置(在这种情况下,如果
notepad.exe
保存在
c:\windows
下,在Java程序中,我需要获取路径
c:\windows
),如果它们是这样从
路径
位置启动的?

您可以通过以下方式获得Windows中可执行文件的位置:

where <executable_name>
将输出:

C:\Windows\System32


没有内置的函数来执行此操作。但是您可以用shell在
PATH
上查找可执行文件的相同方式找到它

拆分
PATH
变量的值,迭代条目(应该是目录),第一个包含
notepad.exe
的条目是使用的可执行文件

public static String findExecutableOnPath(String name) {
    for (String dirname : System.getEnv("PATH").split(File.pathSeparator)) {
        File file = new File(dirname, name);
        if (file.isFile() && file.canExecute()) {
            return file.getAbsolutePath();
        }
    }
    throw new AssertionError("should have found the executable");
}
publicstaticvoidmain(字符串[]args){
模块m=新模块();
m、 printonemand();
扫描仪sods=新扫描仪(System.in);
路径p=Path.get(“Z:\\DIS\\Project\\Data.txt”);
路径p1=Path.get(“Data.txt”);
int count=p.getNameCount();
System.out.println(“文件是:”+p.toString());
System.out.println(“文件名P:+P.getFileName());
System.out.println(“名称:+p1.getFileSystem());
System.out.println(“路径中有:“+count+”元素”);
for(int i=0;i

}

没有内置的方法可以做到这一点。您需要手动分离
PATH
元素,并在每个元素中搜索
notepad.exe
system.env(“PATH”)
可能(我自己也不知道)。这很脆弱。这将取决于运行它的机器的配置。@duffymo它很脆弱,但没有其他选择(除非你像Amila的回答中提到的那样,在
的地方
)。甚至命令行实用程序(Unix版本的
where
)也是这样实现的(即,没有用于执行
execvp
样式查找的库函数)。感谢阿米拉的帮助。请注意,包含可执行文件的目录必须位于PATH环境变量中,以便“where”找到它。@Ehcnalb,
where
适用于Windows和Mac OS X。在Linux上,您可以使用
which
。感谢janos的帮助。请注意,在Windows上搜索时,您可能还需要搜索
文件=新文件(dirname,name+“.exe”)。如果你想让你的应用程序在所有标准的OSs上运行,你可能需要检查两种风格的文件。我如何访问txt中的数据。文件?如果你有一个新问题,你必须创建一个新问题并发布它。在此之前,确保您搜索您的问题是否已发布。
public static String findExecutableOnPath(String name) {
    for (String dirname : System.getEnv("PATH").split(File.pathSeparator)) {
        File file = new File(dirname, name);
        if (file.isFile() && file.canExecute()) {
            return file.getAbsolutePath();
        }
    }
    throw new AssertionError("should have found the executable");
}
public static void main(String[] args) {
    Module m = new Module();
    m.printOnDemand();

    Scanner sods = new Scanner(System.in);
    Path p = Paths.get("Z:\\DIS\\Project\\Data.txt");
    Path p1 = Paths.get("Data.txt");
    int count = p.getNameCount();

    System.out.println("file is :" + p.toString());
    System.out.println("file name P:" + p.getFileName());
    System.out.println("Names :" + p1.getFileSystem());
    System.out.println("There are :" + count + " elements in the path");


    for(int i = 0; i < count; i++)
    {
        System.out.println("Element :" + i + " is " + p.getName(i));

    }
}