Skip to content

Instantly share code, notes, and snippets.

@phit
Last active October 21, 2017 00:08
Show Gist options
  • Save phit/1e70604e7e3e8720919fc857b10c224a to your computer and use it in GitHub Desktop.
Save phit/1e70604e7e3e8720919fc857b10c224a to your computer and use it in GitHub Desktop.
simple batch script to allow javaw in the windows firewall
@ECHO OFF
SETLOCAL EnableDelayedExpansion
goto check_Permissions
:check_Permissions
echo Administrative permissions required. Detecting permissions...
net session >nul 2>&1
if %errorLevel% == 0 (
echo Success: Administrative permissions confirmed.
goto parse_java_info
) else (
echo Aborting: Please right click and run as admin!
pause nul
)
:parse_java_info
ECHO Parsing java info...
::- Get the Java Version
set KEY="HKLM\SOFTWARE\JavaSoft\Java Runtime Environment"
set VALUE=CurrentVersion
reg query %KEY% /v %VALUE% 2>nul || (
echo Aborting: JRE not installed
pause nul
)
set JRE_VERSION=
for /f "tokens=2,*" %%a in ('reg query %KEY% /v %VALUE% ^| findstr %VALUE%') do (
set JRE_VERSION=%%b
)
::- Get the JavaHome
set KEY="HKLM\SOFTWARE\JavaSoft\Java Runtime Environment\%JRE_VERSION%"
set VALUE=JavaHome
reg query %KEY% /v %VALUE% 2>nul || (
echo Aborting: JavaHome not installed
pause nul
)
set JAVAHOME=
for /f "tokens=2,*" %%a in ('reg query %KEY% /v %VALUE% ^| findstr %VALUE%') do (
set JAVAHOME=%%b
)
GOTO add_exception_to_firewall
:add_exception_to_firewall
ECHO Adding exception to firewall..
SET NAME="Java Platform SE binary"
SET ACTION=allow
SET PROFILE=any
SET ENABLE=yes
SET JAVAW_PATH="!JAVAHOME!\bin\javaw.exe"
ECHO "JAVA_VER: !JRE_VERSION!"
ECHO "JAVAW_PATH: !JAVAW_PATH!"
FOR %%D IN (in out) DO (
FOR %%P IN (tcp udp) DO (
ECHO "Adding %%D rule for %%P"
netsh advfirewall firewall add rule name=%NAME% action=%ACTION% profile=%PROFILE% protocol=%%P enable=%ENABLE% dir=%%D program=%JAVAW_PATH%
)
)
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment