编译时与运行时依赖性 - Java

发布于 2024-10-03 21:21:01 字数 50 浏览 8 评论 0原文

Java 中的编译时依赖和运行时依赖有什么区别? 它与类路径有关,但它们有什么区别呢?

What is the difference between compile time and run time dependencies in Java?
It is related to class path, but how do they differ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

最偏执的依靠 2024-10-10 21:21:01
  • 编译时依赖项:您需要 CLASSPATH 中的依赖项来编译您的工件。它们的产生是因为您对代码中硬编码的依赖项有某种“引用”,例如为某个类调用 new 、扩展或实现某些内容(直接或间接)或方法调用使用直接 reference.method() 表示法。

  • 运行时依赖项:您需要 CLASSPATH 中的依赖项来运行您的工件。它们的产生是因为您执行访问依赖项的代码(以硬编码方式或通过反射或其他方式)。

尽管编译时依赖项通常意味着运行时依赖项,但您可以拥有仅编译时依赖项。这是基于以下事实:Java 仅在第一次访问该类时链接类依赖项,因此,如果由于从未遍历代码路径而在运行时从未访问特定类,Java 将忽略该类及其依赖项。

示例

在 C.java(生成 C.class)中:

package dependencies;
public class C { }

在 A.java(生成 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());
        }
    }
}

在本例中,AC 有编译时依赖code> 到 B,但如果在执行 java dependency.A 时传递一些参数,它只会对 C 产生运行时依赖,因为 JVM 只会尝试解决 B 在执行 B b = new B() 时对 C 的依赖。此功能允许您在运行时仅提供代码路径中使用的类的依赖项,并忽略工件中其余类的依赖项。

  • 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.

浮萍、无处依 2024-10-10 21:21:01

编译器需要正确的类路径才能编译对库的调用(编译时依赖项)

JVM 需要正确的类路径才能加载您正在调用的库中的类(运行时依赖项)。

它们可能在几个方面有所不同:

1) 如果类 C1 调用库类 L1,而 L1 调用库类 L2,则 C1 对 L1 和 L2 具有运行时依赖关系,但仅对 L1 具有编译时依赖关系。

2) 如果类 C1 使用 Class.forName() 或其他机制动态实例化接口 I1,并且接口 I1 的实现类是类 L1,则 C1 对 I1 和 L1 有运行时依赖,但只有编译时依赖在 I1 上。

其他“间接”依赖关系对于编译时和运行时是相同的:

3)您的类 C1 扩展了库类 L1,并且 L1 实现了接口 I1 并扩展了库类 L2:C1 对 L1、L2 有编译时依赖项,和I1。

4)你的类C1有一个方法foo(I1 i1)和一个方法bar(L1 l1),其中I1是一个接口,L1是一个带有参数的类,是接口 I1:C1 对 I1 和 L1 具有编译时依赖性。

基本上,要执行任何有趣的操作,您的类需要与类路径中的其他类和接口进行交互。由该组库接口形成的类/接口图产生编译时依赖链。库实现产生运行时依赖链。请注意,运行时依赖链是运行时依赖的或失败缓慢的:如果 L1 的实现有时依赖于实例化L2 类的对象,并且该类仅在一种特定场景中实例化,那么除了该场景之外没有任何依赖关系。

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.

无声无音无过去 2024-10-10 21:21:01

一个简单的例子是查看 servlet api 之类的 api。要编译您的 servlet,您需要 servlet-api.jar,但在运行时 servlet 容器提供了 servlet api 实现,因此您无需将 servlet-api.jar 添加到运行时类路径。

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.

多情癖 2024-10-10 21:21:01

Java 在编译时实际上并不链接任何东西。它仅使用在 CLASSPATH 中找到的匹配类来验证语法。直到运行时,所有内容才被组合在一起并根据当时的 CLASSPATH 执行。

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.

装纯掩盖桑 2024-10-10 21:21:01

编译时依赖项只是您在正在编译的类中直接使用的依赖项(其他类)。运行时依赖关系涵盖您正在运行的类的直接依赖关系和间接依赖关系。因此,运行时依赖项包括依赖项的依赖项和任何反射依赖项,例如在 String 中拥有但在 Class#forName() 中使用的类名。

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().

趴在窗边数星星i 2024-10-10 21:21:01

对于 Java,编译时依赖就是源代码的依赖。例如,如果类 A 调用类 B 的方法,则 A 在编译时依赖于 B,因为 A 必须知道要编译的 B(B 的类型)。这里的技巧应该是这样的:编译后的代码还不是完整的可执行代码。它包括尚未编译或存在于外部 jar 中的源的可替换地址(符号、元数据)。在链接期间,这些地址必须替换为内存中的实际地址。为了正确地做到这一点,应该创建正确的符号/地址。这可以通过类(B)的类型来完成。我相信这是编译时的主要依赖项。

运行时依赖性与实际的控制流更相关。它涉及实际的内存地址。这是程序运行时所具有的依赖关系。您在这里需要 B 类详细信息,例如实现,而不仅仅是类型信息。如果该类不存在,那么您将得到 RuntimeException 并且 JVM 将退出。

通常且不应该,这两种依赖关系都流向相同的方向。但这是面向对象设计的问题。

在 C++ 中,编译有点不同(不是即时编译),但它也有一个链接器。所以我猜这个过程可能与 Java 类似。

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.

尐籹人 2024-10-10 21:21:01

从@Jason S的回答中,我用其他词得出我的答案,以防万一有帮助:

应用程序的运行时依赖实际上是编译时依赖(L1)的依赖(让我们称之为L2)这个应用程序。如果应用程序不使用它,则可能不会将其声明为依赖项。

  • 如果 L2 恰好被应用程序使用(通过 L1),但未声明为依赖项,则会出现 NoClassDefFoundError。

  • 如果 L2 被声明为应用程序的编译时依赖项,并且不在运行时使用,则它会毫无用处地使 jar 变得更大,并且编译时间会比所需的时间更长。

将 L2 声明为运行时依赖项允许 JVM 仅在需要时才延迟加载它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文