The difference between the classpath environment variable and the files.classpath of projects in JAR

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

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

Question 1: to run a Java application, the JVM also searches the ClassPath by libraries to be able to do the execution? Or does the Java executable already contain all the information the JVM needs?

Question 2: at the same time, when we create a Java project, in Eclipse, for example, eclipse generates two files inside the .JAR: o .project and .classpath . Esse .classpath is particular to each Java project right? And it also has the function of pointing the location of resources (libraries) for compilation, or execution or the 2, I don't know , correct? But isn't the existence of the CLASSPATH environment variable enough for the compiler to know where to fetch resources? Or each project has a file.classpath Why Can I have libraries that are not in the path pointed by the environment variable?

Author: Lucas Pletsch, 2017-10-10

1 answers

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 command line option -cp to provide the classpath instead of the environment variable.

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

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 the jar itself: the MANIFEST.MF may contain a class-Path entry that will serve to indicate where to find the classes.

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

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

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

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

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

Question 2 : Eclipse uses the file .project to describe the project, .classpath to indicate the classes/libraries used in the project. Neither should go in the JAR-probably error of eclipse setup/usage-the user typically 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 to 'install' the project on another Eclipse/machine.

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

 2
Author: , 2017-10-10 11:26:29