How Java linker works?
Asked Answered
I

5

22

I want to know how Java linker works. Specifically, in which order it combines classes, interfaces, packages, methods and etc into jvm-executable format. I have found some information here, but there is not so much information about linking order.

Immensurable answered 22/6, 2011 at 13:2 Comment(2)
What do mean by order? From the first sentence on that page: "The Java virtual machine dynamically loads , links, and initializes classes and interfaces." By inference, this is done on-demand in most JVMs.Bayle
More to the point, what do you mean by 'linker'? There isn't any such thing in Java.Agan
H
23

There is no such thing as a Java "linker". There is, however, the concept of a classloader which - given an array of java byte codes from "somewhere" - can create an internal representation of a Class which can then be used with new etc.

In this scenario interfaces are just special classes. Methods and fields are available when the class has been loaded.

Hyacinthhyacintha answered 22/6, 2011 at 13:6 Comment(8)
That's a good point. If you want to understand this, how classloaders work is a fundamental concept.Heliostat
The OP links (heh) to a part of the JVM spec that explicitly mentions linking as part of the activities the JVM performs after loading a classEvidently
of course Java as a "linker". Even if the JLS hadn't specifically named the process "linking" (see JLS 5.4 Linking) there would still the "concept" (to reuse a word you like) of a linker.Creamery
@SyntaxT3rr0r, no need to be snide. Feel free to write a better answer.Stretch
Strictly speaking the JVM does not link. Except if you want to name JIT compiling linking.Bautram
@MichaelBorgwardt The wording in the question resembles the C linker which (at least used to be) a completely separate step creating new files on disk. The JVM does not do this as a separate step but in memory on the fly.Stretch
Years later we now have jlink docs.oracle.com/javase/9/tools/jlink.htm Does this question now have a different answer?Korenblat
@RyanTheLeach Not really. The question is about combination of stuff into a jvm-executable format. That is not the job of jlink.Stretch
E
15

First of all: methods are always part of a class. Interfaces are basically just special classes, and packages are just a part of the fully qualified name of a class with some impact on visibility and the physical organization of class files.

So the question comes down to: how does a JVM link class files? The JVM spec you linked to says:

The Java programming language allows an implementation flexibility as to when linking activities (and, because of recursion, loading) take place, provided that the semantics of the language are respected, that a class or interface is completely verified and prepared before it is initialized, and that errors detected during linkage are thrown at a point in the program where some action is taken by the program that might require linkage to the class or interface involved in the error.

For example, an implementation may choose to resolve each symbolic reference in a class or interface individually, only when it is used (lazy or late resolution), or to resolve them all at once, for example, while the class is being verified (static resolution). This means that the resolution process may continue, in some implementations, after a class or interface has been initialized.

Thus, the question can only be answered for a specific JVM implementation.

Furthermore, it should never make a difference in the behaviour of Java programs, except possibly for the exact point where linking errors result in runtime Error instances being thrown.

Evidently answered 22/6, 2011 at 13:12 Comment(0)
H
10

Java doesn't do linking the way C does. The principle unit is the class definition. A lot of the matching of a class reference to its definition happens at runtime. So you could compile a class against one version of a library, but provide another version at runtime. If the relevant signatures match, everything will be ok. There's some in-lining of constants at compile time, but that's about it.

Heliostat answered 22/6, 2011 at 13:8 Comment(0)
P
5

As noted previously Java compiler doesn't have a linker. However, JVM has a linking phase, which performed after class loading. JVM spec defines it at best:

Linking a class or interface involves verifying and preparing that class or interface, its direct superclass, its direct superinterfaces, and its element type (if it is an array type), if necessary. Resolution of symbolic references in the class or interface is an optional part of linking.

This specification allows an implementation flexibility as to when linking activities (and, because of recursion, loading) take place, provided that all of the following properties are maintained:

  • A class or interface is completely loaded before it is linked.

  • A class or interface is completely verified and prepared before it is initialized.

  • Errors detected during linkage are thrown at a point in the program where some action is taken by the program that might, directly or indirectly, require linkage to the class or interface involved in the error.

https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-5.html#jvms-5.4

Poolroom answered 2/1, 2016 at 14:31 Comment(0)
D
2

Linking is one of the three activities performed by ClassLoaders. It includes verification, preparation, and (optionally) resolution.

Verification : It ensures the correctness of .class file i.e. it check whether this file is properly formatted and generated by valid compiler or not. If verification fails, we get run-time exception java.lang.VerifyError.

Preparation : JVM allocates memory for class variables and initializing the memory to default values.

Resolution : It is the process of replacing symbolic references from the type with direct references. It is done by searching into method area to locate the referenced entity.

Dispossess answered 8/7, 2020 at 5:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.