An old tradition holds that one’s first encounter with a new programming language should begin with writing a program to announce Hello, world
. In Java, the program looks like this:
class Greeter { public static void main(String[] args) { System.out.println("Hello, world"); } }
You can run Java programs online at a variety of places. For now, just go to the Java runner at Tio.run and enter and run the program above into the “Code” box. You should see something like this:
Online runners are great for trying things out, but to build large systems, you will need to build programs on your own computer. To develop Java programs, you will need to download and install a Java Development Kit (JDK).
Download and install now, if you haven’t already. The actual installation process will vary from machine to machine, so do your best to follow whatever instructions you can find to complete the installation process. Don’t be afraid to ask for help.
Once a JDK is downloaded and installed, we can write programs and run them from the command line. First, let’s make sure your download went okay and that the main tools are installed. On the command line, enter these two commands:
javac -version
java -version
jshell -version
These commands will show the version of Java installed on your box, something like this:
$ javac -version javac 17 $ java -version java version "17" 2021-09-14 LTS Java(TM) SE Runtime Environment (build 17+35-LTS-2724) $ jshell -version jshell 17
Stay up to DateYou should have at least version 17 for these tools. If you have anything older, update!
Now, make a new folder for writing our first programs. In this new folder, create a file called Greeter.java containing the Java program above. The class name Greeter
has to be the same as the first part of the filename Greeter.java. (Those are the rules, people.) Once you have the file, you will first compile then run the program:
$ javac Greeter.java $ java Greeter Hello, world
You can run both commands on the same line like this:
$ javac Greeter.java && java Greeter Hello, world
Recall that &&
means run the first command, and then, only if the first command succeeded, run the second.
What exactly is going on here? javac
is the command to compile Java source code files. It reads .java
files and produces .class
files.
java
is the command to run class files. Give this command the name of a class file (together with any command line arguments) and the command will run the program starting at the public static void main
method.
javac
. Locate the Greeter.class
file.
Do I really have to do both javac and java?If your entire program fits in just one
.java
file, then no. For our program above, we could have just writtenjava Greeter.javaHowever, most real life programs have multiple files, so that short cut does not work: you will have tojavac
each one of them. So you might as well get in the habit of running both commands.Now of course your development environment will probably just have a button to just run the whole giant project; however, the command line will always be there, so get used to it too.
It’s most helpful to use a modern editor like Code, in which you should install some good packages that can check and clean your code while you type (or whenever you save a file). Please install these packages; there is no pride in suffering through the discovery of typos through strange error messages in a console! Let your editor mark whatever problems it can, so you can fix trivial problems without leaving the editor. A good Java package can also give you type-ahead suggestions, which saves sooooooo much time.
The instructor and TAs will help you configure your environment during class. We will be using Visual Studio Code with: Extension Pack for Java and SonarLint.
Let’s modify the program so that if a command line argument is present, we’ll assume it is a name to greet. If no argument is given, we’ll just say hello to the world. Command line arguments are passed to your program in the array argument to main
:
class Greeter { public static void main(String[] args) { if (args.length == 0) { System.out.println("Hello, world"); } else if (args.length == 1) { System.out.println("Hello, " + args[0]); } else { System.err.println("Program requires either zero or one argument"); } } }
Let’s try it out:
$ javac Greeter.java && java Greeter
Hello, world
$ javac Greeter.java && java Greeter Alice
Hello, Alice
$ javac Greeter.java && java Greeter Jane Doe
Program requires either zero or one argument
$ javac Greeter.java && java Greeter 'Jane Doe'
Hello, Jane Doe
In addition to the javac
and java
commands, the JDK has a wonderful little tool called jshell
, which lets you run little snippets of Java; it’s amazing for learning and experimenting. Even the most advanced Java programmers will “drop into the shell” to try out new things are see if their hunches are correct. We’ll work with this extensively in class, but for now, here’s a taste (don’t worry about understanding every line for now):
$ jshell | Welcome to JShell -- Version 17 | For an introduction type: /help intro jshell> 8 - 2 * 5 $1 ==> -2 jshell> "Hello".startsWith("Hell") $2 ==> true jshell> int triple(int x) { ...> return x * 3; ...> } | created method triple(int) jshell> triple(30) $4 ==> 90 jshell> $4 * 2 $5 ==> 180 jshell> IntStream.range(1, 5).forEach(System.out::println) 1 2 3 4 jshell> /exit | Goodbye
We’ve covered: