Differences between revisions 3 and 4
Revision 3 as of 2018-08-04 10:03:29
Size: 3130
Editor: ?MarkusKoschany
Comment:
Revision 4 as of 2018-08-05 04:48:38
Size: 3129
Editor: ?MarkusKoschany
Comment:
Deletions are marked like this. Additions are marked like this.
Line 26: Line 26:
and compile your source file into Java byte code (*.class) with the javac compiler. and compile your source file into Java bytecode (*.class) with the javac compiler.

Translation(s): English

Java 1x1

HelloWorld.java

   1 public class HelloWorld {
   2   
   3     public static void main(String[] args) {
   4         System.out.println("Hello World!");
   5     }
   6 
   7 }

To compile your little Hello World program install the Java Virtual Machine provided by OpenJDK.

apt install default-jdk

and compile your source file into Java bytecode (*.class) with the javac compiler.

javac HelloWorld.java

The output will be ?HelloWorld.class. Now the program can be run with

java HelloWorld

and the output will be Hello World!

A bit more complex example

Now consider a bit more complex example. Download the archive on your computer and extract all files.

Compile the two *.java files in the compute directory first.

 javac compute/Compute.java compute/Task.java

Again the source files are compiled into the compute directory as Java bytecode. It is common practice in Java to move multiple class files into a jar archive which can be easily transferred to other computers. A jar file is just an archive in zip-format which can include class files but also resource files like images and meta information stored in a so called MANIFEST file.

jar cvf compute.jar compute/*.class

A default MANIFEST is automatically created. It can be used for signing packages, version control, defining an entry point to your application via a Main-Class entry and for declaring the CLASSPATH.

The CLASSPATH

Java libraries/classes can be found by a mechanism called class loading. Somehow the JVM must know about them. The CLASSPATH is a way to define the path to those class files. Our compute.jar file is required for building the source files in the engine directory. It can be freely moved around, e.g. to a directory called /home/user/examples.

javac -cp /home/user/examples/compute.jar engine/*.java

By using the -cp switch the Java compiler will know where the required classes are located to compile the engine package. You could also modify the MANIFEST file and create a Class-Path entry like this one

Class-Path: /home/user/example/compute.jar

In both cases the Java source files will be successfully compiled into Java class files.

The concept of CLASSPATH and MANIFEST files is very important when it comes to packaging Java applications and libraries for Debian. There are other solutions for classloading known as the OSGi framework where jar files are turned into so called bundles with the help of Debian packages like BND and libmaven-bundle-plugin-java. Also Java 9 introduced the new Java Module System project Jigsaw which is supposed to overcome the limitations of the CLASSPATH concept.

With these terms in mind, let's move to some real-life examples.