1

I am writing an application in Java and I need to use environmental variables in AWS EC2 machine (Linux). I am using System.getenv("myvariable") in the application to get the particular environment variable. I need to set the permanent environmental variables and currently I have set the variable in ~/.bashrc , ~/.profile and also ~/.bash_profile. I am giving export myvariable=xyz in al the 3 files and then I ran source ~/.bashrc , source ~/.profile and source ~/.bash_profile. However I am getting null in the application.

I still don't understand why I am not able to get any environmental variables even though I am exporting the variable in ~/.bash_profile.I have even checked by running echo $myvariable and I can see xyz.

If I set the same environment variable in my local MAC machine in the same way I set above and use the same shell to run the same java code, I can see the variable with the value.

So basically I am getting null every time in my AWS EC2 linux machine.

Is there any other place I need to set the variable? I have even restarted the machine but it didn't help.

ra1ned
  • 337
  • 5
  • 19
  • 2
    It sounds like your Java application isn't running as the same user so it isn't getting the same environment variables. How do you run the Java application? – Mark B Dec 29 '16 at 22:00
  • I am using spring-boot to run my maven application. So I issue this command sudo ./mvnw spring-boot:run to run my application . – Sreenatha M D Dec 29 '16 at 22:05
  • 2
    By default, `sudo` doesn't preserve environment variables, so when you're running your application via `sudo`, all the environment variables you need will not be available to the application. Any particular reason why your service needs to be run as `sudo`? Port privileges? – wkl Dec 29 '16 at 22:14
  • As my application create a target folder and needs permission to create .class files in the target folder during the runtime. So is there any way we can make those environmental variables available in sudo? – Sreenatha M D Dec 29 '16 at 22:20
  • Thanks for suggesting that. I just used sudo -E /mvnw spring-boot:run and I am able to see the value for environmental variable. :) – Sreenatha M D Dec 29 '16 at 22:30
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. Also see [Where do I post questions about Dev Ops?](http://meta.stackexchange.com/q/134306) – jww Dec 30 '16 at 08:40

1 Answers1

3

It sounds like the problem is how you are setting the env variables. Sudo does not preserve them by default, so you need to use the sudo -E option.

This is explained in more detail in: How to keep Environment Variables when Using SUDO

gsaslis
  • 3,066
  • 2
  • 26
  • 32