0

I have created an environment variable on my mac called "api_url". I want Java to read that variable and store it as a string.

Why doesn't the code below work? Notice at the bottom, the output is "null".

Java code with bash shell

user952342
  • 2,602
  • 7
  • 34
  • 54
  • 1
    And just for the record: even when using the correct call `getenv()` you have to make sure that the **process** that runs your IDE has seen that env variable. Exporting it in one shell doesnt make it visible to other, already existing processes! – GhostCat Feb 21 '19 at 17:11

2 Answers2

1

You misunderstood something here. Properties are information about the local system and configuration. To access environment variables, you have to use System.getenv() instead of System.getProperty().

Try String url = System.getenv("api_url");.

loicnestler
  • 517
  • 3
  • 8
0

You're using System.getProperty, which is for getting JVM properties - not environment variables. You need to use System.getenv("api_url") instead.

System.getenv(String) docs

apetranzilla
  • 5,331
  • 27
  • 34