Personal notes on Java
For other technologies like HTML, CSS, .NET, PHP, etc. check my other blog

Specifying a classpath

References:


The class path is the path that the Java runtime environment searches for classes and other resource files. The class search path (more commonly known by the shorter name, "class path") can be set using either the:

  • -classpath option when calling a JDK tool like javac or java (the preferred method)
  • or by setting the CLASSPATH environment variable (not recommended). 
The -classpath option is preferred because you can set it individually for each application without affecting other applications and without other applications modifying its value.

Using the CLASSPATH environment variable (not recommended):

In windows you can operate with environment variables directly from a console (any modifications will only be valid for the current console):

  • List all environment variables:
    C:> set
  • check the contents of a variable (ex. the CLASSPATH var):
    C:> echo %CLASSPATH%
  • change the contents of a var:
    C:> set CLASSPATH=.;C:\axis2-1.6.2\lib\*
    You can also use the contents of other vars, ex:
    C:>set CLASSPATH=.;%AXIS2_HOME%\lib\* <--- the ".;" part especifies the root dir
    C:>set CLASSPATH=%CLASSPATH%;some\other\dir <-- this will append the new dir to the classpath
If you want to permanently change the contents of a var go to:
Righ-Click "My Computer" -> Properties -> Advanced -> Press "Environment Variables" button.
Edit/Create variable named CLASSPATH and add any required directories to it. You can use wildcards to specify multiple files, ex.:
  • .;C:\axis2-1.6.2\lib\*

Using the javac -cp (or -classpath):

  • Compiling the class MainFrame (that needs access to classes and jars specified in the classpath):
    C:\project> java -cp bin;C:\classes;E:\lib\junit.jar com.elharo.gui.MainFrame
  • Using classpath wildcards to specify multiple jars (NOTE: if you don't use the "" it wont work): C:\project> javac -cp "C:\axis2-1.6.2\lib\*" net\roseindia\*.java
    will compile all java classes in net.roseindia package and use required jars located in C:\axis2-1.6.2\lib\

Sem comentários:

Enviar um comentário