Do I need to compile Java for bytecode?

Does the Java language specification indicate that Java is compiled into Java bytecode?

From what I understand, this is not so:

JLS 1

Compilation time usually translates programs into machine-independent bytecode [view.

[...]

The Java programming language is usually compiled for the bytecode command set and the binary format defined in the Java Virtual Machine Specification, Java SE 9.

(my emphasis)

I cannot find any other references to "bytecode" or "bytecode" in the specification.

Does this mean that all bytecode manipulations are not technically covered by the "Java language" as defined by JLS, and technically rely on implementation details?

+5
source share
2 answers

You have noticed that the term β€œusually,” as well as the lack of a description of byte code in JLS, is intended to define the Java programming language as independent of the runtime environment as possible. However, it is not so simple:

Communication with predefined classes and interfaces

As noted above, this specification often refers to the Java SE platform API classes. In particular, some classes have a special relationship with the Java programming language. Examples include classes such as Object , Class , ClassLoader , String , Thread , and classes and interfaces in the java.lang.reflect , among others. This specification restricts the behavior of such classes and interfaces, but does not provide a complete specification for them. The reader refers to the Java SE platform API documentation.

Therefore, this specification does not describe the reflection in detail. Many linguistic constructs have analogues in the Core Reflection API ( java.lang.reflect ) and language model API ( javax.lang.model ), but they are not usually discussed here. For example, when we list ways to create an object, we usually don’t include the ways in which the Core Reflection API can do this. Readers should be aware of these additional mechanisms, even if they are not mentioned in the text.

Thus, the Java programming language is larger than JLS, as well as the Java SE platform API. And there, we have the defineClass methods of the mentioned defineClass class that accept input in the format of the class file. Therefore, even if we use other deployment methods than class files in bytecode format, a fully compatible environment should support this format in this place. Note that Java 9 introduced another method that accepts input in the class file format , which does not even require Reflection or the implementation of custom class loaders.

This excludes JavaME, which does not have these API artifacts mentioned by JLS, otherwise we already had an example Java environment that does not support bytecode manipulation.

But this still does not fully answer the question of whether bytecode manipulation outside the language works, speaking of JavaSE or EE. Even if the support for the bytecode format is provided by the standard API, the control of the bytecode depends on the implementation details, either the Instrumentation API, which is optional, or processes the compiled class files in their expanded form as a file hierarchy, jar files, or module files, and it is not guaranteed that this will be a deployed form of the application (as said at the beginning). Thus, it is actually impossible to implement a bytecode manipulation tool that is guaranteed to work with all possible Java environments, although you will need to make longer lengths to create an environment that is fully compatible, but does not work with these tools ...

+4
source

JSL does not need to know how the JVM will read it; it describes only the Java language. The JAVAC compiler provided in the JDK makes a link, but is not part of the language itself

The Oracle JDK software contains a compiler from source code written in the Java programming language to the Java virtual machine command set

In the Java β„’ programming language compiler , we can find the same explanation:

The Java programming language compiler, javac, reads source files written in the Java programming language and compiles them into bytecode class files. If desired, the compiler can also process annotations found in source and class files using the Pluging Annotation Processing API. The compiler is a command line tool, but can also be invoked using the Java Compiler API. The compiler takes the source code defined by the Java Language Specification (JLS) and creates class files defined by the Java Virtual Machine Specification (JVMS) .

Therefore, the JAVAC team is basically a bridge between specifications.

  • Input: a.java describe in JLS
  • Exit: a.class descrive in the JVMS.

You can find some information by checking out the Jave Virtual Machine Specification .

The Java virtual machine does not know anything about the Java programming language, only about a specific binary format, a class file format. The class file contains Java virtual machine instructions (or bytecodes) and a character table, as well as other supporting information.

(I would like to find the opposite statement that the Java language does not know anything about the language of the Java virtual machine ...)

Later in these specifications, we found additional information about the format of the class and how it was translated into this list of instructions.

+1
source

Source: https://habr.com/ru/post/1273736/


All Articles