Java and the Mac OS X Terminal This page is obsolete. From the Terminal, type the java command below. Machine:~/introcs/hello wayne$ java HelloWorld Hello, World If all goes well, you should see the output of the program - Hello, World. Input and Output. A Command Line Primer for Beginners. The How-To Geek. 9/09/10 12:00pm. Filed to: Command Line Filed to. Which is the default on Linux and Mac systems in the Terminal application.
The final step in writing your code is to define the main function in you HelloWorld class. The instructions inside the brackets of the main function will tell your computer what to do when it executes the program. For this program, you want the computer to print the words 'Hello World!' to your console. The java instruction
System.out.println(parameter)
will print the parameter of the instruction to the computer console. Inside the braces of the main function in the HelloWorld class, write the instruction
System.out.println('Hello World!');
Be sure the words that you want the program to print to your console are placed inside quotation marks. Place a semi-colon at the end of your instruction to show that the instruction on that line of code has ended. This step concludes the coding portion of the tutorial. Your program should look similar to the program in the image above. The indentations are not necessary for the functionality of your program, but they are recommended to make you code more readable.
In this section, our plan is to lead you into the world of Java programming by taking you through the three basic steps required to get a simple program running. As with any application, you need to be sure that Java is properly installed on your computer.You also need an editor and a terminal application. Here aresystem specific instructions for three popular home operating systems.[Mac OS X ·Windows ·Linux]
Programming in Java.
We break the process of programming in Java into three steps:
Create the program by typing it into a text editor andsaving it to a file named, say, MyProgram.java.
Compile it by typing 'javac MyProgram.java' in theterminal window.
Execute (or run) it by typing'java MyProgram' in the terminal window.
The first step creates the program; the second translates it into a languagemore suitable for machine execution (and puts the result in a file namedMyProgram.class); the third actually runs the program.
Creating a Java program.A program is nothing more than a sequence of characters, like a sentence, aparagraph, or a poem. To create one, we need only define that sequence charactersusing a text editor in the same way as we do for email.HelloWorld.java is an example program.Type these character into your text editor and save it into a filenamed HelloWorld.java.
Compiling a Java program.A compiler is an application that translates programs from theJava language to a language more suitable for executing on the computer.It takes a text file with the .java extension as input (yourprogram) and produces a file with a .class extension (the computer-language version). To compile HelloWorld.java typethe boldfaced text below at the terminal. (We use the % symbolto denote the command prompt, but it may appear different dependingon your system.)If you typed in the program correctly, you should see no error messages.Otherwise, go back and make sure you typed in the program exactly asit appears above.
Executing (or running) a Java program.Once you compile your program, you can execute it.This is the exciting part, where the computer follows your instructions.To run the HelloWorld program, type the following in the terminal window:
If all goes well, you should see the following response
Understanding a Java program.The key line with System.out.println() prints the text 'Hello, World'in the terminal window.When we begin to write more complicated programs, we will discuss themeaning of public,class, main,String[],args,System.out, and so on.
Creating your own Java program.For the time being, all of our programs will be just likeHelloWorld.java, except with a different sequence of statementsin main(). The easiest way to write such a program is to:
Copy HelloWorld.java into a new file whose name is theprogram name followed by .java.
Replace HelloWorld with the program name everywhere.
Replace the print statement by a sequence of statements.
Errors.
Most errors are easily fixed by carefully examining the program as wecreate it, in just the same way as we fix spelling and grammatical errors when we type an e-mail message.
Compile-time errors.These errors are caught by the system when we compile the program, because they prevent the compiler from doing the translation (so it issues an error message that tries to explain why).
Run-time errors.These errors are caught by the system when we execute the program, because the program triesto perform an invalid operation (e.g., division by zero).
Logical errors.These errors are (hopefully) caught by the programmer when we execute the programand it produces the wrong answer.Bugs are the bane of a programmer's existence.They can be subtle and very hard to find.
One of the very first skills that you will learn is to identify errors;one of the next will be to be sufficiently careful when coding to avoid many of them.
Input and output.
Typically, we want to provide input
to our programs:data that they can process to produce a result. The simplest way to provide input data is illustrated in UseArgument.java.Whenever this program is executed, it reads the command-line argument that you type after the program name and prints it back out to the terminal as part of the message.
Exercises
Write a program TenHelloWorlds.javathat prints 'Hello, World' ten times.
Modify UseArgument.java to make a programUseThree.java that takes three namesand prints out a proper sentence with the names in the reverse of the order given, so that for example,'java UseThree Alice Bob Carol'gives 'Hi Carol, Bob, and Alice.'.
Web Exercises
Write a program Initials.java thatprints your initials using nine rows of asterisks like the one below.
Describe what happens if, in HelloWorld.java, you omit
main
String
HelloWorld
System.out
println
Describe what happens if, in HelloWorld.java, you omit
the ;
the first '
the second '
the first {
the second {
the first }
the second }
Describe what happens if, in HelloWorld.java,you misspell (by, say, omitting the second letter)
main
String
HelloWorld
System.out
println
I typed in the following program. It compiles fine, but when I executeit, I get the error java.lang.NoSuchMethodError: main.What am I doing wrong?Answer: you forgot the String[] args. It isrequired.