What is the difference between compile time and run time dependencies in Java? It is related to class path, but how do they differ?
7 Answers
Compile-time dependency: You need the dependency in your
CLASSPATH
to compile your artifact. They are produced because you have some kind of "reference" to the dependency hardcoded in your code, such as callingnew
for some class, extending or implementing something (either directly or indirectly), or a method call using the directreference.method()
notation.Run-time dependency: You need the dependency in your
CLASSPATH
to run your artifact. They are produced because you execute code that accesses the dependency (either in a hardcoded way or via reflection or whatever).
Although compile-time dependency usually implies run-time dependency, you can have a compile-time only dependency. This is based on the fact that Java only links class dependencies on first access to that class, so if you never access a particular class at run-time because a code path is never traversed, Java will ignore both the class and its dependencies.
Example of this
In C.java (generates C.class):
package dependencies;
public class C { }
In A.java (generates A.class):
package dependencies;
public class A {
public static class B {
public String toString() {
C c = new C();
return c.toString();
}
}
public static void main(String[] args) {
if (args.length > 0) {
B b = new B();
System.out.println(b.toString());
}
}
}
In this case, A
has a compile-time dependency on C
through B
, but it will only have a run-time dependency on C if you pass some parameters when executing java dependencies.A
, as the JVM will only try to solve B
's dependency on C
when it gets to execute B b = new B()
. This feature allows you to provide at runtime only the dependencies of the classes that you use in your code paths, and ignore the dependencies of the rest of the classes in the artifact.

- 21,974
- 5
- 38
- 51
-
2I know this is now a very old answer, but how can the JVM not have C as a runtime dependency from the start? If it's able to recognize "here's a reference to C, time to add it as a dependency", then isn't C already essentially a dependency since the JVM recognizes it and knows where it is? – wearebob Jun 07 '19 at 16:03
-
@wearebob It could have been specified that way I guess, but they decided that lazy linking was better, and personally I agree for the reason stated above: it allows you to use some code if necessary, but does not force you to include it in your deployment if you do not need it. That is quite handy when dealing with third party code. – gpeche Jun 07 '19 at 16:56
-
If I have a jar deployed somewhere though, it's already going to have to contain all of it's dependencies. It doesn't know if it will be run with arguments or not (so it doesn't know whether or not C will be used), so it would have to have C available either way. I just don't see how any memory/time is saved by not having C on the classpath from the start. – wearebob Jun 07 '19 at 19:47
-
1@wearebob a JAR does not need to include all its dependencies. That 's why almost every non-trivial application has a /lib directory or similar containing multiple JARs. – gpeche Jun 07 '19 at 19:52
-
1@wearebob. this question touches on software architecture and application life cycle. Consider public APIs and service implementations. The concept of compile/runtime is reflected in build tools like Gradle too. Think of 'implementation' as some swappable service code. In simple applications compile and runtime codebase are often identical coming from the an uber Jar. In case of enterprise apps which may go through many releases, the story is more complicated as you have to upgrade dependencies. Compile/runtime helps maintaining backward compatibility. hope this helps – Vortex Apr 11 '21 at 03:30
The compiler needs the right classpath in order to compile calls to a library (compile time dependencies)
The JVM needs the right classpath in order to load the classes in the library you are calling (runtime dependencies).
They may be different in a couple of ways:
1) if your class C1 calls library class L1, and L1 calls library class L2, then C1 has a runtime dependency on L1 and L2, but only a compile time dependency on L1.
2) if your class C1 dynamically instantiates an interface I1 using Class.forName() or some other mechanism, and the implementing class for interface I1 is class L1, then C1 has a runtime dependency on I1 and L1, but only a compile time dependency on I1.
Other "indirect" dependencies which are the same for compile-time and run-time:
3) your class C1 extends library class L1, and L1 implements interface I1 and extends library class L2: C1 has a compile-time dependency on L1, L2, and I1.
4) your class C1 has a method foo(I1 i1)
and a method bar(L1 l1)
where I1 is an interface and L1 is a class that takes a parameter which is interface I1: C1 has a compile-time dependency on I1 and L1.
Basically, to do anything interesting, your class needs to interface with other classes and interfaces in the classpath. The class/interface graph formed by that set of library interfaces yields the compile-time dependency chain. The library implementations yield the run-time dependency chain. Note that the run-time dependency chain is run-time dependent or fail-slow: if the implementation of L1 sometimes depends on instantiating an object of class L2, and that class only gets instantiated in one particular scenario, then there's no dependency except in that scenario.

