- How to Set JAVA_HOME Path in Ubuntu 18.04 and 20.04 LTS
- Install OpenJDK on Ubuntu
- Install OpenJDK 8
- Install OpenJDK 11
- Install OpenJDK 14
- Set JAVA_HOME Path
- Add JAVA bin directory to the PATH variable
- Test JAVA setup
- Karim Buzdar
- How to set JAVA_HOME for Java?
- 3 Answers 3
- How to set JAVA_HOME and PATH in Linux? Example
- 1. Steps to set JAVA_HOME in Linux
- 2. Why should you set JAVA_HOME in Linux
- Ubuntu: where is java_home environment variable stored
- 4 Answers 4
- What is JAVA_HOME? How does the JVM find the javac path stored in JAVA_HOME?
- 9 Answers 9
How to Set JAVA_HOME Path in Ubuntu 18.04 and 20.04 LTS
A large number of Java-based programs require Java Runtime Environment (JRE) to run smoothly regardless of operating systems. For development purposes, most IDEs like Eclipse and NetBeans require Java Development Kit (JDK) installed on the machine. Whether you are a newbie developer looking forward to learning development or perhaps an application you have installed requires you to have Java installed on your system, we have you covered. Setting up of JAVA_HOME path is quite easy.
In this tutorial, we are going to learn how to install JDK on Ubuntu 18.04 and 20.04 and then set up the java home path. Let’s start by installing the JDK first.
Install OpenJDK on Ubuntu
Note: Make sure that you have updated the Advanced Package Tool (APT) before you move forward to install OpenJDK.
Press Ctrl + Alt + T to open the terminal and enter the following command mentioned in the box. You can either install OpenJDK 8 or the newer versions OpenJDK 11 or OpenJDK 14:
Install OpenJDK 8
Install OpenJDK 11
Install OpenJDK 14
You will be prompted to enter your sudo password to continue with the installation.
Once you’ve entered that, wait for the system to finish the installation and then move onto step 2.
Set JAVA_HOME Path
All you have to do now is to set the “JAVA_HOME” and “PATH” environment variables and then you are done. Enter the following commands to set your environment variables. Make sure that your environment variables point to a valid installation of JDK on your machine. For Ubuntu 18.04, the path is /usr/lib/jvm/java-8-openjdk-amd64/
To check whether your JAVA_HOME path has been successfully saved, enter the following command to check.
The value stored in the JAVA_HOME variable will be displayed on the terminal as you can see in the screenshot
Add JAVA bin directory to the PATH variable
Like we have added JAVA_HOME path, we will now update the PATH variable as well. To do that, enter the following command on the terminal.
This will append the java bin directory to the existing PATH variable. You can also check the PATH variable by entering the following command Advertisement
Test JAVA setup
You have successfully installed OpenJDK on your machine. You can verify your installation by entering the following command on your terminal.
Through this simple tutorial, you have now configured the JAVA_HOME variable in your operating system. You can now easily run java based applications as well as development environments on your machine. Let us know in comments if you faced any issues while implementing this tutorial.
Karim Buzdar
About the Author: Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. You can reach Karim on LinkedIn
Источник
How to set JAVA_HOME for Java?
I’ve installed a version of Java. How can we set the $JAVA_HOME environment variable correctly?
3 Answers 3
You can set your JAVA_HOME in /etc/profile as Petronilla Escarabajo suggests. But the preferred location for JAVA_HOME or any system variable is /etc/environment .
Open /etc/environment in any text editor like nano or gedit and add the following line:
(java path could be different)
Use source to load the variables, by running this command:
Then check the variable, by running this command:
Update
Usually most linux systems source /etc/environment by default. If your system doesn’t do that add the following line to
/.bashrc (Thanks @pje)
/.bashrc so that it loads all my environment settings on startup. Working for me so far.
To set JAVA_HOME environment variable, do the following:
- Launch Terminal by pressing Ctrl + Alt + T on your keyboard.
- Enter the following command:
$ gksudo gedit /etc/environment - Depending on where you installed your Java, you will need to provide the full path. For this example, I installed Oracle JDK 7 in the /usr/lib/jvm/java-7-oracle directory.
Scroll to the end of the file and enter the following:
JAVA_HOME=/usr/lib/jvm/java-7-oracle
export JAVA_HOME - Save your file and exit gedit.
- Lastly, reload the system PATH with the following command:
$ . /etc/environment
The above method will save you the hassle in having to run the commands every time you log in to your computer.
Источник
How to set JAVA_HOME and PATH in Linux? Example
Like in the previous article, I explained a Maven issue (invalid target release: 1.7, 1.8) related to the Java version, which occurred even though the correct version of Java was installed on the machine, but JAVA_HOME was pointing to an older version.
Unfortunately, When you install Java on Linux or Windows, this environment variable is not set automatically. You need to do it yourself. Java developers like me always set mandatory environment variables like JAVA_HOME, PATH, and CLASSPATH on the login scripts executed by the shell when you log in to Linux like bash_profile.sh , but you can also set JAVA_HOME into /etc/profile for all users.
I’ll show you steps on how to find the location of Java and how to set up the JAVA_HOME in Linux in this article, and these steps should also work with any Linux or Java version like Ubuntu, RedHat, CentOS, SUSE, ArchLinux, Debian, Fedora, etc.
Though I strongly encourage every Java developer to learn Linux, it’s a really important skill and serves you for a long time. If you decide to improve your Linux skill this year, I suggest you take a look at these best online Linux courses, which are both short and sweet and also explains all important details.
1. Steps to set JAVA_HOME in Linux
1) The first step is to find the location where Java is installed on your Linux machine. To check that open putty and connect to your Linux server and type the following command
where options include:
-d32 use a 32-bit data model if available
-d64 use a 64-bit data model if available
-server to select the «server» VM
It means Java is installed and added to your PATH, so half of the job is done already. All you need to is find the location of JDK, which you can find either by printing PATH as follows
You can see that JDK is installed on «/usr/sun/jdk/v1.6.0_16-64bit».
2) Add the following line of your .bash_profile file if you want to set JAVA_HOME for yourself or /etc/profile if you want to set JAVA_HOME for all user
That’s it you are done. Though don’t forget to open another shell window by typing bash , csh , or ksh . The new environment variable will not be added to an existing shell, you need to open a new shell window.
Also, if you use bash shell then adding into
/.bash_profile makes sense, if you use csh (C shell) then add JAVA_HOME into
/.csh_profile, and similarly for ksh (K shell) add JAVA_HOME into
They are hidden files in your home directory in Linux, that’s why they had prefix dot (.) in their name. You can use «ls — alrt» command to see them.
Here is a sample /etc/profile file with JAVA_HOME and PATH variable defined for your reference:
By the way, if you don’t see Java installed on your machine, then download the Linux version of Java from Oracle’s website and install it.
Once you are done with installation just find the folder where JDK is installed and follow the steps given above to set JAVA_HOME for your Linux environment.
For applications, JAVA_HOME is generally referred to on the start script. It’s bad practice to refer to the hardcoded path of Java installation in scripts, you will never find Tomcat, Maven, Eclipse, NetBeans, or IntelliJ doing that because then when you update to a new Java version they won’t be able to use it until you change their start script.
Using JAVA_HOME makes it easy to switch to a different or new Java version. See these free Java development courses to learn more about the core concepts of Java in detail. It’s updated for Java 11 and also not very expensive, I just bouthbought11 a couple of days ago.
2. Why should you set JAVA_HOME in Linux
Even though many developers know about JAVA_HOME, they don’t set it in their environment due to various reasons. I strongly recommend you to set JAVA_HOME , PATH , and CLASSPATH for your development environment. Here are some reasons why you set JAVA_HOME in Linux
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 a new version and make sure your JAVA_HOME points to the new version of Java. This is the best benefit of using an environment variable or links.
2) The JAVA_HOME variable is short and concise instead of the full path to the JDK installation directory.
3) JAVA_HOME variable helps your program achieve platform independence like 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 the respective operating system.
4) The JAVA_HOME is standard, which means other tools that need Java, like Maven, Eclipse can refer to this variable without having any knowledge of where Java is physically installed on your machine, which is obviously different in different user’s machines. JAVA_HOME allows everybody to access Java in a common and standard way.
I also suggest joining Java Fundamentals: The Java Language course on Pluaralsight to learn more about Java fundamentals like this one.
That’s all about how to set the JAVA_HOME environment variable in Linux. I have also touched base on why you should do it, which is probably more important. Many tools like Maven and IDEs like Eclipse and Netbeans use this variable to locate the JDK binaries installed on this machine, hence it’s better to set this up. If you face any problem while setting and using the JAVA_HOME environment variable, feel free to ask. I’ll be glad to help you out.
Источник
Ubuntu: where is java_home environment variable stored
I have installed java by these commands:
If I understood properly these commands above garantee that java will update automatically. I have been using Eclipse, STS and Tomcat successfully, then I believe that java is properly installed with its environments.
But I want to know where is the java_home environment. I tried:
gedit /etc/environment I found:
whereis java (I believe that this is just a file find)
/.pam_environment But it is completely empty
/.bashrc and after gedit .profile (I believe that this is just script files that run while starting linux). But I didn’t find any string with «java».
printenv JAVA_HOME (I believe that this only prints variables set by export and it is valid only for the exact session you set) But nothing has printed.
I am new to Linux. Could someone help me find where is the java environment in my system and please adjust any wrong believes I wrote before?
4 Answers 4
In my case after installing java from webupd8team ppa in Ubuntu 18.04 the scripts with setting and exporting JAVA_HOME variable are located in
When installing java with the webupd8team ppa you will need to set the JAVA_HOME environment variable yourself. You can do this in many ways. How I did it is as follows:
Run the sudo update-alternatives —config java command to get the list of java installations. It will also display the location of the java executable. e.g. /usr/lib/jvm/java-8-oracle/jre/bin/java .
From the java installation that you use select the path up to jre . e.g. /usr/lib/jvm/java-8-oracle/ . This will become your JAVA_HOME path.
/.bashrc file and add the following line: export JAVA_HOME=»/usr/lib/jvm/java-8-oracle/» .
Either restart your terminal, or type bash .
This should set the JAVA_HOME environment variable.
I don’t have an Ubuntu with Java right now, but I’ve done this in the past. http://www.janosgyerik.com/installing-java-and-setting-java_home-in-ubuntu/
Edit /etc/jvm , add this line to the top of the list: /path/to/your/jvm , which in your case is probably: /usr/lib/jvm/java-7-oracle but you have to check, maybe locate java-7-oracle` might help.
Thanks to the above step, the java executable will find the right version of Java. You can confirm this by running the command:
However, setting JAVA_HOME is a different matter. These commands worked in my older version of Ubuntu to detect + set + confirm JAVA_HOME :
If the output looks good, then add these lines to your
Источник
What is JAVA_HOME? How does the JVM find the javac path stored in JAVA_HOME?
I would like to know what is JAVA_HOME. Where do I set the path of javac.exe and java.exe. It is in environment variables. When I compile a Java program from command prompt, how does the JVM find javac.exe ?
9 Answers 9
JVM does not find java.exe . It doesn’t even call it. java.exe is called by the operating system (Windows in this case).
JAVA_HOME is just a convention, usually used by Tomcat, other Java EE app servers and build tools such as Gradle to find where Java lives.
The important thing from your point of view is that the Java /bin directory be on your PATH so Windows can find the .exe tools that ship with the JDK: javac.exe , java.exe , jar.exe , etc.
JAVA_HOME and JRE_HOME are not used by Java itself. Some third-party programs (for example Apache Tomcat) expect one of these environment variables to be set to the installation directory of the JDK or JRE . If you are not using software that requires them, you do not need to set JAVA_HOME and JRE_HOME . PATH is an environment variable used by the operating system (Windows, Mac OS X, Linux) where it will look for native executable programs to run. You should add the bin subdirectory of your JDK installation directory to the PATH , so that you can use the javac and java commands and other JDK tools in a command prompt window. Courtesy: coderanch
set environment variable
The command prompt wouldn’t use JAVA_HOME to find javac.exe, it would use PATH.
JAVA_HOME is a environment variable (in Unix terminologies), or a PATH variable (in Windows terminology). A lot of well behaving Java applications (which need the JDK/JRE) to run, looks up the JAVA_HOME variable for the location where the Java compiler/interpreter may be found.
JAVA HOME is used for setting up the environment variable for JAVA. It means that you are providing a path for compiling a JAVA program and also running the same. So, if you do not set the JAVA HOME( PATH ) and try to run a java or any dependent program in the command prompt.
You will deal with an error as javac : not recognized as internal or external command . Now to set this, Just open your Java jdk then open bin folder then copy the PATH of that bin folder.
Now, go to My computer right click on it—-> select properties——> select Advanced system settings——>Click on Environment Variables——>select New——>give a name in the text box Variable Name and then paste the path in Value.
Источник