简体   繁体   中英

How to set a system property via Bash accessible by child Java processes

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.

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. Eg 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. (eg 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.

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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM