Search code examples
javaclassloader

How we can load a class from different location?


I've written a program in directory

C:\Program files\Java\jdk1.6\bin\

and I want to load a class from different location i.e.

D:\\MyProgs\

How should I do this?


Solution

  • You have to set Path (environment variable) of C:\Program files\Java\jdk1.6\bin. If you have Win OS then open System Properties (Control Panel) + Advanced + Environment Variables) to set or create new Path variable.

    If you want to run a class which is located at another folder then you have to use -classpath switch. In fact both, java compiler (javac.exe) and java launcher (java.exe) require the location of classes and you may specify using -classpath or -cp switch.

    For instance:

    D:\MyProgs\>java -cp .;"C:\Program files\Java\jdk1.6\bin" NameOfClass