0

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.

asymmetric
  • 3,800
  • 3
  • 34
  • 52
Shubham Khare
  • 171
  • 1
  • 1
  • 12

2 Answers2

3

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"
Wilson
  • 11,339
  • 2
  • 29
  • 33
1

Do you have more jars that need to go on the classpath? JUNIT is for test purposes only and I would guess that you need to add all the dependencies you are using also on the classpath. On the other hand it is not a good idea to do this through system environment variables. Please also look at the java -classpath command then you can add classpath dependencies for one application or command.

Ivonet
  • 2,492
  • 2
  • 15
  • 28