## page was renamed from Java/Packaging/Java1x1 #language en ~-[[DebianWiki/EditorGuide#translation|Translation(s)]]: English -~ <> = Java 101 = == HelloWorld.java == {{{#!highlight java public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } }}} To compile your little Hello World program install the [[https://en.wikipedia.org/wiki/Java_virtual_machine | Java Virtual Machine]] provided by [[https://en.wikipedia.org/wiki/OpenJDK | 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 [[https://people.debian.org/~apo/java/java_example.tar.gz | 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 [[https://en.wikipedia.org/wiki/OSGi | OSGi]] framework where jar files are turned into so called bundles with the help of Debian packages like [[DebianPkg:bnd|BND]] and [[DebianPkg:libmaven-bundle-plugin-java|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. [[https://wiki.debian.org/Java/Packaging/NoBuildSystem | NoBuildSystem]], [[https://wiki.debian.org/Java/Packaging/Ant | Ant]], [[https://wiki.debian.org/Java/Packaging/Maven | Maven]], [[https://wiki.debian.org/Java/Packaging/Gradle | Gradle]]