Skip to content

Instantly share code, notes, and snippets.

@Lokno
Last active July 23, 2023 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lokno/4a53c597bbceab5095fcdac58795373e to your computer and use it in GitHub Desktop.
Save Lokno/4a53c597bbceab5095fcdac58795373e to your computer and use it in GitHub Desktop.
Searches for all java.exe executables on the host machine and runs each with the -version flag to display the Java version information.
@echo off
REM Batch Script: find_java_versions.bat
REM Description: Searches for all java.exe executables on the host machine and runs each with the -version flag
REM to display the Java version information.
REM Set the initial search directory to the root of the C drive (you can change this if needed)
set "search_dir=C:\"
echo Searching for any Java executables ^(java.exe^) in the %search_dir% directory...
REM Use the dir command to find all java.exe files on the host machine and store the results in a temporary file
dir /s /b "%search_dir%java.exe" > "%temp%\java_exe_list.txt"
echo.
REM Check if any java.exe files were found
if not exist "%temp%\java_exe_list.txt" (
echo No Java executables ^(java.exe^) not found on this machine.
) else (
echo Found Java executables:
echo.
REM Read and run each java.exe from the temporary file
for /f "usebackq delims=" %%a in ("%temp%\java_exe_list.txt") do (
echo Running: "%%a" -version
"%%a" -version
echo.
)
)
REM Clean up the temporary file
del "%temp%\java_exe_list.txt"
pause
@Lokno
Copy link
Author

Lokno commented Jul 23, 2023

If you find yourself in need of Java to run some small application, this is a way to check if you already have the runtime installed somewhere. Then simply add the directory of any version new enough to run your application to your user path.

Adding a directory to your user path in Windows:

  1. Open the Start menu and search for "Environment Variables" or "Edit the system environment variables." Select the appropriate option (usually found in the Control Panel or System Properties).
  2. In the System Properties window, click on the "Environment Variables" button.
  3. In the Environment Variables window, you'll see two sections: User variables for your user account and System variables for all users on the computer. For this example, we'll add the directory to the User variables, which will affect only your account.
  4. In the User variables section, look for the variable called "Path" (sometimes "PATH" with capital letters) and select it. Click on the "Edit" button.
  5. In the Edit Environment Variable window, click the "New" button.
  6. Type or browse to the directory you want to add to the PATH. For example, if you want to add "C:\my_folder" to the PATH, you would type or browse to that directory.
  7. Click "OK" to close all the windows.
  8. To apply the changes, you will need to close and relaunch any application, script, or command prompt you were using.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment