mac os java version from root is different
Asked Answered
L

4

6

after installing jdk7u5 for mac os I got different java-versions for "normal" user and root.

java -version gives me the 7.

sudo java -version gives me the 6.

I've looked up in the /System/Library/Frameworks/JavaVM.framework/Versions/ found that there are some links. After I resolved the links to the real Directory I tried it again with the whole path.

/System/Library/Frameworks/JavaVM.framework/Versions/A/Commands/java -version -> 7

sudo /System/Library/Frameworks/JavaVM.framework/Versions/A/Commands/java -version -> 6

Same with javac!

Hope you guys could help me!

Laux answered 5/7, 2012 at 13:0 Comment(5)
So what exactly is your question? Do you want to set the root user to Java 7?Loom
I want that the java for root is the same as for the normal user. In my case 'sudo java -version' should also be version 7Laux
Try setting your JAVA_HOME to the updated version of java.Loom
@Loom - this is not the way to do it on OSXRunnymede
@Mark - Ah well I was just throwing stuff out there.. I didn't actually know the answer, I don't really use mac's that often for dev stuff.Loom
R
5

Under OSX /usr/bin/java locates the java version to be used by looking at what you chose using /Applications/Utilities/Java Preferences.app. The files ths uses are also updated by the Oracle updater.

As you have to drag the versions manually to set Java 1.7 you will have only set this for the user you run it as ie not root. To set it for root I ran the app as sudo /Applications/Utilities/Java\ Preferences.app/Contents/MacOS/Java\ Preferences and moved 1.7 to the top.

As far as I can see the files the app updates are in ~/Library/Preferences/ByHost/com.apple.java.JavaPreferences.<hex number>.plist where <hex number> is machine related. Root's home directory is /var/root so I now have /var/root/Library/Preferences/ByHost/com.apple.java.JavaPreferences.<hex number>.plist

Runnymede answered 5/7, 2012 at 13:34 Comment(1)
Just hope that noting running as rot needs 1.6 - but I very much doubt Apple would use JavaRunnymede
C
0

Caught the same issue on Mac OS X 10.7.5 + JRE 1.6.0_43 + JDK 1.7.0_17.

  • java -version => 1.7
  • sudo java -version => 1.6

Open java control panel with sudo, this fixes the problem:

  • sudo /usr/bin/javaws -viewer
Castigate answered 21/3, 2013 at 21:2 Comment(2)
Can you explain a little more about what to do ar the java viewer then?Mafia
Java Web Start (JWS) was deprecated in Java 9, and starting with Java 11, Oracle removed JWS from distributions. thus javaws is no longer a solution. consider to use brew install temurin11 or brew install temurin17Castigate
M
0

For me non of the answers above mentioned worked. In MacOS sudo may clear environment values so that is why your JAVA_HOME is different in sudo and without.

One solution that worked for me is to go to /etc/sudoers file and check env_reset or env_keep property.

You have the description of each property here:

env_reset   If set, sudo will reset the environment to only contain the following variables: HOME, LOGNAME, PATH, SHELL, TERM, and USER (in addi-
           tion to the SUDO_* variables).  Of these, only TERM is copied unaltered from the old environment.  The other variables are set to
           default values (possibly modified by the value of the set_logname option).  If sudo was compiled with the SECURE_PATH option, its value
           will be used for the PATH environment variable.  Other variables may be preserved with the env_keep option.

env_keep    Environment variables to be preserved in the user's environment when the env_reset option is in effect.  This allows fine-grained con-
           trol over the environment sudo-spawned processes will receive.  The argument may be a double-quoted, space-separated list or a single
           value without double-quotes.  The list can be replaced, added to, deleted from, or disabled by using the =, +=, -=, and ! operators
           respectively.  This list has no default members.

So for keep the JAVA_HOME and tell sudo not change it ( and take the value by default or setted in your /.bash_profile) you should add a value for env_keep as follow

Defaults   env_keep += "JAVA_HOME"
Mafia answered 1/12, 2022 at 13:7 Comment(0)
C
-1

It appears that the java version used by the link you specified depends on the JAVA_VERSION environment variable. Could it be that this variable is set for your user, but will be cleared by sudo? In that case, explicitely unsetting it for the user or setting it for root should help to keep them in sync. Of course, you might also replace the /A/ by the actual version you want to execute.

Cheboksary answered 5/7, 2012 at 13:9 Comment(1)
There is no JAVA_VERSION set. which java --> /usr/bin/java sudo which java --> /usr/bin/java, /usr/bin/java is a link to /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/javaLaux

© 2022 - 2025 — McMap. All rights reserved.