1

I'm currently fighting with my IDE and usage of PATH, JAVE_HOME, etc. on Windows 7.

As stated in this question: PATH variable showing different values (command prompt vs environment variables dialog box) I've unfortunately copied my whole PATH (multiple time) in the user one using setx.

But there is something I don't understand: if I do a echo %PATH%, it seems that it prints the system path before the user one. However, it seems that when using the path, windows first search on the user one then the system.

I'm asking this because I'm trying to override the default JDK.

Example, when I do echo %path:;=&echo.% (to print path with carriage return)

C:\Program Files (x86)\Java\jdk1.7.0_07\bin
c:\program files\apache-maven-3.5.0\bin 
C:\Program Files (x86)\Java\jdk1.7.0_07\bin 
c:\program files\apache-maven-3.5.0\bin
C:\Program Files (x86)\Java\jdk1.7.0_07\bin
c:\program files\apache-maven-3.5.0\bin
C:\WINDOWS\System32
C:\ProgramData\Oracle\Java\javapath
C:\WINDOWS\system32 
C:\WINDOWS 
C:\WINDOWS\System32\Wbem
C:\WINDOWS\System32\WindowsPowerShell\v1.0\

etc.

C:\Users\asoub\java\open_jdk_8u40\bin 
C:\Program Files (x86)\Java\jdk1.7.0_07\bin 
c:\program files\apache-maven-3.5.0\bin
C:\Program Files (x86)\Java\jdk1.7.0_07\bin 
c:\program files\apache-maven-3.5.0\bin 
C:\Program Files (x86)\Java\jdk1.7.0_07\bin 
c:\program files\apache-maven-3.5.0\bin
C:\WINDOWS\System32
C:\ProgramData\Oracle\Java\javapath
C:\WINDOWS\system32 
C:\WINDOWS 
C:\WINDOWS\System32\Wbem
C:\WINDOWS\System32\WindowsPowerShell\v1.0\

But if I do java -version, I get openjdk version "1.8.0_40". And to confirm this, if I print only user path with C:\Users\asoub>For /F "Skip=2Tokens=1-2*" %A In ('Reg Query HKCU\Environment /V PATH 2^>Nul') Do @Echo %A=%C

PATH=C:\Users\asoub\java\open_jdk_8u40\bin
C:\Program Files(x86)\Java\jdk1.7. 0_07\bin
c:\program files\apache-maven-3.5.0\bin
C:\Program Files (x86)\Java\jdk1.7.0_07\bin
c:\program files\apache-maven-3.5.0\bin
C:\Program Files (x86)\Java \jdk1.7.0_07\bin
c:\program files\apache-maven-3.5.0\bin
C:\WINDOWS\System32
C:\ProgramData\Oracle\Java\javapath
C:\WINDOWS\system32
C:\WINDOWS
C:\WINDOWS\System32\Wbem
C:\WINDOWS\System32\WindowsPowerShell\v1.0\
C:\Program Files\SafeNet\Aut

The java binary file is of course in java\...\bin. The first folder read by Windows when using java -version is the first one from the user path, not the first one from the global path. But when the full path is printed, the first folder with a java executable shown is jdk 1.7.0 ...

So, does Windows echo the path: System first, user second, but uses it user first, system second?

2
  • Please share output from where java.
    – JosefZ
    Feb 20, 2020 at 16:22
  • Using 'where java' outputs 'C:\Users\asoub\java\open_jdk_8u40\bin\java.exe'
    – Asoub
    Feb 21, 2020 at 8:25

1 Answer 1

1

A long time ago (I didn't know the echo %path:;=&echo.% trick in those days), I wrote the following bat script. It could help finding possible inconsistencies in PATH and PATHEXT variables (and their registry origin).

Usage:

  • TestPath.bat         - mainly shows duplicities in PATH and PATHEXT variables,
  • TestPath.bat dir     - lists number of executables inside folders in PATH,
  • TestPath.bat dir all - lists names of executables inside folders in PATH.

The TestPath.bat script:

@ECHO OFF
set "HkcuEnv=HKCU\Environment"
set "HklmEnv=HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
REM set hk
SETLOCAL enableextensions enabledelayedexpansion
echo --- %date% %time% %~nx0 %*  

if /I "%~1" EQU "dir" (
    set path
    Call :printPath path "" dir %~2
    echo/
    echo tested using the following findstr regex: 
    echo "%pathext:;=$ %$"
    ENDLOCAL
    goto :eof
)

set pathext

Call :duplicity pathext

Call :printPath path ""

Call :duplicity path

set "HKCU_type="
set "HKCU_path="
for /F "tokens=2*" %%G in (
  'reg query HKCU\Environment /v Path 2^>NUL ^|findstr /I "path"'
  ) do (
    set "HKCU_type=%%G"
    set "HKCU_path=%%H"
  ) 
Call :printPath HKCU_path %HKCU_type%
if /I "%HKCU_type%"=="REG_EXPAND_SZ" Call :printPath HKCU_path %HKCU_type% Expanded

set "HKLM_type="
set "HKLM_path="
set "qqqq=HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
for /F "tokens=2*" %%G in ('reg query "%qqqq%" /v Path^|findstr /I "path"'
  ) do (
    set "HKLM_type=%%G" 
    set "HKLM_path=%%H" 
  ) 
Call :printPath HKLM_path %HKLM_type%
if /I "%HKLM_type%"=="REG_EXPAND_SZ" Call :printPath HKLM_path %HKLM_type% Expanded

echo/
pause
REM echo/
REM set HK
echo/
echo/Usage: %~nx0
echo/       %~nx0 dir
echo/       %~nx0 dir all
ENDLOCAL
goto :eof

:printPath
echo/
echo %~0 %~1 %~2 %~3 %~4
if "!%~1!" NEQ "" (
  set "ggg="!%~1:;=" "!""
  rem set "ggg=!ggg:\"="!"
  for %%G in (!ggg!) do (
    if /I "%~3" NEQ "expanded" (
        if /I "%~3" EQU "dir" (
            set "_partpath=%%~G"
            if not "%~4"=="" ( 
                echo/
                echo "%%~G"
                if /I "!_partpath:%SystemRoot%=!" EQU "%%~G" (
                    dir /B /A:-D "%%~G" | findstr /I "%pathext:;=$ %$"
                    echo errorlevel !errorlevel!
                    if errorlevel 1 pause
                    rem timeout /T 3 /NOBREAK >NUL
                ) else (
                    >NUL (dir /B /A:-D "%%~G" | findstr /I "%pathext:;=$ %$")
                    echo errorlevel !errorlevel!                system default
                    if errorlevel 1 pause
                )
            ) else (
                if NOT "%%~G"=="" (
                    for /F %%g in ('
                      2^>NUL dir /B /A:-D "%%~G" ^| findstr /I "%pathext:;=$ %$" ^| find "." /C
                    ') do echo "%%~G" %%g file(s^)
                ) else (
                    echo "%%~G" ? file(s^)
                )
            )
        ) else (
            echo "%%~G"
        ) 
    ) else (
        call echo "%%~G"
    )
  )
)
goto :eof

:duplicity
echo/
echo %~0 %~1 %~2
set /A "ii=0"
set "ggg="!%~1:;=" "!""
set "ggg=!ggg:\"="!"
for %%G in (!ggg!) do (
  set /A "ii+=1"
  set /A "jj=0"
  for %%g in (!ggg!) do (
    set /A "jj+=1"
    if /I "%%~G"=="%%~g" if !ii! LSS !jj! echo !ii!, !jj!: %%~g 
  )
)
goto :eof

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .