IT

모든 사용자를 위해 Linux에서 JAVA_HOME을 설정하는 방법

lottoking 2020. 6. 19. 07:57
반응형

모든 사용자를 위해 Linux에서 JAVA_HOME을 설정하는 방법


Linux 시스템을 처음 사용하는데 Java 폴더가 너무 많은 것 같습니다.

java -version은 다음을 제공합니다.

  • 자바 버전 "1.7.0_55"
  • OpenJDK 런타임 환경 (rhel-2.4.7.1.el6_5-x86_64 u55-b13)
  • OpenJDK 64 비트 서버 VM (빌드 24.51-b03, 혼합 모드)

Maven 프로젝트를 만들려고 할 때 오류가 발생합니다.

Error: JAVA_HOME is not defined correctly.
We cannot execute /usr/java/jdk1.7.0_05/bin/java

루트가 아닌 사용자뿐만 아니라 루트에 대해 수정해야 할 파일과 Java가 정확히 어디에 있는지 알려주십시오.


  1. find /usr/lib/jvm/java-1.x.x-openjdk
  2. vim /etc/profile

    권한이없는 사용자로 로그인 한 경우 sudo를 추가하십시오. sudo vim

  3. 삽입 모드에 들어가려면 'i'를 누르십시오
  4. 더하다:

    export JAVA_HOME="path that you found"
    
    export PATH=$JAVA_HOME/bin:$PATH
    
  5. 로그 아웃 한 후 다시 로그인하거나 재부팅하거나 source /etc/profile현재 셸에서 즉시 변경 사항을 적용 하는 사용

모든 사용자에게 다음 줄을 넣는 것이 좋습니다. /etc/profile

export JAVA_HOME=$(readlink -f /usr/bin/javac | sed "s:/bin/javac::")

이것은 동적으로 업데이트되며 대체 시스템 과 잘 작동 합니다. 업데이트는 새 로그인 셸에서만 수행됩니다.


/ etc / profile을 사용하거나 /etc/profile.d/jdk_home.sh와 같은 파일을 더 잘 사용할 수 있습니다

export JAVA_HOME=/usr/java/jdk1.7.0_05/

이 파일은 새로운 로그인 쉘에만로드된다는 것을 기억해야합니다. 따라서 bash -l 또는 새로운 gnome-session 후에는 새로운 Java 버전으로 변경되지 않습니다.


RHEL 7에서는 다른 대답들 중 어느 것도 나를 위해 "고착" 하지 않았 으며 심지어 설정 JAVA_HOME하고 PATH직접 /etc/profile또는 ~/.bash_profile작동하지 않을 수도 있습니다. JAVA_HOME설정 되어 있는지 확인하려고 할 때마다 비어 있습니다.

$ echo $JAVA_HOME
    (<-- no output)

내가해야 할 일은 스크립트설정하는 것 입니다 /etc/profile.d/jdk_home.sh:

#!/bin/sh
export JAVA_HOME=/opt/ibm/java-x86_64-60/
export PATH=$JAVA_HOME/bin:$PATH

