Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Environment variable option while running JAR?

I have a JAR which accepts environment variable options. When I run the main class manually by setting run configuration, I provide environment variable as : KERBOROS_KEYTAB_LOC="location of the keytab file"

Now I need to set these options while running the JAR. How I can set that? I tried below option but it is not working.

java -DKERBOROS_KEYTAB_LOC="location of the keytab file" -jar jarfile.jar
like image 954
Shwetali Avatar asked May 18 '26 21:05

Shwetali


2 Answers

On linux, execute

$ export KERBOROS_KEYTAB_LOC="location of the keytab file"

On windows

C:\>SomeDir>set KERBOROS_KEYTAB_LOC="location of the keytab file"

then run the jar as always

like image 109
admlz635 Avatar answered May 21 '26 12:05

admlz635


For Windows

  • Put this into a .bat file

    @echo off
    setlocal enabledelayedexpansion

    :: Load environment variables from .env file
    for /f "delims=" %%i in (Variables.env) do (
        set "%%i"
    )

    :: Run your jar
    java -jar YourJarFileName.jar

    :: Pause the script when exit occurs and wait for a key press
    echo Press any key to exit...
    pause >nul

    endlocal
  • Change Variables.env to your name.env file where you have defined your env variables
  • Lastly, change YourJarFileName.jar
  • Run this .bat file.

Note: Keep all these files, 3 of them in the same directory.

like image 26
Muhammad Usman Avatar answered May 21 '26 11:05

Muhammad Usman