Меню Рубрики

Mac os java home

How to set JAVA_HOME in Mac permanently?

I am trying to set JAVA_HOME by entering export JAVA_HOME=/Library/Java/Home at terminal. It sets the JAVA_HOME for current session.

How can I set it permanently?

13 Answers 13

You can use /usr/libexec/java_home -v to get the path you need for JAVA_HOME . For instance, to get the path to the 1.7 JDK you can run /usr/libexec/java_home -v 1.7 and it will return the path to the JDK. In your .profile or .bash_profile just add

and you should be good. Alternatively, try and convince the maintainers of java tools you use to make use of this method to get the version they need.

To open ‘.bash_profile’ type the following in terminal :

and add the following line to the file:

Press CTRL+X to exit the bash. Press ‘Y’ to save changes.

To check whether the path has been added, type following in terminal:

I was facing the same issue in MAC Catalina, If I edit .bash_profile i found export JAVA_HOME=»/Library/Java/JavaVirtualMachines/jdk1.8.0_201.jdk/Contents/Home But When I run terminal echo $JAVA_HOME it was returning empty, Later I found that the file .zshrc was missing I created this file with

Then edit it by nano .zshrc and wrote

Which solves my issue permanently

To set your Java path on mac:

    Open terminal on mac, change path to the root cd

Click I to insert text and use the following text to set JAVA_HOME and PATH

  1. Type :wq to save and exit the file.
  2. Type source .bash_profile to execute the .bash_profile file.
  3. You can type echo $JAVA_HOME or echo $PATH

/.bash_profile is not a default file; most won’t have it, so you can’t just start editing it. And you can’t just create it because it has syntax; it needs to be a full script file. – ktappe Sep 18 at 6:13

Besides the settings for bash/ zsh terminal which are well covered by the other answers, if you want a permanent system environment variable for terminal + GUI applications (works for macOS Sierra; should work for El Capitan too):

(this will set JAVA_HOME to the latest 1.8 JDK, chances are you have gone through serveral updates e.g. javac 1.8.0_101, javac 1.8.0_131)

Of course, change 1.8 to 1.7 or 1.6 (really?) to suit your need and your system

Also, you can put the environment variable in one of these files:

This explains correctly, I did the following to make it work

  1. Open Terminal
  2. Type vim .bash_profile
  3. Type your java instalation dir in my case export JAVA_HOME=»/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
  4. Click ESC then type :wq (save and quit in vim)
  5. Then type source .bash_profile
  6. echo $JAVA_HOME if you see the path you are all set.

1) The first step is to if you have Java installed and running your system.

Usually, it should be /usr/bin/java.

2) JAVA_HOME is essentially the full path of the directory that contains a sub-directory named bin which in turn contains the java.

  • You should have bin folder inside /Java folder and in turn it must contain the [java] executable package.

3.1) If you want to set the path only for the current session then execute this command in your terminal export JAVA_HOME=/Library/Java

3.2) If you want it to persist, you will have to add the command to your

    Open up Terminal vi

