How do I setup JAVA_HOME and Path variables in Windows?

Setting up a JAVA_HOME and Path variables is the second thing you’ll need to do after installing a JDK (Java Development Kit). Although this is not required by Java it self, it is commonly use by other application. For instance then Apache Tomcat web application server and other application server will need it. Or we might need it if we want to compile or running our Java classes from the command prompt. It helps us to organize the default JDK and the execution path.

So here are the steps that we’ll need to do to configure the JAVA_HOME and Path variable on a Windows operating system.

Step 1. Finding the location of our JDK installation directory. If we already know where we have installed the JDK continue to the Step 2.

  1. The JDK usually installed in the C:\Program Files\Java directory by default.
  2. Under this directory we can find one or more versions of installed JDK, for examples I have jdk1.6.0_39 and jdk1.7.0_13. Just choose the default one we’re going to use.

Step 2. Setting JAVA_HOME variable

After we know the location of your JDK installation, we can copy the directory location from the Windows Explorer address bar.

  1. Open Windows Explorer
  2. Right Click the Computer and select the Properties menu.
  3. Click Advanced system settings and the System Properties windows will be shown.
  4. Select the Advance tab.
  5. Click the Environment Variables button.
  6. A new Environment Variables window will be shown.
  7. Under the System Variables, click the New button to create a new environment variable.
  8. Enter the variable name as JAVA_HOME, all letters are in uppercase.
  9. In the variable value enter the JDK installation path you’ve copy above.
  10. Click OK.

Step 3. Setting the Path variable

After we’ve set the JAVA_HOME variable, now we can update the Path variable.

  1. In the Environment Variables window, under the System Variables section find a variable named Path.
  2. If we don’t have the Path variable we need to add one using the New button.
  3. If we already have the Path variable we’ll need to update its value, click Edit button to update.
  4. Add %JAVA_HOME%\bin; to the beginning of the Path variable value.
  5. Press OK to when we are done.
  6. Press another OK to close the Environment Variables window.

Step 4. Check to see if the settings work

  1. Open your Windows Command Prompt.
  2. Type java -version in the command line.
  3. If everything was set correctly we’ll see the running version of your installed Java JDK.

As an example on my Windows Command Prompt I have something like:

D:\>java -version
java version "1.7.0_13"
Java(TM) SE Runtime Environment (build 1.7.0_13-b20)
Java HotSpot(TM) Client VM (build 23.7-b01, mixed mode)

If you don’t see the correct output, for instance you get an error like “‘java’ is not recognized as an internal or external command, operable program or batch file”, please retry the steps described above. Enjoy your new adventure with Java programming. Happy coding!

How do I get the absolute path of a file?

package org.kodejava.example.io;

import java.io.File;

public class AbsolutePathExample {
    public static void main(String[] args) {
        // Create an instance of File.
        File file = new File("kodejava.html");

        // Now we want to know where is exactly this file is located
        // in our file system. To do this we can use the getAbsolutePath()
        // method of the File class.
        String absolutePath = file.getAbsolutePath();

        // Print out the location of kodejava.html
        System.out.println("AbsolutePath = " + absolutePath);
    }
}

Here is the result of the program:

AbsolutePath = /Users/wsaryada/Workbench/kodejava/kodejava.html