- Think of the classpath as a roadmap that tells the Java Virtual Machine (JVM) where to look for the .class files (compiled Java code) and libraries (.jar files) that your program needs to run. It's essentially a list of directories and files that the JVM searches through to find the classes and resources your code references.
- How is it Set?
- Environment Variable: You can set the CLASSPATH environment variable to list the directories and JAR files you want to include.
- Command Line: When running a Java program (java command), you can use the -cp or -classpath option to specify the classpath directly.
- Manifest Files: JAR files can contain a manifest file that specifies the classpath for the classes inside the JAR.
- Build Tools: Build tools like Maven or Gradle automatically manage the classpath for your project, making it easier to build and run your code.
java -cp /path/to/my/project:/path/to/library.jar MyMainClass
This command tells the JVM to look for classes in /path/to/m