0

I can't find anything on how to work with Java environments (production, development, etc) since every time I search for "java environment variables" I get results of how to install Java on windows and windows environment variables (JAVA_HOME) or similar things. Could someone explain how Java environments work and where to find official documentation?

To put you in context, I'm a junior developer and I want to set a few different paths for my embedded database depending on whether I'm in production or development. My project doesn't have anything from spring or maven.

To better explain myself, I will give an example. In javascript, you can create files like .env , .env.development, etc. And inside them you put key value pairs. You can then retrieve that in your code. It dynamically returns the value depending on whether you are in development or production. I'm looking to do the same thing in Java but in a simple way. Without having to use Spring as it is extremely advanced

vktop
  • 51
  • 6
  • Does this answer your question? [Switch between Prod and Dev environment](https://stackoverflow.com/questions/43305246/switch-between-prod-and-dev-environment) – Zankrut Parmar Oct 16 '22 at 10:23
  • This isn't a Java concept; are you using a framework which supports different settings for development/production/testing, or do you just want to know how to read an environment variable in Java? – kaya3 Oct 16 '22 at 10:26
  • Thanks but no, that post is interesting but it doesn't help me much since the idea is to learn how environments work without having to get into Spring (Spring is too advanced for a junior). Isn't there a native Java way to work with .env files or something? – vktop Oct 16 '22 at 10:32
  • I know environments are not unique to Java, but I'm looking for a way to implement them in Java. I don't use any framework apart from JavaFX. It is a simple application to learn programming. And I would like to know how to work with environments without reaching levels as advanced as Spring – vktop Oct 16 '22 at 10:33
  • So then you just want to [read an environment variable](https://stackoverflow.com/q/20610080/12299000)? – kaya3 Oct 16 '22 at 10:45
  • To better explain myself, I will give an example. In javascript, you can create files like .env , .env.development, etc. And inside them you put key value pairs. You can then retrieve that in your code. It dynamically returns the value depending on whether you are in development or production. I'm looking to do the same thing in Java but in a simple way. Without having to use Spring as it is extremely advanced – vktop Oct 16 '22 at 10:50
  • I am not sure, but i think System.getEnvironment is nor what i am looking for since it returns you variables like JAVA_HOME. https://stackoverflow.com/questions/531694/how-can-i-get-system-variable-value-in-java – vktop Oct 16 '22 at 11:03
  • 1
    System.getEnvironment is exactly what you want. You will have to pass different variables depending on which system you are on. – Loading Oct 16 '22 at 11:03
  • 1
    It returns things like `java_home`...but also things like `my_db_pwd` or (better) `my_app_environment...` ;) ..these you'd *have to set* on the according env./pass as cmd arguments/...(e.g. `java ... MyApp -Dmy_env=prod`;) – xerx593 Oct 16 '22 at 11:04
  • Is there any documentation or course that I can consult to learn how to work with the environments? When I search for "java environment variables" the only thing I find is how to install JAVA_HOME in the – vktop Oct 16 '22 at 11:11
  • ..without spring or maven (plain (ole) java), you'd (mimic "spring profiles"): introduce (multiple/hirarchical) `my_domain[_env].properties` (google: "java.util.properties") ..some (custom) piece of code would "load" you these (regarding structure/according to `env`), and make it available to "your app". – xerx593 Oct 16 '22 at 11:13
  • Then "your app" could do (environment independently) things like: `myProps.get("my_domain_custom_config_password_etc");` (which would refer to some entry(key!) of your *deployed* `my_domain[_env].properties` files..) – xerx593 Oct 16 '22 at 11:16
  • But for "few properties", just ensure: to set them correctly (on the target environment) and read them correctly. :-) – xerx593 Oct 16 '22 at 11:20
  • 1
    thanks, i will try to find more info on what you said. – vktop Oct 16 '22 at 11:21
  • Also note (subtle) difference between `System.getenv()` (these refers to "OS (env) vars") and `System.getProperties()` (which is a "JVM Thing"..and can be set via cmd/programmatically) – xerx593 Oct 16 '22 at 11:30

0 Answers0