What is the difference between compile time and run time dependencies in Java? It is related to class path, but how do they differ?
A dependency that can be resolved at compile time is a compile-time dependency. A dependency that cannot be resolved until runtime is a runtime dependency. Compile-time dependencies tend to be easier for developers to see than runtime dependencies, but sometimes runtime dependencies can be more flexible.
Compile time is the period when the programming code (such as C#, Java, C, Python) is converted to the machine code (i.e. binary code). Runtime is the period of time when a program is running and generally occurs after compile time.
Compile-time and Runtime are the two programming terms used in the software development. Compile-time is the time at which the source code is converted into an executable code while the run time is the time at which the executable code is started running.
A runtime dependency is an existing JAR file needed by your plug-in at execution. A runtime may need to be added to a plug-in for use by : Model Class: A model class may have imports from several external JAR files. Artifact Filter: A filter class may have imports from several external JAR files.
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 calling new
for some class, extending or implementing something (either directly or indirectly), or a method call using the direct reference.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With