Environment variables are global system variables accessible by all the processes running under the Operating System OS. Environment variables are useful to store system-wide values such as the directories to search for the executable programs PATH and the OS version. Examples of environment variables in Windows OS are: In setting up JDK and Java applications, you will encounter these environment variables: To display a particular variable, use command "set varname".
Pay particular attention to the variable called PATH. Local variables available to the current process only are in lowercase. To list all the environment variables, use the command "env" or "printenv". You could use "set" to list all the variables, including all local variables.
Enclosed the value with double quotes if it contains spaces. Local variable is available within this process only. Take note that files beginning with dot. To display hidden files, use command "ls -a" or "ls -al". To refresh the bash shell, issue a "source" command or re-start the bash shell: Use "setenv varname value" and "unsetenv varname" to set and unset an environment variable.
PATH When you launch a program from the command line, the operating system uses the PATH environment variable to search for the program in your local file system. In other words, PATH maintains a list of directories for searching executable programs. If the program cannot be found in these directories, you will get the following error: PATH maintains a list of directories.
The directories are separated by semi-colon ';'. JDK's "bin" directory e. The JDK's "bin" directory should be listed before "c: This is because some older Windows systems provide their own Java runtime which is often out-dated in these directories try search for "java.
But as PATH is frequently used, a dedicated command called path is provided. In other words, the current working directory is searched first, before searching the other entries specified in PATH, in a the order specified. DO NOT remove any existing entry; otherwise, some programs may not run. When you launch an executable program with file permission of executable in a Bash shell, the system searches the program in ALL the directories listed in the PATH. If the program cannot be found, you will get the following error: As a result, you have to enter ".
It is recommended to include the current directory in the PATH by adding this line at the end of the. How Classes are Found? Java Compiler "javac" , Java Runtime "java" and other Java tools searches for classes used in your program in this order: Java platform bootstrap classes: You can print its contents via System. User classes search path in short, class path: Defaulted to the current working directory.
The runtime command-line option -jar, which override all the above. The user class paths are kept in Java System property "java. It is recommended that you use the -cp or -classpath command-line option customized for each of your applications , instead of setting a permanent CLASSPATH environment for all the Java applications. Cannot Find Classes If the Java Runtime "java" cannot find the classes used in your program in all the above places, it will issue error "Could not find or load main class xxxx" JDK 1.
Similarly, Java Compiler "javac" will issue compilation errors such as "cannot find symbol", "package does not exist". External native libraries ". Otherwise, you will get a runtime error "java. Otherwise, the current directory will not be searched.
A common problem in running hello-world program is: The current directory is therefore not searched, which results in "Error: Could not find or load main class Hello". Path entries are separated by semi-colon ;.
Make sure that the current directory '. Read the above section on how to set or change environment variable in Windows, Mac and Unixes. Java classes run in Windows as well as Unixes - binary compatible. Unixes have many shells, such as the newer bash and the older csh, ksh.
Windows have two shells: Each shell come with its own set of commands, utilities, and its own scripting programming language. Unix uses command "printenv" print environment or "env" to list all the environment variables. Windows uses command "set".
Unix's PATH is set permanently in the login or shell initialization script e. To run a program in the current directory, you need to issue ". It is recommended to include the current directory. A Windows' path includes a drive letter and directories. Each drive has a root directory. Linux's paths do not have drive letter. There is a single root. Windows use semi-colon ';' as path separator e. Unix uses "0AH" line-feed only.