-1

I have a java maven project in which a library jar depends on configured environment variable so for running it in eclipse I created this variable in environment tab of maven run build(see screenshot). Now I have to package and export this swing app to executable jar and give user for testing. How to I pass this environment variable in the jar to be available at runtime. I think there should some possibility would be there to configure it in pom file or some properties file.

enter image description here

nanosoft
  • 2,913
  • 4
  • 41
  • 61

3 Answers3

1

you can package your project to a jar and provide a script to launch your program.

main entry in script is:

java -Xmx512M -cp "your jars" "Main method's Class"
rex
  • 63
  • 6
  • Do you mean to say VM argument are same as configured environment variable? The variable which is exported in linux shell is same as VM arguments? I seriously doubt it because run as maven build in eclipse as different tab for environment variables and different section for VM arguements under "Arguments" tab – nanosoft Jan 04 '17 at 08:51
  • no they are not the same thing, but you can still using my suggestion to solve your problem, java **-DGOOLE_API_KEY=XXXX** -cp "your jars" "Main method's Class". – rex Jan 04 '17 at 08:58
  • As I mentioned in my question its a swing application and will be used by users. It is not intended to be run by command line but by double click. User won't be passing any argument for running it... I need to bundle the environment variable inside the jar only. I believe there should be come configuration solution to this via maven or properties. – nanosoft Jan 04 '17 at 09:26
0

You can do that like below:-

 <property>
          <jvm.options>-Xmx512M</jvm.options>
    </property>
    <plugins>
    <plugin>
       <groupId>org.apache.maven.plugins</groupId>
       <artifactId>maven-compiler-plugin</artifactId>
       <version>3.3</version>
       <compilerArgs>
            <arg>${jvm.options}</arg>
       </compilerArgs>
    </plugin>
</plugins>
Amit Bhati
  • 5,569
  • 1
  • 24
  • 45
0

You can either use a -DDGOOLE_API_KEY=XXXX as suggested by @rex or do:

System.setProperty("GOOLE_API_KEY","XXXX");

Inside your code. You will have to make sure this loads before the library is reading it. Best, as a static block in a very premature loaded class.

public class {

  static {
    System.setProperty("GOOLE_API_KEY","XXXX");
  }

}

If you will use the command line -D option you have to give it to the customer as clear text, and this will make it very easy for them to use...

Meaning every one that will run your software will know your api key. If you will hardcode it inside the jar, it will be harder (but very possible) to get it.

If you will keep it in the code, it will make it easier for good guys to stay good.

galusben
  • 5,948
  • 6
  • 33
  • 52