처음에는 첫 번째 줄 ( #!/bin/sh)을 무시했으며 그 줄 이 없으면 작동하지 않습니다.

이제 작동합니다.

$ echo $JAVA_HOME
/opt/ibm/java-x86_64-60/

Linux에서 경로를 설정하는 것은 매우 쉽습니다. 다음과 같이하십시오 :

1 단계 개방형 터미널 및 유형sudo gedit .bashrc

2 단계

비밀번호를 묻습니다. 암호를 입력하면 bash 파일이 열립니다. 그런 다음 아래로 이동하여 아래에 입력하십시오.

3 단계

   export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/"
   export PATH=$PATH:$JAVA_HOME/bin

4 단계 그런 다음 파일을 저장하고 파일을 종료하십시오

위는 단일 사용자를위한 것입니다. 모든 사용자에 대해 아래 단계를 수행해야합니다

1 단계 gedit /etc/profile

2 단계 export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64/"

3 단계 export PATH=$PATH:$JAVA_HOME/bin

도움이 되었기를 바랍니다. 감사!


오라클의 업무 수행 (전직 Sun 직원 으로서는 익숙하지 않음)

ln -s latestJavaRelease / usr / java / default
여기서 latestJavaRelease는 사용하려는 버전입니다.

그런 다음 JAVA_HOME = / usr / java / default를 내보내십시오.


답변은 이전 게시물이 유효합니다. 그러나 다음과 관련하여 하나의 답변이 완전하지는 않습니다.

  1. Changing the /etc/profile is not recommended simply because of the reason (as stated in /etc/profile):
  • It's NOT a good idea to change this file unless you know what you are doing. It's much better to create a custom.sh shell script in /etc/profile.d/ to make custom changes to your environment, as this will prevent the need for merging in future updates.*
  1. So as stated above create /etc/profile.d/custom.sh file for custom changes.

  2. Now, to always keep updated with newer versions of Java being installed, never put the absolute path, instead use:

#if making jdk as java home

export JAVA_HOME=$(readlink -f /usr/bin/javac | sed "s:/bin/javac::")

OR

#if making jre as java home

export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:/bin/java::")

  1. And remember to have #! /bin/bash on the custom.sh file

Copy the bin file path you installed

YOUR PATH

open terminal and edit environment file by typing following command,

sudo nano /etc/environment

In this file, add the following line (replacing YOUR_PATH by the just copied path):

JAVA_HOME="YOUR_PATH"

That should be enough to set the environment variable. Now reload this file:

source /etc/environment

now test it by executing:

echo $JAVA_HOME

On Linux I add this line to my ~/.profile:

export JAVA_HOME=$(readlink -ze /usr/bin/javac | xargs -0 dirname -z | xargs -0 dirname)

Probably a good idea to source whatever profile you edit to save having to use a fresh login.

either: source /etc/ or . /etc/

Where is whatever profile you edited.


While we are up to setting JAVA_HOME, let me share some benefits of setting JAVA_HOME or any other environment variable:

1) It's easy to upgrade JDK without affecting your application startup and config file which points to JAVA_HOME. you just need to download new version and make sure your JAVA_HOME points to new version of Java. This is best benefit of using environment variable or links.

2) JAVA_HOME variable is short and concise instead of full path to JDK installation directory.

3) JAVA_HOME variable is platform independence i.e. if your startup script uses JAVA_HOME then it can run on Windows and UNIX without any modification, you just need to set JAVA_HOME on respective operating system.

Read more: http://javarevisited.blogspot.com/2012/02/how-to-set-javahome-environment-in.html#ixzz4BWmaYIjH


This is a very simple script to solve the problem

export JAVA_HOME_BIN=`which java`
export JAVA_HOME_DIR=`dirname $JAVA_HOME_BIN`
export JAVA_HOME=`dirname $JAVA_HOME_DIR`

And for testing:

echo $JAVA_HOME

Posting as answer, as I don't have the privilege to comment.

Point to note: follow the accepted answer posted by "That Dave Guy".

After setting the variables, make sure you set the appropriate permissions to the java directory where it's installed.

chmod -R 755 /usr/java

Step 1 - check the current java version by "echo $JAVA_HOME"

Step 2 - vim /etc/profile

Step 3 - At the end of file you will find export JAVA_HOME, we need to provide the new path here, make sure that it is not relative.

Step 4 - Save and exit :wq

Step 5 - "source /etc/profile/", this would execute the change

Step 6 - Again do a echo $JAVA_HOME - change would have been reflected.


Just add this command to your dockerfile

RUN echo "JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")" | tee -a /etc/profile && source /etc/profile && echo $JAVA_HOME

1...Using the short cut Ctlr + Alt + T to open terminal

2...Execute the below command:

echo export JAVA_HOME='$(readlink -f /usr/bin/javac | sed "s:/bin/javac::")' | sudo tee /etc/profile.d/jdk_home.sh > /dev/null

3...(Recommended) Restart your VM / computer. You can use source /etc/source if don't want to restart computer

4...Using the short cut Ctlr + Alt + T to open terminal

5...Verified JAVA_HOME installment with

echo $JAVA_HOME

One-liner copy from flob, credit to them


I use the line:

export JAVA_HOME=$(readlink -f $(dirname $(readlink -f $(which java) ))/../)

to my ~/.profile so it uses the base of the default java directory at login time. This is for bash.

참고URL : https://stackoverflow.com/questions/24641536/how-to-set-java-home-in-linux-for-all-users

반응형