How to set LD_LIBRARY_PATH/DYLD_LIBRARY_PATH on macos

旧城冷巷雨未停 提交于 2021-02-04 16:11:26

问题


On macos catalina '''echo $VARIABLE'''

I see the value of the variable but java couldn't read the system variable.

In linux there is not a problem so I think it is a zsh issue.

Java read all the variables env, except LD_LIBRARY_PATH and DYLD_LIBRARY_PATH


回答1:


Variables LD_LIBRARY_PATH / DYLD_LIBRARY_PATH are not passed to the environment of a child process on macOS if System Integrity Protect (SIP) is enabled.

To confirm :

#!/bin/zsh

cat << EOF > EnvDemo.java
public class EnvDemo {
   public static void main(String[] args) throws Exception {
      System.out.println(System.getenv("LD_LIBRARY_PATH"));
      System.out.println(System.getenv("DYLD_LIBRARY_PATH"));
      System.out.println(System.getenv("PATH"));
      System.out.println(System.getenv("CUSTOM_FLAG"));
   }
} 
EOF

javac EnvDemo.java
export LD_LIBRARY_PATH=/usr/local/library
export DYLD_LIBRARY_PATH=/usr/local/library
export CUSTOM_FLAG=custom_flag
java EnvDemo
# null
# null
# /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/oracle-19-5
# custom_flag

echo "env | grep LD_LIBRARY_PATH"
env | grep LD_LIBRARY_PATH
# No output here
echo "env | grep DYLD_LIBRARY_PATH"
env | grep DYLD_LIBRARY_PATH
# No output here



回答2:


I believe that any given env variable in a process is not necessarily copied to any processes spawned from it.

So, here, your zsh process clearly has the LD_LIBRARY_PATH env variable, but your java process does not.

If you set it like so:

LD_LIBRARY_PATH=/Applications/blabla

you'd get this behaviour. You're looking for:

export LD_LIBRARY_PATH=/Applications/blabla



来源:https://stackoverflow.com/questions/60126159/how-to-set-ld-library-path-dyld-library-path-on-macos

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!