I would like to delete the 'CATALINA_HOME' using java code.
Sorry, but you can't. A program (at least on Linux / Unix / Solaris) cannot modify the environment of its parent shell. At most, a program only sees a snapshot of its parent's environment variables ... as they were when the parent fork
s the child process.
(Windows variables work a bit differently, but I think the same restriction applies ...)
The only thing that a Java can do (reliably / portably) is to change the environment variables that a child process of your Java process will inherit. You can do that using ProcessBuilder
.
Now in theory, a program could attempt to modify the source of the variables' values:
On a Unix like system, they often come from one of the shell's RC files; e.g. $HOME/.profile
, $HOME/.bashrc
and so on ... depending on the shell. Or the variable may have been set in the "init" script that launches Tomcat.
On a Windows system, the may have come from the registry, or a BAT file or ...
The problem is two-fold:
It is impossible to be entirely sure where the value really comes from.
Even if you can be sure, changing the source of the value won't affect the respective shell's current value for the variable.
Note: this is not a shortcoming of Java. You would have the same problem when coding in C, C++, Python ...