4

Is there a way to set an environment variable or some kind of global variable in java so it can be visible from within the c++ code that I call from java (using swig). Off course as a workaround I can expose another function which will track the value of env var in some static variable in c++, so I can use it when it is needed. However it is interesting if there is a way to set env or global variable in java and make it visible in c++ code called from java.

Best regards -Grigor

1 Answers1

1

There are ways to set environment variables in Java but they are not particularly nice, see this previously asked question.

I think your best bet would be to use JNI.

Community
  • 1
  • 1
Jamie Reid
  • 532
  • 2
  • 17
  • Thanks for your response, actually I saw that questions however I wasn't able to get the environment variable within the dll after setting it in java. – Grigor Aleksanyan Feb 21 '15 at 15:41
  • Just to be clear Jamie, after setting env var in java (using one of the approaches mentioned in that thread) I can get correct value for it in java using System.getenv(my_var), However I wasn't able to do the same in the dll's code. Calling getenv function returns null for that env variable in c++. So the question is how should I get it in c++ ? using getenv , anything else or it is not possible at all ? – Grigor Aleksanyan Feb 21 '15 at 15:56
  • As I originally mentioned above, it is quite a bit of a hack and without seeing the code, it will be quite hard for me to be able to give you much help. I still stand by my original statement that the best way would be to use JNI. – Jamie Reid Feb 21 '15 at 19:29