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

Asked

Viewed 1,683 times

1

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

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

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

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

1 answer

2


Variable CLASSPATH: used to indicate where to search for user CLASSES; used not only by 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 hierarchy of the Packages).

Exception: if to 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: MANIFEST.MF can contain a Class-Path entry that will serve to indicate where to find the classes.

Variable JAVA_HOME: nay 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: contains only the name complete (including package) of the classes used, nothing regarding the path.

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

  • JVM: requires CLASSPATH variable or option -cp or 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 of the two should go in the JAR - probably Eclipse configuration/usage 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 to 'install' the project on another Eclipse/machine.

Each Project has its own .classpath not to have to include all libraries in all compilations and especially to have different versions of a library/class in different projects.

  • So this . classpath file that you have in each project folder is just Eclipse usage, right? I opened one to see and found that it points to a "Container" that would be an eclipse plugin that has reference to all these libraries. This would be so that I don’t need to reference all libraries in all the . classpath of all the right projects?

  • Another thing, when you say that the CLASSPATH variable is "used to indicate where to look for user CLASSES", do you mean the classes in my project or the Java libraries? Because I opened a MANIFEST and its "Classpath" field only has one point. Yes, I’m lost yet. kkkk

Browser other questions tagged

You are not signed in. Login or sign up in order to post.