Main Menu

search

You are here

Java: CLASSPATH Environment variable

[last updated: 2019-05-26]
go to: Java
go to: Java-Editing, Compiling, Executing
-----

(link to:) good tutorial

  • Environment Variables:
    • CLASSPATH is an environment variable used by Application ClassLoader to find and load the .class files
    • To list all your Environment Variables:
      $ env
    • To list CLASSPATH:
      $ echo ${CLASSPATH}
  • Setting CLASSPATH:
    • If you do nothing, then the default setting for CLASSPATH is the directory you're in.
    • You should set CLASSPATH to include all the directories which contain .class files and jar files.
    • To clear CLASSPATH and return it to default,
          $ export CLASSPATH=
    • To set CLASSPATH to some new directory,
          $ export CLASSPATH=[newDirName]
      Note: As written, this statement will result in CLASSPATH being just [newDirName], that is, it will NOT include the default directory you're in.
          If you want to include the default directory (or all of any previously defined directories), use:
          $ export CLASSPATH= ${CLASSPATH}:[newDirName]
    • To add an additional directory to list of directories already defined in CLASSPATH,
          $ export CLASSPATH=${CLASSPATH}:[another path to add to existing setting]:[another path to add] ...
    • However, this setting does not persist through bootup.
      If you want to specify a particular classpath on the command line,
      that is, without permanently setting CLASSPATH in profile as below, do this:
      $ java (or javac) -classpath (or -cp) [path to location of class files you want to find]
    • To permanently set CLASSPATH:
      • got to /etc, and sudo nano profile
      • Add at the bottom of the file:
        ... see site index for profile editing for PATH variable
  • Using CLASSPATH:
    • Disclaimer: This is my best guess, because as I struggle through the process, this worked.
    • If you have NOT defined a package in your source file,
      then setting CLASSPATH will let java find your .class file.
      That is, suppose your class file is located in a directory: /someDir
      Then if you set your CLASSPATH to include /someDir, then $ java [yourClass] will execute (it will find your class), regardless of the directory you're located in when you execute the java statement.
    • If you Have defined a package in your source file, ...
      here's what works:
      suppose your source file has package dir1;
      Then when in the directory above dir1, execute your class file with:
      $ java dir1.[classFileName]
      Or, if you edit CLASSPATH to include the directory above dir1, then you can execute your class file from anywhere with the same command:
      $ java dir1.[classFileName]
  • Setting PATH:
    • ...
    • $ export PATH=${PATH}:[another path to add to existing setting]
  • Misc notes:
    When running tests, you should add both the compiled main classes directory and the compiled test classes directory on the classpath.
    When running the application, only the compiled main classes should be on the classpath.

.

.

.

eof