-1

I have two projects. One at location a and other at b.

Project a needs to load the conf file from project b's directory. I can't hardcode the path for file which is located in b's directory because If I run this on different machine everymachine's username will be different and path can be different as well.

So I want to do is, everyone sets their conf file path as environment varibale in .bashrc. That way I want to use exported variable path in my java code. Now the question is how do I do that?


Following is something similar to what I want to do:

I have one varibale stored in my system at .bashrc as follows:

export variableName1=~/path_to_file

Now I want to use this variable in java while loading the config file as follows:

        Config config = ConfigFactory.parseFile(new File("${variableName1}/path_to_file"));

How do I do that?

I tried getting the value of variableName1 as follows:

        String value =  System.getProperty("variableName1");
                          OR
        String value =  System.getenv("variableName1");

But this returns null value only.

C.k.
  • 93
  • 2
  • 13
  • Do you mean System.getProperty("")? – Vitaliy Moskalyuk Aug 24 '20 at 08:30
  • Is this all about variable usernames or is even the *home/* part different? – deHaar Aug 24 '20 at 08:31
  • Does this answer your question? [How can my Java code read OS environment variables?](https://stackoverflow.com/questions/461018/how-can-my-java-code-read-os-environment-variables) – Joe Aug 24 '20 at 08:38
  • I don't quite get what your problem is... Is it loading an environment variable or creating variable paths depending on the current user? Or both? – deHaar Aug 24 '20 at 08:39
  • loading the environment variable. Because whenever I do System.getenv("variableName1"); or System.getProperty("variableName1"); I get the null value – C.k. Aug 24 '20 at 08:40
  • @deHaar I have modified the question. Does it help to understand the problem? – C.k. Aug 24 '20 at 09:42
  • Yes, it helps... But unfortunately, I have no idea why your variable doesn't get read (apart from the obvious ones, like typos or access right issues. Which variables do you get when you read all (available) ones by `Map envs = System.getenv();`? `variableName1` is not in that map, is it? – deHaar Aug 24 '20 at 09:48
  • @deHaar correct. variableName1 is not in that map. – C.k. Aug 24 '20 at 10:18
  • How are you starting your application? Are you even starting it from a terminal? If not, then the environment variable might not have been set. – Mark Rotteveel Aug 24 '20 at 17:52

2 Answers2

0

That's an environment variable so is accessed with

String myDirectory = System.getenv("variableName1");
drekbour
  • 2,895
  • 18
  • 28
  • But this one is giving me null value :( – C.k. Aug 24 '20 at 08:39
  • You can print everything with `System.out.println(System.getenv());` If it's not in there then you have not even loaded the `.bashrc` file – drekbour Aug 24 '20 at 08:46
  • It's not [printing everything I have set in .bashrc but it seems to print only specific values. Any idea why does it behave that way? – C.k. Aug 24 '20 at 08:57
0

Step 1: You need to export the variable name in .bashrc file for accessing from all terminal.

export variableName1="~/path_to_file"

Then restart the computer.

Step 2:

In Java code you can get the value by:

 String value =  System.getenv("variableName1");
tuhin47
  • 5,172
  • 4
  • 19
  • 29