Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何扫描可执行文件的搜索路径_Java_Cmd_Path - Fatal编程技术网

Java 如何扫描可执行文件的搜索路径

Java 如何扫描可执行文件的搜索路径,java,cmd,path,Java,Cmd,Path,是否可以在Java中获取shell命令引用的文件路径 例如,当我在Windows命令提示符中键入php-v时,它会知道我指的是C:\php\php.exe(对于我自己的计算机),因为我将其添加到了系统路径变量中。在Java中也可以这样做吗 我知道您可以从Java获取Path环境变量,并使用String.split(;”)解析它,但我想知道是否有更直接的方法?请查看JGit库中的以下代码 您可以实现类似的功能 /** * Searches the given path to see if it

是否可以在Java中获取shell命令引用的文件路径

例如,当我在Windows命令提示符中键入
php-v
时,它会知道我指的是
C:\php\php.exe
(对于我自己的计算机),因为我将其添加到了系统路径变量中。在Java中也可以这样做吗


我知道您可以从Java获取Path环境变量,并使用
String.split(;”)
解析它,但我想知道是否有更直接的方法?

请查看JGit库中的以下代码

您可以实现类似的功能

/**
 * Searches the given path to see if it contains one of the given files.
 * Returns the first it finds. Returns null if not found or if path is null.
 *
 * @param path
 *            List of paths to search separated by File.pathSeparator
 * @param lookFor
 *            Files to search for in the given path
 * @return the first match found, or null
 * @since 3.0
 **/
protected static File searchPath(final String path, final String... lookFor) {
    if (path == null)
        return null;

    for (final String p : path.split(File.pathSeparator)) {
        for (String command : lookFor) {
            final File e = new File(p, command);
            if (e.isFile())
                return e.getAbsoluteFile();
        }
    }
    return null;
}

你也许可以用这段来自的摘录做点什么

String cmd=“which”+;//linux
String cmd=“where”+;//窗户
进程p=Runtime.getRuntime().exec(cmd);
BufferedReader stdInput=新的BufferedReader(新的InputStreamReader(p.getInputStream());
BufferedReader stdError=新的BufferedReader(新的InputStreamReader(p.getErrorStream());
//读取命令的输出
而((s=stdInput.readLine())!=null){
ref.log.setText(ref.log.getText()+s+“\n”);
ref.updateDisplay();
}
//从尝试的命令中读取任何错误
而((s=stdError.readLine())!=null){
ref.log.setText(ref.log.getText()+s+“\n”);
ref.updateDisplay();
}

输出应该包含文件的路径。

我不建议这样做,但您可以在linux上调用本机命令,如
which
,或在Windows上调用
where.exe
。IMHO,解析
路径
变量是一个更干净的选项。也许通过使用
jmx
?豆子可能会暴露这一点,但这只是一个想法。扫描路径不是一个好主意,因为尽管路径上有一个php,但可以直接调用另一个。我可能错过了一些。您可能只想查看一个窗口solution@pgmann我说的是系统
$PATH
环境变量,而不是java home。因此,基本上我希望以编程方式获得像@Amaud stated(
where
/
which
)这样的输出,而无需调用命令。我以前不知道这个命令。看来
java.lang.Runtime.exec
也要求命令具有绝对路径?@pemapmoder我不这么认为。。。我稍后再检查。@pemapmoder根据这篇文章,它继承了Java进程的路径:哦,谢谢,我可能把它和进程混淆了。
String cmd = "which "+<insert executable to find>; // linux
String cmd = "where "+<insert executable to find>; // windows

Process p = Runtime.getRuntime().exec(cmd);

BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
// read the output from the command
while ((s = stdInput.readLine()) != null) {
    ref.log.setText(ref.log.getText()+s+"\n");
    ref.updateDisplay();
}
// read any errors from the attempted command
while ((s = stdError.readLine()) != null) {
    ref.log.setText(ref.log.getText()+s+"\n");
    ref.updateDisplay();
}