Running Java by Hand - StefanSchade/Java-Core-JVM-Basics GitHub Wiki

Normally the IDE masks a lot of what’s going on, so here is an "hello world" kind of run through the compile process of a java program.

Prerequisites

Compile Process in Theory

Step 1: Compile

Input: Source files in a variety of languages

  • Java (.java)

  • Groovy (.groovy)

  • Scala (.scala)

  • Kotlin (.kt)

Processing: javac Compile source to bytecode

Output: Platform independend bytecode

  • Class files (.class)

These classfiles can run on the Java Runtime environment or be packaged into libraries

Step 2: Packagage

Input: many .class files that make up one application (many might mean: thousands)

Processing: Combine files into a single file using ZIP Compression

Output: *.jar, *.war, *.ear

  1. Overview over Java archive types

File Ext:

Name:

contains:

*.jar

Java Archive

ZIP file containig class files, typically not a complete application

*.war

Web Application Archive

can include other jar files, webresources, and more, complete web application

*.ear

Enterprise Archive

contains one or more war files meant to be deployed to an application server (Tomcat, Webboss, Websphere)

FAT JAR

aka. Uber JAR

executable jar that contains all dependencies including embeded application server, used by Spring

Docker Container

Image contains Runtime Environment, JVM, Java Package (Fat Jar) deployment entity

Step 3: Run

Input: Bytecode as class file, of forms

  • Classfiles (.class)

  • Jar (.jar)

Processing: java

  • necessary class files and JARs must be in classpath (either specified as environment variable or comandline argument)

  • main class must be called explicitly

"Hello World" example in Detail

HelloWorld
  • generate source file "HelloWorld.java" with a main routine

  • compile: javac HelloWorld.java

  • run: java Helloworld

Now create a JAR containing HelloWorld

  • create jar: jar cf helloworld.jar HelloWorld.class

  • run jar: java -classpath helloworld.jar HelloWorld

⚠️ **GitHub.com Fallback** ⚠️