Bookmark Topic Watch Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Downloading the Java SDK
 

  • Go to the official Java SE download page
  • Download the latest Java Platform, Standard Edition. You do not need to download the JRE; indeed most Java11+ downloads don't supply the JRE separately. Download the JDK, which includes the JRE. If you're interested to know the difference between the JDK and the JRE, check our JavaBeginnersFaq.
  • At the download page, select your target platform, which includes Windows, Linux and others. Also choose the 32-bit version (i586) or 64-bit (x64). Save the file in a directory of your choice.


  • Installing the Java SDK
     

    These instructions will assume that you are installing the JDK1.8.0_172, which was the most recent “LTS” (=Long Term Support) version at the time of this update. It is recommended to leave the version number in the install directory (e.g. 1.8.0_172). This will allow you to install several versions of the jdk under the same root directory (e.g. C:\java), and to easily recognize which version of the JDK lies in which directory.

    Windows

  • Double-click the file you have downloaded.
  • Follow the installer instructions. You don't need to change the default options. You may just change the directory where you want to install the JDK. After the installation is over, make sure that the directory you have chosen has been created.


  • Linux

    Ubuntu Users please see below.

    Other Flavours of Linux: You have to choose whether you want to use a zipped (.tar.gz) file or use an RPM package. Older versions of Java (up to Java 6) used a self-extracting binary file rather than a compressed file; instructions for installing that have been removed from this document. If your system is RPM-based, you may wish to use the RPM package instead of the binary file.

    Compressed tar file (Java7/8/9/10)

  • The file downloaded is a .tar.gz.
  • Move the file to the directory where you wish to install java (e.g. /usr/java). You can choose any sensible place to put it in, but make sure you have writing permission on that directory (or use sudo or change to root). Remember which directory you used, because that needs to be part of the PATH later.
  • Unzip the file : tar -zxvf jdk-8u172-linux-x64.tar.gz
  • After the installation is over, the JDK directory should be created under the current directory (e.g /usr/java/jdk1.8.0_172). Remember you will need to use sudo or root to access /usr.
  • You may remove the tar file if you wish: rm jdk-8u172-linux-x64.tar.gz


  • RPM Package

  • Change to root using the 'su' command
  • Set the execution permission on the downloaded file : chmod a+x jdk-8u172-linux-x86_64-rpm.bin
  • Execute the installer : ./jdk-8u172-linux-x64-rpm.bin
  • Follow the installer instructions.
  • After the installation is over, the installer should have created two symbolic links : /usr/java/latest and /usr/java/default. The /usr/java/latest link will tell you where your JDK is installed. For more information about these links, check the official install instructions.


  • Ubuntu

    Java should already be installed in your system, but the JDK used should be Open JDK, not Oracle JDK. If you wish to use the Oracle JDK, try the repository available from webupd8team. The instructions on the website are clear. That installation method appears to use the same technique shown above, of downloading a .tar.gz file and unzipping it, but it is automated. Test the installation by giving the following two instructions to the terminal:

    java -version

    javac -version

    If both those give the up-to-date version number, then the Java installation is already in your PATH and there is no need to edit the PATH as shown in the next section. You can of course install Java the same way as for all other flavours of Linux if you prefer.

    Tip: In the JDK install directory, there should be a file called src.zip. It contains all sources of the Java API. You might be interested to have a look at them someday !

    Setting the JAVA_HOME and PATH environment variables
     

    You will probably have to open a new command line or terminal window to use the updated PATH; the old window usually uses the old PATH environment variable.

    Windows XP

  • Start/Settings/Control Panel.
  • Double-click System, and select the Advanced tab.
  • Click the Environment Variables button.
  • You can set environment variables for either your user only, or for all users (System variables). But don't set a user JAVA_HOME and a system PATH (see this discussion). Add a new variable in either of them by clicking the New button.
  • Set the variable name as JAVA_HOME, and its value to the directory where you installed the JDK (e.g. C:\Program Files\java\jdk1.8.0_172)
  • Click OK.
  • Look for a variable called PATH (the name might slightly vary, for example 'Path'). Select it and click Edit. At the beginning of its value, add the following : "%JAVA_HOME%\bin;" (without the double quotations). Don't forget the semi-colon at the end, to separate this directory to the other directories already in the path.
  • Click OK and close all remaining windows.


  • Windows Vista / Windows 7/8/10

  • Start/Control Panel.
  • Double-click System, and select "Advanced system settings" on the left.
  • On the "Advanced" tab, click the Environment Variables button.
  • You can set environment variables for either your user only, or for all users (System variables). But don't set a user JAVA_HOME and a system PATH (see this discussion). Add a new variable in either of them by clicking the New button.
  • Set the variable name as JAVA_HOME, and its value to the directory where you installed the JDK (e.g. C:\Program Files\java\jdk1.8.0_172)
  • Click OK.
  • Look for a variable called PATH (the name might slightly vary, for example 'Path'). Select it and click Edit. At the beginning of its value, add the following : "%JAVA_HOME%\bin;" (without the double quotations). Don't forget the semi-colon at the end, to separate this directory to the other directories already in the path.
  • Click OK and close all remaining windows.
  • On Windows 7 it may be necessary to reboot your PC before the new environment variables are recognised. See this post
  • On Windows 10 there is a window with different PATH entries; the PATH to Java® looks the same as in older versions, but make sure each PATH entry has a line to itself. Don't try to combine two PATH entries on the same line. It is not necessary to end the PATH entry with a semicolon. Put the new Java® PATH on the first line.


  • Linux

    The directions vary depending which shell your computer uses, but most people use the "bash" shell. This is how to alter a PATH variable on "bash". Note the PATH variable already exists, so it is only necessary to add the PATH to one's Java installation.


  • Open the file called ".bashrc" in your home directory, with a text editor. From the shell, one can enter "gedit .bashrc" or "gedit ~/.bashrc", to use gedit. Don't forget the dot which means it is a hidden file.
  • Add a line with the Java path at its beginning. It will read like this "export PATH=/usr/java/jdk1.8.0_172/bin:$PATH". Note there is only one space, after export. The Java path begins with / and ends with bin, and paths are separated by : colons. If you have installed Java in your "home" folder, you may be able to use a path beginning with ~ instead of /.
    As an alternative, you can set both JAVA_HOME and PATH like this in .bashrc; you only need to set the PATH one way:-
    export JAVA_HOME=/usr/java/jdk1.8.0_172/
    export PATH=$JAVA_HOME/bin:$PATH
  • Save .bashrc.
  • Open a new shell. Test it with "java -version" and "javac -version". Use "echo $PATH" to see what your path is set to.
  • It is possible to add other paths similarly.


  • If you prefer, you can set a new temporary PATH by writing an instruction similar to this at a terminal: "export PATH=/usr/java/jdk1.7.0_79/bin:$PATH". That will update your PATH for that instance of the terminal, so you can use a different JDK from normal.

    Mac OS

    See Easton Hathaway's Blog The procedure is quite similar to that on Linux. You can see an example of an OS/X Java PATH in this post in our Mac Forum

    What is the purpose of these variables ?
     

    The JAVA_HOME variable is used by other programs to know where your JDK is. Most IDEs will use it. It is also used as a convenient shortcut, to avoid having to change the other places referring to the JDK install directory. For example, we have used it to set the PATH variable (%JAVA_HOME%). This is very convenient, because if you install another version of the JDK, all you need to do is to update your JAVA_HOME, without having to touch the PATH.

    But don't set JAVA_HOME as a user variable and use it in a system PATH (see discussion here).

    The PATH variable is used by your system command prompt. When you execute a program, your system will look for its location. It will scan all directories included in the PATH, and look for the program you are executing. This means that when you type "java", or "javac" at the command prompt, your system will look into %JAVA_HOME%\bin", find the command and execute it. If you don't set your PATH properly, you'll end up with an error message like "Command not found".

    Checking that your JDK is configured properly
     

    Open the command prompt, go to a directory different from your JDK install directory, and type "javac -version". This should display the version of your SDK. Make sure it is correct. If an error like "Command not found" is displayed, make sure you have correctly set your PATH. Check the environment variables settings again. If the version displayed is not the same as the JDK you have installed, another installed JDK may conflict. Make sure you have set your JDK directory at the beginning of the PATH variable.

    Creating your first Java program
     

    This is where the fun begins ! Let's create a simple greetings class. Use your favorite text editor to create a new file, not a word processor. You don't need an IDE like Eclipse or IntelliJ yet. For the moment, stick to the editor you're used to. Copy/paste the following code in your editor :



    Save the file in the directory of your choice. But do not save it under the JDK install directory. Instead, save it in a directory where you'd like to keep your java sources in. For example, C:\java\src. Name the file MooseGreetings.java. Be extra careful with the uppercase and lowercase. The filename must match exactly the name of your class. mOoSegReeTings.java won't work. Also be careful that your editor does not append the ".txt" extension to your file. Some filthy editors like Windows Notepad may do it, but Notepad is probably not your favorite editor !

    Compiling and executing the program
     

    (For Mac-specific instructions, go to https://coderanch.com/t/111632/Mac/JAVA-MAC-OS)

    At the command prompt, go to the directory where you have saved the MooseGreetings.java file. Type "javac MooseGreetings.java", and press the enter key.



    The class should be compiled, and a new file called MooseGreetings.class should be created in the same directory. If a compiler error happens, make sure you copy/pasted the above code properly. If the file is not found, make sure you're in the right directory, and that your file is properly named MooseGreetings.java. If you can't figure out why it does not compile, ask at the Beginning Java forum.

    Let's execute the program. In the same directory, type the following command : "java -classpath . MooseGreetings".



    Greetings for the Javaranch Moose should be printed : mooooooooooo.

    That's it ! You've successfully executed your first Java program. You're now ready to get into the Java world ! If you have any questions, some friendly folks will be happy to help you at the Beginning Java forum.

    What's that fancy -classpath flag used when executing a Java program ?
     

    The -classpath flag (or -cp flag) may be used at the command prompt to tell Java where to look for .class files. Your class files may be in directories, or in JAR files. These can be set in the classpath either by setting the CLASSPATH environment variable, or by using the -classpath flag. Details about setting the CLASSPATH are in the HowToSetTheClasspath FAQ. In our example, "-classpath ." means that Java should scan the current directory (".") for class files. If you don't have a system CLASSPATH set on your computer, you can omit -classpath . from the instruction.
     
    Consider Paul's rocket mass heater.
      Bookmark Topic Watch Topic
    • New Topic