Search code examples
javajunit-rule

Changing CLASSPATH to JUNIT_HOME/junit-4.12.jar, java command stops working


When I change my CLASSPATH variable to JUNIT_HOME/junit-4.12.jar in system variables, my Java command stops working.

For example, when I want to execute a class file I get the error "Could not find or load main class", though javac is working fine.

When I remove the CLASSPATH, the java command starts working again.


Solution

  • The CLASSPATH variable is one way to tell applications, including the JDK tools, where to look for user classes. When you execute a java command to start a Java application, it start a Java runtime environment, loading a specified class, and calling that class's main method.

    If your CLASSPATH variable is set to JUNIT_HOME/junit-4.12.jar, only classes inside the JUNIT_HOME/junit-4.12.jar will be loaded. Therefore, you will get a Could not find or load main class error.

    The preferred way to specify the class path is by using the -cp command line switch. This allows the CLASSPATH to be set individually for each application without affecting other applications.

    The default value of the class path is ".", meaning that only the current directory is searched. If you want also find classes file in other directory, say classes in c:\otherDirectory, you can set the class path to the following:

    java -classpath ".;c:\otherDirectory"