Apple's latest Java update removes Java preferences, how do I change the shell?

Apple's Java update this week removes Java Preferences.app from Utilities. When working between different versions of Java in different development environments, it no longer seems easily achievable to migrate from Java 6 to Sun 1.7, as before.

I believe that there must still be a shell mechanism to accomplish this, however I do not easily find it. How do you PROPERLY change the current active version of Java during a shell session?

(I say PROPERLY because I want to be sure that all env vars are set appropriately, such as JAVA_HOME, its path, etc.)

In addition, I thought that a couple of years ago I came across a script that would change Java env for you by simply specifying the parameters and then determining which one to switch to. I might be confused about the tool in Ubuntu, but I'm pretty sure it was for OS X ... It would be helpful to know what it was or maybe we need to write a quick script if one does not exist, which will satisfy this need ; perhaps inserting it into Homebrew?

+4
source share
2 answers

Thanks to @MadProgrammer who originally answered this question. I studied things a bit and found several examples that helped me with the following script, which I am currently using to run Java applications that will not currently work with Java 1.7:

#!/usr/bin/env bash # Or plug in `zsh` instead of `bash` above if that is your preferred shell DIR=$(pwd) cd "$DIR" APPLICATION_NAME="App Name" echo -n "Locating Java ... " PLATFORM=$(uname) JAVA_PATH="java" JAVA_VER=$($JAVA_PATH -version 2>&1 | sed 's/java version "\(.*\)\.\(.*\)\..*"/\1\2/; 1q') if [[ "$JAVA_VER" == *"command not found"* ]]; then echo "Java not found." else JAVA_VER_FULL=$($JAVA_PATH -version 2>&1 | awk -F '"' '/version/ {print $2}') if [[ $JAVA_VER -eq 17 ]]; then echo "v1.7 found." if [[ $PLATFORM -eq "Darwin" ]]; then echo "[!!!] WARNING: Java v1.7 on OS X is known to have problems with ${APPLICATION_NAME}." echo -n "Looking for compatible version of Java ... " JAVA_PATH="/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home/bin/java" if [[ -f "$JAVA_PATH" ]]; then JAVA_VER=$($JAVA_PATH -version 2>&1 | sed 's/java version "\(.*\)\.\(.*\)\..*"/\1\2/; 1q') JAVA_VER_FULL=$($JAVA_PATH -version 2>&1 | awk -F '"' '/version/ {print $2}') if [[ $JAVA_VER -eq 16 ]]; then echo "$JAVA_VER_FULL found. Using this version." else echo "No known compatible versions of Java could be found. ${APPLICATION_NAME} may not run properly, or at all." fi fi else echo "$JAVA_VER_FULL found." fi else echo "$JAVA_VER_FULL found." fi fi echo "" echo "Starting ${APPLICATION_NAME}..." # Insert the java application or jar that you wish to execute here prepended by `$JAVA_PATH` instead of simply `java` # For example: # # $JAVA_PATH -jar /path/to/the/application.jar 

One potentially useful situation for this is to create a script to launch Minecraft that is having problems with the release of version 1.7. Obviously, there are other similar applications that have various problems, and it will also be useful for controlling the version of Java you want to use, and not just using the default method or trying to crack symbolic links.

0
source

Firstly, thanks for the question, I needed to solve this myself.

This is what I found out.

  • whereis java points to /usr/bin/java
  • /usr/bin/java is a symbolic link to /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/java
  • On my Mac /System/Library/Frameworks/JavaVM.framework/Versions/Current is a symbolic link to /System/Library/Frameworks/JavaVM.framework/Versions/A , which is currently Java 1.7.0._07-b10

We need to redirect Current to the JVM we want to use.

On my system, Java 1.6.0_37 was installed in /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK , which is a symbolic link to /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents

What I did was ...

Backup Current

 sudo mv Current Current.bak 

Make a new symlink to CurrentJDK

 sudo ln -s CurrentJDK Current 

Then on the command line I did java -version and got

 Java(TM) SE Runtime Environment (build 1.6.0_37-b06-434-11M3909) Java HotSpot(TM) 64-Bit Server VM (build 20.12-b01-434, mixed mode) 

Now, to move it back, I just moved Current

 sudo mv Current Current.1.6 

Then dragged Current to A

 sudo ln -s A Current 

Then on the command line I did java -version and got

 java version "1.7.0_07" Java(TM) SE Runtime Environment (build 1.7.0_07-b10) Java HotSpot(TM) 64-Bit Server VM (build 23.3-b01, mixed mode) 

Now I only need sudo mv Current Current.1.7 and sudo mv Current.1.6 Current to go back and cancel it in order to move forward.

This decision was inspired by https://discussions.apple.com/thread/4444438?start=0&tstart=0 , but I had to play with it to make sure;)

UPDATED

After some game, I created a java6 that lives in /usr/bin/java6 and points to /System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Commands/java , this basically allows me to execute java6 from command line without having to switch versions

+2
source

Source: https://habr.com/ru/post/1441152/


All Articles