Java - Compile (class file)

About

The compilation create a class source.class file from a java source file ie source.java file.

The create class language is called Java - Bytecode (classfile) and can be run on any OS with any java executable (ie Java - Java Virtual Machine (JVM|Java))

Type

javac utility

Basic

The javac utility permits to compile a the source file of a class

javac -sourcepath src -d build\classes src\com\gerardnico\HelloWorld.java

will create the file

build\classes\HelloWorld.class

To a specific java version

In order to to generate class files compatible with Java 1.4, use the following command line:

javac -source 1.4 HelloWorld.java

Code

Check

Get the compiler version

To get the version of the target Java image used by javac (ie the java version), you can use javap

From the root directory of your project (normally src/java), execute the following command

javap -verbose com.domain.classname | grep "major"
  major version: 52

where:

Java Version Major version
1.2 46
1.3 47
1.4 48
5 49
6 50
7 51
8 52
9 53
10 54
11 55

Library

the java compiler library is located at javax/tools/JavaCompiler. It is javax/tools/Tool (Before the implementation of module in Java 9, bundled in <JDK_HOME>/lib/tools.jar)

A tool is a common interface tools that can be invoked from a program.

Add the compiler as dependency (see Maven doc)

<dependency>
    <groupId>jdk.tools</groupId>
    <artifactId>jdk.tools</artifactId>
    <scope>system</scope>
    <systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>

Documentation / Reference


Powered by ComboStrap