- 184,598
- 164
- 608
- 970
-
1
-
Thanks, but how does the class loading work at run time? At compile time it is easy to understand. But at runtime, how does it act, in a case when I have two Jars of different versions? Which one will it pick? – Kunal Nov 24 '10 at 20:50
-
1I'm pretty sure the default classloader takes the classpath and steps through it in order, so if you have two jars in the classpath that both contain the same class (e.g. com.example.fooutils.Foo), it will use the one that is first in the classpath. Either that or you'll get an error stating the ambiguity. But if you want more info specific to classloaders you should ask a separate question. – Jason S Nov 24 '10 at 21:22
-
I think in the first case, the compile time dependencies should also be there on L2 i.e. the sentence should be: 1) if your class C1 calls library class L1, and L1 calls library class L2, then C1 has a runtime dependency on L1 and L2, but only a compile time dependency on L1 & L2. This is so, as at the compile time also when java compiler verifies L1, then it also verifies all the other classes referenced by L1 (excluding the dynamic dependencies like Class.forName("myclassname)) ... otherwise how does it verifies that the compilation is working fine. Please explain if you think otherwise – Rajesh Goel Feb 09 '16 at 20:38
-
2No. You need to read up on how compilation and linkage works in Java. All the compiler cares about, when it refers to an external class, is how to *use* that class, e.g. what its methods and fields are. It doesn't care what actually happens in that external class's methods. If L1 calls L2, that's an implementation detail of L1, and L1 has already been compiled elsewhere. – Jason S Feb 09 '16 at 22:41
-
In second para of this answer, it is load time dependency, but not runtime dependency. Runtime dependency is purely thru reflection – overexchange Mar 03 '19 at 15:46
An easy example is to look at an api like the servlet api. To make your servlets compile, you need the servlet-api.jar, but at runtime the servlet container provides a servlet api implementation so you do not need to add servlet-api.jar to your runtime class path.

- 241
- 1
- 7

- 13,052
- 4
- 54
- 77
-
1For clarification (this confused me), if you are using maven and building a war, "servlet-api" is usually a "provided" dependency instead of a "runtime" dependency, which would cause it to be included in the war, if I am correct. – xdhmoore Sep 14 '16 at 21:49
-
3'provided' means, include at compile time, but don't bundle it in the WAR or other collection of dependencies. 'runtime' does the opposite (not available at compile, but packaged with the WAR). – KC Baltz Aug 04 '17 at 16:51
Java doesn't actually link anything at compile time. It only verifies the syntax using the matching classes it finds in the CLASSPATH. It's not until runtime that everything gets put together and executed based on the CLASSPATH at that time.

- 6,120
- 2
- 26
- 31
Compiletime dependencies are only the dependencies (other classes) which you use directly in the class you're compiling. Runtime dependencies covers both the direct and indirect dependencies of the class you're running. Thus, runtime dependencies includes dependencies of dependencies and any reflection dependencies like classnames which you have in a String
, but are used in Class#forName()
.

- 1,082,665
- 372
- 3,610
- 3,555
-
Thanks, but how does the class loading work at run time? At compile time it is easy to understand. But at runtime, how does it act, in a case when I have two Jars of different versions? Which class would Class.forName() pickup in case of multiple classes of different classes in a class path? – Kunal Nov 24 '10 at 20:52
-
The one matching the name of course. If you *actually* mean "multiple versions of same class", then it depends on the classloader. The "closest" one will be loaded. – BalusC Nov 24 '10 at 20:54
-
Well I think if you have A.jar with `A`, B.jar with `B extends A` and C.jar with `C extends B` then C.jar depends on compile time on A.jar even though C dependency on A is indirect. – gpeche Nov 24 '10 at 22:39
-
1The issue in all compile-time dependencies is *interface* dependency (whether the interface is through a class's methods, or through an interface's methods, or through a method which contains an argument that is a class or interface) – Jason S Nov 24 '10 at 23:04
For Java, compile time dependency is your source code's dependency. For instance, if class A calls a method from class B, then A is dependent to B at the compile time since A has to know about B (type of B) to be compiled. The trick here should be this: Compiled code is not a complete and executable code yet. It includes replaceable addresses (symbols, metadata) for the sources which are not yet compiled or existing in external jars. During linking, those addresses must be replaced by actual adresses in the memory. To do it properly, correct symbols/adresses should be created. And this can be done with the type of the class (B). I believe that's the main dependency at the compile time.
Runtime dependency is more related with the actual flow-of-control. It involes actual memory addresses. It's a dependency that you have when your program is running. You need class B details here like implementations, not only the type info. If the class not exists, then you will get RuntimeException and JVM will exit.
Both dependencies, generally and shouldn't, flow the same direction. This is a matter of OO design though.
In C++, compilation is a bit different (not just-in-time) but it has a linker too. So the process might be thought similar to Java I guess.

- 2,471
- 2
- 31
- 40
From @Jason S answer I derive mine with other words, in case it helps:
A runtime dependency of an app is actually a dependency (let's call it L2) of a compile-time dependency (L1) of this app. It may not be declared as a dependency if it won't be used by the app.
If L2 happens to be used by the app (through L1) while not declared as a dependency, there will be a NoClassDefFoundError.
If L2 is declared as a compile-time dependency of the app, and not used at runtime, it uselessly makes the jar larger and compile-time longer than needed.
Declaring L2 as a runtime dependency allows the JVM to lazy-load it, only when needed.

- 976
- 3
- 14
- 31