1

A lot of times in Java we want to use some functionality that is given to us in the form of JARs(ex. some external library). Most often than not I've noticed that JARs contain .class files.

Since .class files represent compiled bytecode ready for use by the JVM, my question is the following: How is it that .class files are all that's needed for us to make use of an external library? Maybe a certain JAR contains the class file called: Person.class. How am I able to reference this class in my code when all that the JAR file exposes is a .class file. Isn't the source code(.java file) what's important and what's needed? In the same way that I can have two classes in the same package, I'm able to reference one from the other, because the two .java files(not .class files) are in the same scope(just to give an example).

Excuse me if it's a dumb question, but I really want to understand this.

Mr. Nicky
  • 1,519
  • 3
  • 18
  • 34
  • I am not sure if your question is pointing to how the information is retrieved from the jar to let the jvm know what are relevant files available in the form of dependency. Perhaps the manifest file is what you are looking for, https://docs.oracle.com/javase/tutorial/deployment/jar/defman.html – happyHelper Oct 07 '17 at 11:20

3 Answers3

3

Even if you write your source code in .java files, they are eventually compiled to form .class files which store bytecode that can be interpreted easily. When you use the jar files in your project, all the class files inside those jar files are included in your classpath, hence enabling you to use them.

So in a JAR package, .class files are sufficient to be used as a dependency.

Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
  • Does that mean that the compiler has no problem referencing and calling classes/functions from already compiled .java files, i.e. .class files? And that is why I can easily add import statements(for example) to external library classes and make use of JARs? – Mr. Nicky Oct 07 '17 at 11:30
1

The Java compiler takes your Java code, which is something that humans can understand, into .class files, which is something that the Java Virtual Machine (JVM) can understand. The JVM then takes the .class files and runs them on your machine.

A .jar file is effectively a collection of .class files packaged up (under the hood, it's really little more than a .zip in disguise). When you add a .jar onto your classpath, you are telling the JVM that it is one more place it should look when it needs a particular class.

Joe C
  • 15,324
  • 8
  • 38
  • 50
1

I am not sure if I totally got your question, but the JARs are simply compiled javacode, which means, that the semantic/logic etc of the code has not been changed. You need to be able to access the functions/classes etc of the java code you want to use, because otherwise you would not gain any advantage of using a JAR.

One advantage of the JARs is, that the source code of these libraries is already compiled. Since these .class files are compiled .java files, they are all you need to access the functions that were written in the .java file.

Absent
  • 884
  • 3
  • 11
  • 24
  • So you're saying that the compiler has no problem referencing and calling classes/functions from already compiled .java files, i.e. .class files? And that is why I can easily add import statements to external library classes and make use of JARs? – Mr. Nicky Oct 07 '17 at 11:23
  • 1
    Exactly. The source code gets compiled into .class files in a way that the functions etc can be used in other code simply by including/importing the compiled java code. – Absent Oct 07 '17 at 11:36