CS-151 Labs > Lab 0. Setup
Warmup
In this part you will compile and run your first Java program.
Right-click in the middle of the screen, and select “Open in Terminal” from the context menu. This will open the Terminal application. Terminal is used for giving commands to the operating system: for example, we can manipulate files and folders from the Terminal. If you plan to use Terminal in the future, you can pin it to the task bar on the left. To do so, right-click on the Terminal icon on the taskbar, and select Add to favorites from the drop-down menu.
When you open Terminal, you end up in the Desktop directory.
If you ever get lost and don’t know where you are, you can execute command cd
(change directory) without any arguments, and you will end up in your home directory by default.
In your home directory, create a sub-directory for this course: mkdir cs151
.
Store all your future cs151 class work in this directory. Make directory for warmup 0 inside cs151.
Here are the commands:
% cd
% mkdir cs151
% cd cs151
% mkdir warmup0
% mkdir lab0
Always create the warmup and the lab folder for all the future labs, to keep your work organized.
Now change directory (cd
) to the warmup0
folder.
% cd warmup0
First, you will learn how to create, compile and run Java programs from command line. To be able to program in Java, at a minimum, you need a text editor and the JRE – Java Runtime Environment. These are already installed on lab computers.
Download the demo/starter files Hello.java and Hello2.java (right-click-save as…), and store them in the warmup0
directory.
Simple Program
The Java source code can be stored in a regular text file with extension .java, and then converted into a byte code using command javac
(java compile).
Let’s use gedit application as the text editor. You can start gedit by typing:
% gedit &
The ampersand at the end of the command tells to the operating system that we want to start gedit in the background. That means that once gedit starts, we can continue working in the terminal: terminal and gedit will be two completely separate processes.
Open Hello.java
in gedit, and read the code. Note that any Java code must reside inside a class. The class name must be identical to the name of the .java file. We use multiple classes for more complex projects. However any project must have an entry point – where the execution begins. The entry-point instructions are written in a special method main
, which must be declared as void
, public
, and static
. Currently our main
contains a single instruction: the system just prints a message to the screen.
Compile the code:
% javac Hello.java
To see the content of the current folder use command ls
(list files).
% ls
Hello.class Hello.java Hello2.java
After compilation we have a new file in our directory: Hello.class.
This file contains the byte code of our program.
Unlike programs written in fully-compiled languages (such as C or C++), Java programs are not converted into a machine code – the only language that machines understand. Instead, the byte code in Hello.class
– generated by javac
command – is loaded into Java Virtual Machine (JVM) and executed. The advantage is that the same byte code can be executed on different operating systems, each with their own JVM.
To run the code:
% java Hello
Welcome to CS-151!
Congratulations! You just compiled and executed your first Java program.
Program Arguments
Each Java program can be executed with additional parameters which we list on the command line next to the name of the program itself. In our code, we can access these command-line arguments via String array args
– the parameter of the main
method.
Open Hello2.java
in text editor. With your partner, read the code in Hello2.java
. What is this program doing?
Then repeat the compile/run cycle for Hello2.java
.
Try to run it with the following command-line arguments:
% java Hello2
% java Hello2 Alice Bob
What if we want to print personal welcome messages to our two famous friends Steve Jobs and Bill Gates?
Would java Hello2 Steve Jobs Bill Gates
work? Try it out.
The program considers each space as a separator. In order to force the program to treat several tokens as a single argument, you need to surround them with quotation marks, like this:
% java Hello2 "Steve Jobs" "Bill Gates"
Now you know how to compile and run simple Java programs from the command line.