How can I change Mac OS's default Java VM returned from /usr/libexec/java_home

后端 未结 12 723
刺人心
刺人心 2020-12-04 04:42

(Wasn\'t sure if this should go on SU... migration is certainly an option, but more programmers read questions here, so here goes).

I am running Mac OS X 10.8.4, and

相关标签:
12条回答
  • 2020-12-04 05:22

    It's pretty simple, if you don't mind rolling up your sleeves... /Library/Java/Home is the default for JAVA_HOME, and it's just a link that points to one of:

    • /System/Library/Java/JavaVirtualMachines/1.?.?.jdk/Contents/Home
    • /Library/Java/JavaVirtualMachines/jdk1.?.?_??.jdk/Contents/Home

    So I wanted to change my default JVM/JDK version without changing the contents of JAVA_HOME... /Library/Java/Home is the standard location for the current JVM/JDK and that's what I wanted to preserve... it seems to me to be the easiest way to change things with the least side effects.

    It's actually really simple. In order to change which version of java you see with java -version, all you have to do is some version of this:

    cd /Library/Java
    sudo rm Home
    sudo ln -s /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home ./Home
    

    I haven't taken the time but a very simple shell script that makes use of /usr/libexec/java_home and ln to repoint the above symlink should be stupid easy to create...

    Once you've changed where /Library/Java/Home is pointed... you get the correct result:

    cerebro:~ magneto$ java -version
    java version "1.8.0_60"
    Java(TM) SE Runtime Environment (build 1.8.0_60-b27) Java HotSpot(TM)
    64-Bit Server VM (build 25.60-b23, mixed mode)
    
    0 讨论(0)
  • 2020-12-04 05:22

    A bit late but as this is an ongoing issue with Mac OSX...

    The simplest solution I found was to simply remove the OpenJDK stuff that Apple installs. Every time an update of Mac OSX arrives it gets installed and you'll need to remove it again.

    This works really well if you develop apps for Google App Engine on your mac using Java. The OpenJDK does not work well and the Java version that comes with the Mac OSX Yosemite upgrade will make the Eclipse Plug-in for App Engine crash on every deployment with the helpful error: "Read timed out".

    0 讨论(0)
  • 2020-12-04 05:23

    I tested "jenv" and other things like setting "JAVA_HOME" without success. Now i and endet up with following solution

    function setJava {
        export JAVA_HOME="$(/usr/libexec/java_home -v $1)"
        launchctl setenv JAVA_HOME $JAVA_HOME
        sudo ln -nsf "$(dirname ${JAVA_HOME})/MacOS" /Library/Java/MacOS 
        java -version
    }
    

    (added to ~/.bashrc or ~/.bash.profile or ~/.zshrc)

    And calling like that:

    setJava 1.8
    

    java_home will handle the wrong input. so you can't do something wrong. Maven and other stuff will pick up the right version now.

    0 讨论(0)
  • 2020-12-04 05:26

    I've been there too and searched everywhere how /usr/libexec/java_home works but I couldn't find any information on how it determines the available Java Virtual Machines it lists.

    I've experimented a bit and I think it simply executes a ls /Library/Java/JavaVirtualMachines and then inspects the ./<version>/Contents/Info.plist of all runtimes it finds there.

    It then sorts them descending by the key JVMVersion contained in the Info.plist and by default it uses the first entry as its default JVM.

    I think the only thing we might do is to change the plist: sudo vi /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Info.plist and then modify the JVMVersion from 1.8.0 to something else that makes it sort it to the bottom instead of the top, like !1.8.0.

    Something like:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">
    <dict>
        ...
        <dict>
                ...
                <key>JVMVersion</key>
                <string>!1.8.0</string>   <!-- changed from '1.8.0' to '!1.8.0' -->`
    

    and then it magically disappears from the top of the list:

    /usr/libexec/java_home -verbose
    Matching Java Virtual Machines (3):
        1.7.0_45, x86_64:   "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home
        1.7.0_09, x86_64:   "Java SE 7" /Library/Java/JavaVirtualMachines/jdk1.7.0_09.jdk/Contents/Home
        !1.8.0, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home
    

    Now you will need to logout/login and then:

    java -version
    java version "1.7.0_45"
    

    :-)

    Of course I have no idea if something else breaks now or if the 1.8.0-ea version of java still works correctly.

    You probably should not do any of this but instead simply deinstall 1.8.0.

    However so far this has worked for me.

    0 讨论(0)
  • 2020-12-04 05:26

    I had a similar situation, and the following process worked for me:

    1. In the terminal, type

      vi ~/.profile
      
    2. Then add this line in the file, and save

      export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk<version>.jdk/Contents/Home
      

      where version is the one on your computer, such as 1.7.0_25

    3. Exit the editor, then type the following command make it become effective

      source ~/.profile 
      

    Then type java -version to check the result

        java -version 
    

    What is .profile? From:http://computers.tutsplus.com/tutorials/speed-up-your-terminal-workflow-with-command-aliases-and-profile--mac-30515

    .profile file is a hidden file. It is an optional file which tells the system which commands to run when the user whose profile file it is logs in. For example, if my username is bruno and there is a .profile file in /Users/bruno/, all of its contents will be executed during the log-in procedure.

    0 讨论(0)
  • 2020-12-04 05:27

    I think JAVA_HOME is the best you can do. The command-line tools like java and javac will respect that environment variable, you can use /usr/libexec/java_home -v '1.7*' to give you a suitable value to put into JAVA_HOME in order to make command line tools use Java 7.

    export JAVA_HOME="`/usr/libexec/java_home -v '1.7*'`"
    

    But standard double-clickable application bundles don't use JDKs installed under /Library/Java at all. Old-style .app bundles using Apple's JavaApplicationStub will use Apple Java 6 from /System/Library/Frameworks, and new-style ones built with AppBundler without a bundled JRE will use the "public" JRE in /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home - that's hard-coded in the stub code and can't be changed, and you can't have two different public JREs installed at the same time.


    Edit: I've had a look at VisualVM specifically, assuming you're using the "application bundle" version from the download page, and this particular app is not an AppBundler application, instead its main executable is a shell script that calls a number of other shell scripts and reads various configuration files. It defaults to picking the newest JDK from /Library/Java as long as that is 7u10 or later, or uses Java 6 if your Java 7 installation is update 9 or earlier. But unravelling the logic in the shell scripts it looks to me like you can specify a particular JDK using a configuration file.

    Create a text file ~/Library/Application Support/VisualVM/1.3.6/etc/visualvm.conf (replace 1.3.6 with whatever version of VisualVM you're using) containing the line

    visualvm_jdkhome="`/usr/libexec/java_home -v '1.7*'`"
    

    and this will force it to choose Java 7 instead of 8.

    0 讨论(0)
提交回复
热议问题