Thursday, 21 September 2023

How to get started with Java as quickly as possible

Apparently, Java 21 has some new things1 to make it easier to get started with Java2 right away.

So this is the ultimate blogpost of mine to get started quickly and easily.

Fair warning: I'm using the macos version of Java, so the directory structure might be a bit different compared to other Operating Systems.

Step 1. Download java

Go to https://jdk.java.net/21 and download the build you need.

Step 2. Unpack

Unpack the archive somewhere.

% tar zxvf Downloads/openjdk-21_macos-x64_bin.tar.gz

It will unpack in directory jdk-21.jdk.

Step 3. Write a small Java program

In this instance, we'll assume the file is called HelloWorld.java.

void main() {
  System.out.println("Hello, world.");
}

Step 4. Run the small Java program

% ~/jdk-21.jdk/Contents/Home/bin/java --enable-preview --source 21 ./HelloWorld.java
Note: ./HelloWorld.java uses preview features of Java SE 21.
Note: Recompile with -Xlint:preview for details.
Hello, world.

Congratulations, you've run your first Java Program!

The other way to do it, which admittedly is even quicker, is using the Java Playground3.

References

[1] JEP 445: Unnamed Classes and Instance Main Methods (Preview)
https://openjdk.org/jeps/445
[2] Paving the on-ramp
https://randomthoughtsonjavaprogramming.blogspot.com/2022/10/paving-on-ramp.html
[3] dev.java - Java Playground
https://dev.java/playground/

No comments:

Post a Comment