Running java from an Eclipse installation from the command line

Learning that lasts. Online courses from $14.99

For an introductory Java class I am taking, I installed the Eclipse integrated development environment (IDE) on a Microsoft Windows 10 system. In addition to compiling and running Java programs through the IDE, though, I wanted to compile Java programs using the Java compiler, javac.exe, and run them using java.exe from Windows' command-line interface (CLI). The installation process for Eclipse installed the javac.exe and java.exe executables in a user directory with a long directory path to those executables. Note: you should install the Eclipse IDE from the account which you wish to use to compile and run java programs.

C:\Users\Jim>dir /s javac.exe
 Volume in drive C has no label.
 Volume Serial Number is 9420-A68C

 Directory of C:\Users\Jim\.p2\pool\plugins\org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_18.0.1.v20220515-1614\jre\bin

05/15/2022  04:10 PM            13,312 javac.exe
               1 File(s)         13,312 bytes

C:\Users\Jim>dir /s java.exe
 Volume in drive C has no label.
 Volume Serial Number is 9420-A68C

 Directory of C:\Users\Jim\.p2\pool\plugins\org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_18.0.1.v20220515-1614\jre\bin

05/15/2022  04:10 PM            39,424 java.exe
               1 File(s)         39,424 bytes

C:\Users\Jim>

The location wasn't added to the path environment variable, so if you try to run the programs from a command prompt, you will see the following, unless you specify the full path name where the executable files are located.

C:\Users\Jim>java
'java' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\Jim>javac
'javac' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\Jim>

I didn't want to have to copy and paste the full path on the command line or type it in every time I wanted to use the two executables from the command line. To avoid that problem you can add the directory path to the path environment variable through a set path= command or create another environment variable, e.g., JAVA_HOME that points to the directory where the two files are located, though both methods apply only to a particular command line instance. I.e., if you close a command prompt window where you've set one of the variables to include the location where Eclipse installed java.exe and javac.exe then open another command prompt window, you will have to set the environment variable again in the new instance. An alternative way to make a permanent path change from a command prompt interface is to use the Windows setx command, though I would not recommend it, since if your path variable already has a path that is more than 1,024 characters long, setx can truncate the path to 1,024 characters, so that you not only don't get the additional directory added to the path, but you may lose some of the path setting you had prior to issuing the command. If you want to make a permanent change, see responses to the "Overcoming the 1024 character limit with setx" posting at the superuser.com site.

Creating a JAVA_HOME environment variable

If you wish to create a JAVA_HOME variable, you can set it to the path for the jre directory (you could set it to the bin directory within that directory, but some java applications expect that variable to be set to the jre directory, so I set it to the jre directory). You can use a set JAVA_HOME= command like the following to create the JAVA_HOME environment variable at a Windows command prompt by substituting the actual user directory on your system for the "Jim" one in the set command below. You can change the value you set by issuing the set command again with a different value. Note: the set command only sets the environment variable for the account from which it is run, not for all accounts on the system. Once you've set the environment variable, you can run java or javac by prefixing the command with %java_home%\bin as shown below. You can test that you've got the variable set correctly by issuing the command %java_home%\bin\javac --help. Windows will subsitute the value for the variable JAVA_HOME when you put a percent sign before and after the variable name.

C:\Users\Jim>set JAVA_HOME=C:\Users\Jim\.p2\pool\plugins\org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_18.0.1.v20220515-1614\jre\

C:\Users\Jim>%JAVA_HOME%\bin\javac --help
Usage: javac <options> <source files>
where possible options include:
  @<filename>                  Read options and filenames from file
  -Akey[=value]                Options to pass to annotation processors
  --add-modules <module>(,<module>)*
        Root modules to resolve in addition to the initial modules, or all modules
        on the module path if <module> is ALL-MODULE-PATH.
  --boot-class-path <path>, -bootclasspath <path>
        Override location of bootstrap class files
  --class-path <path>, -classpath <path>, -cp <path>
        Specify where to find user class files and annotation processors
  -d <directory>               Specify where to place generated class files

<text snipped>

C:\Users\Jim>

You can view the value for the environment variable by usng the echo command by prefixing and affixing a percent sign to the environment variable name as shown below.

C:\Users\Jim>echo %JAVA_HOME%
"C:\Users\Jim\.p2\pool\plugins\org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_18.0.1.v20220515-1614\jre\"

C:\Users\Jim>

