The difference between the ClassPath environment variable and the project .classpath files in JAR

1

When we install the JDK, we create an environment variable called CLASSPATH, which has the value JAVA_HOME, which in turn has the jdk address, which contains all Java development libraries, correct? >

This is for the Java compiler to find the libraries needed to compile a Java application on this machine, correct?

Question 1: To run a Java application, does the JVM also search the ClassPath for libraries to run? Or does the Java executable already contain all the information the JVM needs?

At the same time, when we create a Java project in Eclipse, for example, eclipse generates two files inside .JAR: .project and .classpath . This .classpath is particular to every Java project right? And it also has the function of pointing the resource location (libraries) for compiling, or running or the 2, do not know , correct? But is not enough the existence of the CLASSPATH environment variable for the compiler to know where to look for resources? Or each project has a .classpath file because I can have libraries that are not in the path pointed to by the environment variable?

    
asked by anonymous 10.10.2017 / 03:07

1 answer

2

Variable CLASSPATH : used to indicate where to look for user CLASSES; used not only by the JVM but also by other JDK commands. There is also the option of the -cp command line to provide the classpath instead of the environment variable.

The classpath can contain ZIP or JAR files or directories with CLASS files (using the hierarchy of packages).

Exception: If you use java -jar to run a JAR file, the CLASSPATH variable will not be used! In this case the classpath comes from JAR itself: MANIFEST.MF can contain a Class-Path entry that will serve to indicate where to find the classes.

JAVA_HOME variable: not is used by Java, but by other applications that use Java - it is almost standard.

Question 1 : can the CLASS, JAR, or JVM (java / java.exe) file be?

  • CLASS: only contains the full name (including package) of the classes used, nothing about the path.

  • JAR: As described above, MANIFEST.MF has the classpath.

  • JVM: You need the CLASSPATH variable or the -cp or the MANIFEST.MF option in the case of java -jar (Java 9 has the option to work with modules).

Question 2 : Eclipse uses the .project file to describe the project, .classpath to indicate the classes / libraries used in the project. Neither should go in JAR - probably Eclipse configuration / use error - the user usually defines which files / directories to include in the JAR! But, on the other hand, these files will not influence anything on the Java side, they may even be useful for 'installing' the project on another Eclipse / machine.

Each project has its own .classpath so you do not have to include all the libraries in all the builds and especially to be able to have different versions of a library / class in different projects.

    
10.10.2017 / 11:10