1

In Java I can set and get a system property

System.setProperty("key","value");
String value = System.getProperty("key"); 

and the property I guess is set at the Java process level, but I need a property set at a higher level which is a bash process which runs some Java tests I developed, and I can't use a Java system property because the value won't persist across my test runs.

Is there a way to set a system property at the bash level that runs my tests that the children Java processes can access. Like for example I know that I can get the user name on a Linux machine using Java.

String userName = System.getProperty("user.name");

So I wonder if there is a higher level way of setting my own property at least at the Bash level that runs my Java tests.

edit

Basically I want a variable ranAtLeastOneTest then set this variable to true after the first test is run, then allow the other tests to access this variables to know if at least one test was run or not.

the_prole
  • 6,481
  • 11
  • 56
  • 120
  • why would it be modified across tests ? – gogaz Jan 14 '19 at 19:12
  • I guess i could just set a environment variables and grab it with java https://stackoverflow.com/questions/7054972/java-system-properties-and-environment-variables – the_prole Jan 14 '19 at 19:13
  • You can do that in the command with switch -Dkey=value – ygbgames Jan 14 '19 at 19:13
  • right, but i need a dynamic property which can be changed by the java process that can be used by other java processes, or changed at the bash layer and shared with the Java children – the_prole Jan 14 '19 at 19:14

2 Answers2

0

Environment variables can only be set for the own process and its child processes. There is no way, that a child process is able to change a environment variable and this change would be visible to the parent process.

If you want to change something in the bash script based on something happening in your started java processes, you have two ways:

  • The java process exits with a specific exit code. E.g 0 (zero) means that at least one test was run and 1 means no tests were run. The bash script could act on the exit code.

  • The bash script monitors the output written by the java tests. (e.g. output from java is written to file and bash script checks output afterwards (grep etc).

If the bash script detects some special condition in one of that ways, it can change a environment variable or use another -Dxxx=yyy parameter when starting the next java process.

Tanktalus
  • 20,069
  • 4
  • 37
  • 65
Ralf
  • 1,493
  • 5
  • 14
  • If the Bash process changes the value of an environment variable after the Java process is run, would the Java process have access to the new env variable value, or does the Java process only have access to snap shot of the values before the Java proc is run? Because it seems to me that Java only has access to the "snap shot" values according to my code, although I could be wrong. – the_prole Jan 14 '19 at 23:21
0

Looks like I can also set an environment variable in my bash process and access it from the Java child process.

Java system properties and environment variables

the_prole
  • 6,481
  • 11
  • 56
  • 120