8

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
Laura
  • 8,100
  • 4
  • 40
  • 50
Shwetali
  • 111
  • 2
  • 2
  • 3
  • 2
    FYI, `-D` sets a Java _system property_, not an environment variable. These are different and separate mechanisms that can be used for roughly similar purposes. – dave_thompson_085 May 15 '19 at 21:29

3 Answers3

11

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

admlz635
  • 1,001
  • 1
  • 9
  • 18
2

Depends on what value you would like to pass as part of environment variable.

If its just key=valuepair where value is not location path, then you just use -Doption , just like how you do VM Args and run.

I had to pass EPAAS_ENV=e1 in environment variable and used below cmd

Java -DEPAAS_ENV=e1 -D<VM ARGS1> -D<VM ARGS2> -D<VM ARGS3> -jar <jarFile.jar>

enter image description here

-2

In general, You can pass the enviroment variable like below while running a jar file.

example:

java -Djava.security.auth.login.config="/path/kafkalogin.config" -jar myApplication.jar

Shabeeralimsn
  • 797
  • 4
  • 11
  • 32
  • 3
    using `-Dkey...`, is not for setting environment variables, but would set a java system property. See https://stackoverflow.com/questions/7054972/java-system-properties-and-environment-variables – admlz635 Jun 25 '20 at 16:26