You can then run the commands using the JAVA_HOME environment variable in lieu of typing out the long path to the directory as shown below (if no parameters are given to the javac program, it will display the options available to you).

C:\Users\Jim>%JAVA_HOME%\bin\javac.exe
Usage: javac <options> <source files>
where possible options include:
  @<filename>                  Read options and filenames from file
  -Akey[=value]                Options to pass to annotation processors
  --add-modules <module>(,<module>)*
        Root modules to resolve in addition to the initial modules, or all modules
        on the module path if <module> is ALL-MODULE-PATH.
  --boot-class-path <path>, -bootclasspath <path>
        Override location of bootstrap class files
  --class-path <path>, -classpath <path>, -cp <path>
        Specify where to find user class files and annotation processors
  -d <directory>               Specify where to place generated class files
  -deprecation
        Output source locations where deprecated APIs are used
  --enable-preview
        Enable preview language features. To be used in conjunction with either -source or --release.
  -encoding <encoding>         Specify character encoding used by source files
  -endorseddirs <dirs>         Override location of endorsed standards path
  -extdirs <dirs>              Override location of installed extensions
  -g                           Generate all debugging info
  -g:{lines,vars,source}       Generate only some debugging info
  -g:none                      Generate no debugging info
  -h <directory>
        Specify where to place generated native header files
  --help, -help, -?            Print this help message
  --help-extra, -X             Print help on extra options
  -implicit:{none,class}
        Specify whether or not to generate class files for implicitly referenced files
  -J<flag>                     Pass <flag> directly to the runtime system
  --limit-modules <module>(,<module>)*
        Limit the universe of observable modules
  --module <module>(,<module>)*, -m <module>(,<module>)*
        Compile only the specified module(s), check timestamps
  --module-path <path>, -p <path>
        Specify where to find application modules
  --module-source-path <module-source-path>
        Specify where to find input source files for multiple modules
  --module-version <version>
        Specify version of modules that are being compiled
  -nowarn                      Generate no warnings
  -parameters
        Generate metadata for reflection on method parameters
  -proc:{none,only}
        Control whether annotation processing and/or compilation is done.
  -processor <class1>[,<class2>,<class3>...]
        Names of the annotation processors to run; bypasses default discovery process
  --processor-module-path <path>
        Specify a module path where to find annotation processors
  --processor-path <path>, -processorpath <path>
        Specify where to find annotation processors
  -profile <profile>
        Check that API used is available in the specified profile
  --release <release>
        Compile for the specified Java SE release. Supported releases: 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18
  -s <directory>               Specify where to place generated source files
  --source <release>, -source <release>
        Provide source compatibility with the specified Java SE release. Supported releases: 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18
  --source-path <path>, -sourcepath <path>
        Specify where to find input source files
  --system <jdk>|none          Override location of system modules
  --target <release>, -target <release>
        Generate class files suitable for the specified Java SE release. Supported releases: 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18
  --upgrade-module-path <path>
        Override location of upgradeable modules
  -verbose                     Output messages about what the compiler is doing
  --version, -version          Version information
  -Werror                      Terminate compilation if warnings occur


C:\Users\Jim>

Adding the location of java.exe and javac.exe to your path

Alternatively, you can avoid having to type in the name of the environment variable java_home each time you wish to run one of the programs by adding the directory where they reside to your existing path environment variable—you can see the value for that variable by issuing the command echo %path% at a command prompt. To add a directory to the existing path you use a set command in the form set PATH=%PATH%;additionalDirectory, where additionalDirectory is the full path to the directory that you wish to include in the path. I.e., you put %path% followed by a semicolon and then the additional directory path on the right side of the equal sign as shown below. It doesn't matter whether you use uppercase or lowercase letters when specifying the name of an environment variable, but you would, of course, need to substitute the name for the Windows account you are using for "Jim".

C:\Users\Jim>set path=%path%;C:\Users\Jim\.p2\pool\plugins\org.eclipse.justj.openjdk.hotspot.jre.full.win32.x86_64_18.0.1.v20220515-1614\jre\bin

C:\Users\Jim>

You should then be able to issue a java --help or javac --help command at the command prompt and see the help information for either program.

Related articles:

  1. Obtaining a command prompt in Windows 10
    Date: February 18, 2017

References:

  1. How to set java path in windows 10
    By: Arpit Mandliya
    Date: December 7, 2020
    Java2Blog
  2. Overcoming the 1024 character limit with setx
    Date: February 8, 2012
    superuser