19

Possible Duplicate:
Is there an equivalent of ‘which’ on windows?

Failed to find it on Google, but just wondering if there is a way to reveal location of java by an equivalent command from Windows prompt.

Basically I have info from client that he doesn't set the JAVA_HOME but still can run java programs. I suspect it then must because the path to that java is set in the system PATH environment variable, but that is just too long to iterate in a quick way, also very painful (have to dig into sub-folders).

Thanks for any suggestion in advance!

Community
  • 1
  • 1
Michael Mao
  • 8,868
  • 21
  • 71
  • 91
  • Too long to iterate? Create an script/small program that iterates it for you. Just separate by ";" There is no need to dig into subfolders. – OscarRyz Aug 11 '10 at 00:14
  • 3
    Note: `JAVA_HOME` should point to JDK. Running Java programs doesn't per-se require JDK, just a JRE is also enough. The vast majority of endusers have just a JRE. – BalusC Aug 11 '10 at 00:18
  • @OscarRyz : that's doable, I am just curious whether there is a direct way of telling the location of java by executing a simple command from cmd :) – Michael Mao Aug 11 '10 at 00:20
  • With Cygwin installed you can do `which java` :) – Hamish Grubijan Aug 11 '10 at 00:23
  • If they have Python installed also, you can use [Ned Batchelder's](http://stackoverflow.com/users/14343/ned-batchelder) [wh.py](http://nedbatchelder.com/code/utilities/wh_py.html). – PTBNL Aug 11 '10 at 00:23
  • @BalusC: The question states that JAVA_HOME isn't always defined. – PTBNL Aug 11 '10 at 00:25
  • I don't think this should be tagged "java" at all. It is really all about windows batch files / scripting ... unless I'm missing something. – Stephen C Aug 11 '10 at 00:26
  • 8
    If you happen to be on Windows 7, there's a handy 'where' command. – thorncp Aug 11 '10 at 01:29
  • WHERE command also available on Windows 2012 – Rich C Apr 22 '15 at 21:41

4 Answers4

32

You can try:

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

This is a feature of the Windows for command and you can use for /? to get the details:

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.
paxdiablo
  • 772,407
  • 210
  • 1,477
  • 1,841
  • @paxdiablo : fantastic solution, may I know this is power-shell scripting? or not? – Michael Mao Aug 11 '10 at 00:24
  • 1
    No, standard `cmd.exe`. A lot of people seem to think it hasn't changed since the brain-dead DOS version but it's actually come a fair way. Nowhere near as good as my beloved `bash` of course, but workable :-) – paxdiablo Aug 11 '10 at 00:26
  • Workable maybe. Ugly definitely!! Don't you love how they implement this as a special case for the `for` command ... rather than as a general feature of % variable substitution. – Stephen C Aug 11 '10 at 00:48
  • 2
    Where *do* you find the documentation for this stuff? Where is there a decent "cmd.exe for unix users" guide? – Arafangion Aug 11 '10 at 01:04
  • 2
    http://www.robvanderwoude.com/batchfiles.php is an excellent site where all sorts of wonderful snippets have been gathered. This is my first point of call for education when I'm forced to develop scripts without CygWin. – paxdiablo Aug 11 '10 at 01:48
2

Here's what I normally use. If I were doing it again today, I'd probably do it a bit differently, but it works well enough that I haven't really had any reason to look at it for years (in fact, I'm pretty sure the last time I did anything to it was adding "cmd" to the list of extensions when I ported it from DOS to Win32...

// 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;
}
Jerry Coffin
  • 437,173
  • 71
  • 570
  • 1,035
2

Am I missing something here? What about trying the following simple command lines?

c:>dir /s java.exe

or

c:>dir /s javaw.exe

They will take time, but they will work. If you want to make it faster, start at "c:\Program files"

luiscolorado
  • 1,425
  • 2
  • 16
  • 22
  • 1
    Your "find"-like approach is definitely fine. The accepted answer checks the System Environment variable %PATH% because if under any dir "java -version" is fine then it usually means java.exe location has been added to the system path. So I reckon it is better in terms of speed :) – Michael Mao Sep 15 '10 at 22:41
1

The other answers look good. For completeness, I'll add that you can also distribute the JRE with your application. It is not as elegant as the other solutions, but it will work and you won't have to worry about which version of java the client has.

emory
  • 10,259
  • 1
  • 28
  • 55