3 ms·
Maybe among the best decisions Java ever made was hiding setenv. You simply cannot set env vars in Java.
by jeffbee 2y ago
Maybe among the best decisions Java ever made was hiding setenv. You simply cannot set env vars in Java.
- mschuster91 2y agoJava in general tends to massively dislike environment variables. Which is a Good Thing, but unfortunately it makes dealing with proxy support so much harder - especially as there are just so damn many HTTP libraries that people use...
- aidenn0 2y agoSo does that also mean you can't write e.g. a POSIX shell in Java?
- shakna 2y agoSystem.getenv exists, so getting is easy... But setting env vars generally involves a convoluted process. You can modify the JVM's buffer, or spawn a new ProcessBuilder, or a few other things. But it's a nasty place to be.
- bhawks 2y ago1) java shell starts 2) read getenv into a java hashmap 3) evaluate shell input - any sets update the hashmap, any gets read from the hashmap 4) when you get to a point where you need to exec a process you use the hashmap to pass envvars into process builder - which is going to call execvpe for you. So yeah you definitely could (there'd be other reasons why java wouldn't be a good choice). TBH reading that folks were calling setenv before an exec to propagate env vars to a child made me sad. I would guess that other use cases were leveraging environ as a poor man's global variable - which is also unfortunate.
- asveikau 2y agoexecve(2) and friends allow you to specify a new processes's environment variables. How Java may choose to expose that I don't know. Assuming they do, that is all a shell needs. You do not strictly need to call setenv(3) to change a variable that gets evaluated with $, etc. You do need to pass the modified environment to a child process.
- lmm 2y agoYou can pass a new environment to a process you spawn, which I think is adequate? You just can't modify the environment of your existing process.
- sunshowers 2y agoThere are tons of reasons you can't write a POSIX shell in Java, starting from the lack of support for non-UTF-8 paths :)
- NotACracker 2y agoI admire your courage to say something positive about java. On another note, I believe Zig does the same thing.