Programming Tips - Java: the basic Java commands / tutorial

Date: 2012aug26 Updated: 2024nov28 Level: beginner Language: mixed Q. Java: the basic Java commands / tutorial A. Install On Red Hat/Fedora/CentOS install with this command:
dnf install java java-devel
That will install
java-NN-openjdk java-NN-openjdk-devel
Where NN is the latest. Simple Program Lets make a simple program in file Hello.java:
public class Hello { public static void main(String []args) { System.out.println("Hello world"); } }
The convention is to begin classes with Uppercase and to make the filename match the class name. Compile and Run Now we can compile and run it with one commmand:
java Hello.java
Displays:
Hello world
Class Files But usually we want to make .class files:
javac Hello.java - compile source .java into .class file
You can run the .class file with:
java Hello - run .class file (omit ".class")
Displays:
Hello world
Jar Files .jar files are archives that hold multiple .class files To put our Hello.class into a jar:
jar --create --main-class=Hello --file hello.jar Hello.class
Run it with:
java -jar hello.jar
Displays:
Hello world
Now, you're well on your way!