unix“哪个java”在windows上等效命令?

时间:2010-08-11 00:11:39

标签: java windows

  

可能重复:
  Is there an equivalent of ‘which’ on windows?

无法在Google上找到它,但只是想知道是否有办法通过Windows提示符中的等效命令显示java的位置。

基本上我从客户端获得的信息是他没有设置JAVA_HOME但仍然可以运行java程序。我怀疑它必须因为java的路径是在系统PATH环境变量中设置的,但是这个时间太长而无法快速迭代,也非常痛苦(必须挖掘子文件夹)。

提前感谢任何建议!

4 个答案:

答案 0 :(得分:31)

您可以尝试:

c:\> for %i in (java.exe) do @echo.   %~$PATH:i
   C:\WINDOWS\system32\java.exe

这是Windows for命令的一项功能,您可以使用for /?获取详细信息:

In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:
    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string

The modifiers can be combined to get compound results:
    %~dpI       - expands %I to a drive letter and path only
    %~nxI       - expands %I to a file name and extension only
    %~fsI       - expands %I to a full path name with short names only
    %~dp$PATH:I - searches the directories listed in the PATH
                   environment variable for %I and expands to the
                   drive letter and path of the first one found.
    %~ftzaI     - expands %I to a DIR like output line

In the above examples %I and PATH can be replaced by other valid
values.  The %~ syntax is terminated by a valid FOR variable name.
Picking upper case variable names like %I makes it more readable and
avoids confusion with the modifiers, which are not case sensitive.

答案 1 :(得分:3)

我在这里遗漏了什么吗?那么尝试以下简单的命令行呢?

c:> dir / s java.exe

c:> dir / s javaw.exe

他们需要时间,但他们会工作。如果你想让它更快,请从“c:\ Program files”

开始

答案 2 :(得分:2)

这是我通常使用的。如果我今天再次这样做,我可能会做的有点不同,但它运作得很好,我没有任何理由多年来看它(事实上,我很确定最后一次当我将它从DOS移植到Win32时,我做了任何,它将“cmd”添加到扩展名列表中...

// Which.c:
#include <io.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *extensions[] = { "com", "exe", "bat", "cmd", NULL };

int is_exe(char *ext) {

    int i;

    for ( i = 0; extensions[i]; i++)
        if ( 0 == stricmp(ext, extensions[i] ) )
            return 1;
    return 0;
}

int main(int argc, char **argv) {

    char path[FILENAME_MAX];
    char buffer[FILENAME_MAX];
    char *path_var;
    char *ext;
    char *dir;
    int i;

    if (argc != 2) { 
        fprintf(stderr, "Usage: which <filename>\n");
        return 1;
    }

/* First try to find file name as-is.
 */
    if ( 0 == access(argv[1], 0)) {
        printf("\n%s", argv[1]);
        return 0;
    }

/* Okay, it wasn't found.  See if it had an extension, and if not, try
 * adding the usual ones...
 */

    ext = strrchr(argv[1], '.' );

    if ( 0 == ext++ || !is_exe(ext) ) {
        for ( i = 0; extensions[i]; i++) {

            sprintf(buffer, "%s.%s", argv[1], extensions[i]);

            if ( 0 == access(buffer, 0)) {
                printf("\n%s", buffer);
                return 0;
            }
        }

        if ( NULL == (path_var=getenv("PATH")))
            return 1;

        dir = strtok(path_var, ";");
        do {
            for ( i = 0; extensions[i]; i++) {

                sprintf(buffer, "%s\\%s.%s", dir, argv[1], extensions[i]);

                if ( 0 == access( buffer, 0)) {
                    printf("\n%s", buffer);
                    return 0;
                }
            }
        } while ( NULL != ( dir = strtok(NULL, ";")));
    }

    else {
        if ( NULL == (path_var=getenv("PATH")))
            return 1;

        dir = strtok(path_var, ";");
        do {
            sprintf(buffer, "%s\\%s", dir, argv[1]);

            if ( 0 == access( buffer, 0)) {
                printf("\n%s", buffer);
                return 0;
            }
        } while ( NULL != ( dir = strtok(NULL, ";")));
    }
    return 1;
}

答案 3 :(得分:1)

其他答案看起来不错。为了完整起见,我还要补充一点,您也可以将JRE与您的应用程序一起分发。它并不像其他解决方案那样优雅,但它可以工作,您不必担心客户端具有哪个版本的Java。