/.bash_profile

  • Add these lines to terminal export JAVA_HOME=/Library/Java/Home and save it
  • Execute below command source

    4) Verify you have correctly added JAVA_HOME path. Below command should give you the proper Java version.

    Источник

    How to Set $JAVA_HOME environment variable on Mac OS X

    In this tutorial, we show you how to set $JAVA_HOME environment variable on latest or older Mac OSX.

    1. Mac OSX 10.5 or later

    In Mac OSX 10.5 or later, Apple recommends to set the $JAVA_HOME variable to /usr/libexec/java_home , just export $JAVA_HOME in file

    Why /usr/libexec/java_home?
    This java_home can return the Java version specified in Java Preferences for the current user. For examples,

    This Mac OSX has three JDK installed.

    2. Older Mac OSX

    For older Mac OSX, the /usr/libexec/java_home doesn’t exists, so, you should set JAVA_HOME to the fixed path :

    References

    Related Articles

    mkyong

    hi all – i tried the above sequence of steps – below output:

    mhasse$ $ vim .bash_profile

    -bash: $: command not found

    mhasse$ export JAVA_HOME=$(/usr/libexec/java_home)

    -bash: /usr/libexec/java_home: No such file or directory

    mhasse$ $ echo $JAVA_HOME

    -bash: $: command not found

    Your site is one of the best out there for development. Thanks for what you provide.

    Thanks a lot. Always find your site useful.

    Doesn’t work. Well it does but it sets JAVA_HOME only for specific terminal. When I open new tab in the terminal and write mvn –version it still shows 1.6 and echo $JAVA_HOME doesn’t show anything at all.

    Make sure you are saving this in your bash profile. if you just type it into your terminal, it will not persist after you close the terminal.

    I have a similar problem – I typed this into my

    /.bash_profile, but using the source command doesn’t persist in another shell. When I use $cat

    /.bash_profile it prints out the correct contents.

    Did you try restarting Terminal altogether? Sometimes there is some weird funniness with session persistence. I always restart Terminal after editing .bash_profile just in case.

    You have to type the command: source

    /.bash_profile, everytime you open a new terminal, for the path to be set in that session.

    Thanks it helped me and saved my time.

    Thank you! very helpful.

    Thanks a lot for these clear, precise and straightforward explanations.

    Thank you! Another post on your blog that has helped me a lot.

    Thanks dude, it saved me too much time and effort.

    Only your solution worked for me after unsuccessfully digging around the Internet for some time.
    Thanks a lot.

    Works like a charm.. thanks a lot.. have been following your blog for quite sometime… kudos on your spot on and precise solutions…

    I need a favorite here please

    Virus:bin root# java -version

    java version “1.8.0_25”

    Java(TM) SE Runtime Environment (build 1.8.0_25-b17)

    Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

    Virus:bin root# whereis java|xargs ls -ltr

    lrwxr-xr-x 1 root wheel 73 Nov 25 14:35 /usr/bin/java -> /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java

    This is excellent and to the point. Thanks so much!

    This page has helped me numerous time. thanks mkyong

    […] de la variable de entorno JAVA_HOME se puede hacer siguiendo los consejos de esta pГЎgina:В http://www.mkyong.com/java/how-to-set-java_home-environment-variable-on-mac-os-x/В y la realizaremos a travГ©s de uno de los ficheros .sh que alojaremos en profile.d de esta […]

    Very nice, helpful. Thanks.

    Thanks a lot, you are the best man!

    Hi colleagues, its enormous article about educationand entirely explained, keep it up
    all the time.

    Worked for me…Thanks

    Can anybody help me?, I just followed all the step in the post, but it doesn’t work to me.
    When I type: Echo $JAVA_HOME, only show me blank.
    Details:

    cat .bash_profile 22:42:12
    export JAVA_HOME=$(/usr/libexec/java_home)
    export M2_HOME=/Applications/apache-maven-3.6.1
    export PATH=$PATH:$M2_HOME/bin
    в‹Љ>

    echo $JAVA_HOME 22:42:16

    java -version 22:42:43
    java version “1.8.0_211”
    Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
    Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)
    в‹Љ>

    javac -version 22:42:48
    javac 1.8.0_211
    в‹Љ>

    source .bash_profile 22:42:51
    .bash_profile (line 1): $(…) is not supported. In fish, please use ‘(/usr/libexec/ja…)’.
    export JAVA_HOME=$(/usr/libexec/java_home)
    ^
    from sourcing file .bash_profile
    called on standard input

    source: Error while reading file ‘.bash_profile’

    Источник

    Where is JAVA_HOME on macOS Mojave (10.14) to Lion (10.7)?

    Java is an optional package on the latest versions of macOS.

    Yet once installed it appears like the JAVA_HOME environment variable is not set properly.

    26 Answers 26

    With the Java optional package or Oracle JDK installed, adding one of the following lines to your

    /.bash_profile file will set the environment variable accordingly.

    Update: added -v flag based on Jilles van Gurp response.

    /usr/libexec/java_home is not a directory but an executable. It outputs the currently configured JAVA_HOME and doesn’t actually change it. That’s what the Java Preferences app is for, which in my case seems broken and doesn’t actually change the JVM correctly. It does list the 1.7 JVM but I can toggle/untoggle & drag and drop all I want there without actually changing the output of /usr/libexec/java_home .

    Even after installing 1.7.0 u6 from Oracle on Lion and setting it as the default in the preferences, it still returned the apple 1.6 java home. The only fix that actually works for me is setting JAVA_HOME manually:

    At least this way when run from the command line it will use 1.7. /usr/libexec/java_home still insists on 1.6.

    Update: Understanding Java From Command Line on OSX has a better explanation on how this works.

    is the way to do it. Note, updating this to 1.8 works just fine.

    For me, Mountain Lion 10.8.2, the solution most voted does not work. I installed jdk 1.7 via Oracle and maven from homebrew.

    My solution is from the hadoop-env.sh file of hadoop which I installed from homebrew, too. I add the below sentence in

    /.bash_profile, and it works.

    This solution also works for OS X Yosemite with Java 1.8 installed from Oracle.

    None of the above answers helped me. I suppose all the answers are for older OS X

    For OS X Yosemite 10.10, follow these steps

    Use your favorite text editor to open:

    Add the following line in the file and save it ( : followed by «x» for vim):

    Then in the terminal type the following two commands to see output:

    In the second line, you are updating the contents of .bash_profile file.

    Update for Java 9 and some neat aliases.

    Note, that for the latest version it is 9 and not 1.9 .

    Some additional alias to switch between the different versions:

    EDIT: Update for Java 10

    EDIT: Update for Java 11

    The above didn’t work for me with Amazon’s EC2 tools, because it expects bin/java etc. underneath JAVA_HOME . /System/Library/Frameworks/JavaVM.framework/Home did work.

    For OS X you can do:

    On Mac OS X Lion, to set visualgc to run, I used:

    The following worked for me. I’m using ZSH on OSX Yosemite with Java 8 installed.

    The following command /usr/libexec/java_home emits the path to JDK home:

    A better (more upgradable) way is to use the following:

    This should work with AWS also since it has bin underneath Home

    Newer Oracle JVMs such as 1.7.0_21-b12 seem to install here:

    for macOS Mojave 10.14.1 and JAVA 11.0.1 I set the profile as

    key in terminal this to confirm:

    This makes it very easy to switch between J6 and J7

    I Had to explicitly set it to the exact path on my Macbook air .

    1. try to echo $JAVA_HOME (if it’s set it’ll show the path), if not, try to search for it using sudo find /usr/ -name *jdk
    2. Edit the Bash p with — sudo nano

    /.bash_profile

  • Add the exact path to JAVA Home (with the path from step 2 above) export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home
  • Save and exit
  • Check JAVA_Home using — echo $JAVA_HOME
  • I am running MACOS MOJAVE — 10.14.2 (18C54) on a Macbook Air with JAVA 8

    For Mac OS X 10.9 I installed the latest version of JRE from Oracle and then reset the JAVA_HOME to /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home.

    I am sure there is a better way but got me up and running.

    hbrien$ echo $JAVA_HOME /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home

    OSX Yosemite, ZSH, and Java SE Runtime Environment 8, I had to:

    $ sudo ln -s /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands /System/Library/Frameworks/JavaVM.framework/Versions/Current/bin

    /.zshrc change JAVA_HOME to export JAVA_HOME=»/System/Library/Frameworks/JavaVM.framework/Versions/Current»

    Got the same issue after I upgrade my Mac OS and following worked for me:

    Add/update the line for JAVA_HOME: export JAVA_HOME=»/Library/Java/JavaVirtualMachines/jdk1.7.0_15.jdk/Contents/Home»

    /.bash_profile or open a new terminal

    I think the jdk version might differ, so just use the version which you have under /Library/Java/JavaVirtualMachines/

    If you are in need to have multiple versions of JDK under Mac OS X (Yosemite), it might be helpful to add some scripting for automated switching between them.

    What you do is to edit your

    /.bash_profile and add the following:

    What the script does is to first remove other JDK versions in the PATH so that they won’t interfere with our new JDK version. Then it makes some clever use of /usr/libexec/java_home which is a command that lists installed JDK versions. The -v argument tells java_home to return the path of the JDK with the supplied version, for example 1.7. We also update the PATH to point to the bin directory of the newly found JAVA_HOME directory. At the end we can simply execute the function using

    which selects the latest installed JDK version of the 1.7 branch. To select a specific version you can simply execute

    instead. Run /usr/libexec/java_home -V to get more details on how to choose versions.

    Источник

    Добавить комментарий

    Ваш адрес email не будет опубликован. Обязательные поля помечены *

  • Mac os java firefox
  • Mac os iscsi initiator
  • Mac os is not supported with binary translation vmware
  • Mac os iphone browser
  • Mac os ip самоназначен