How to create your first Java program (Wiki forum at Coderanch) (original) (raw)

Downloading the Java SDK

Installing the Java SDK

These instructions will assume that you are installing the JDK1.8.0_172, which was the most recent “LTS” (=Long Term Support) version at the time of this update. It is recommended to leave the version number in the install directory (e.g. 1.8.0_172). This will allow you to install several versions of the jdk under the same root directory (e.g. C:\java), and to easily recognize which version of the JDK lies in which directory.

Windows

Linux

Ubuntu Users please see below.

Other Flavours of Linux: You have to choose whether you want to use a zipped (.tar.gz) file or use an RPM package. Older versions of Java (up to Java 6) used a self-extracting binary file rather than a compressed file; instructions for installing that have been removed from this document. If your system is RPM-based, you may wish to use the RPM package instead of the binary file.

Compressed tar file (Java7/8/9/10)

RPM Package

Ubuntu

Java should already be installed in your system, but the JDK used should be Open JDK, not Oracle JDK. If you wish to use the Oracle JDK, try the repository available from webupd8team. The instructions on the website are clear. That installation method appears to use the same technique shown above, of downloading a .tar.gz file and unzipping it, but it is automated. Test the installation by giving the following two instructions to the terminal:

java -version

javac -version

If both those give the up-to-date version number, then the Java installation is already in your PATH and there is no need to edit the PATH as shown in the next section. You can of course install Java the same way as for all other flavours of Linux if you prefer.

Tip: In the JDK install directory, there should be a file called src.zip. It contains all sources of the Java API. You might be interested to have a look at them someday !

Setting the JAVA_HOME and PATH environment variables

You will probably have to open a new command line or terminal window to use the updated PATH; the old window usually uses the old PATH environment variable.

Windows XP

Windows Vista / Windows 7/8/10

Linux

The directions vary depending which shell your computer uses, but most people use the "bash" shell. This is how to alter a PATH variable on "bash". Note the PATH variable already exists, so it is only necessary to add the PATH to one's Java installation.

If you prefer, you can set a new temporary PATH by writing an instruction similar to this at a terminal: "export PATH=/usr/java/jdk1.7.0_79/bin:$PATH". That will update your PATH for that instance of the terminal, so you can use a different JDK from normal.

Mac OS

See Easton Hathaway's Blog The procedure is quite similar to that on Linux. You can see an example of an OS/X Java PATH in this post in our Mac Forum

What is the purpose of these variables ?

The JAVA_HOME variable is used by other programs to know where your JDK is. Most IDEs will use it. It is also used as a convenient shortcut, to avoid having to change the other places referring to the JDK install directory. For example, we have used it to set the PATH variable (%JAVA_HOME%). This is very convenient, because if you install another version of the JDK, all you need to do is to update your JAVA_HOME, without having to touch the PATH.

But don't set JAVA_HOME as a user variable and use it in a system PATH (see discussion here).

The PATH variable is used by your system command prompt. When you execute a program, your system will look for its location. It will scan all directories included in the PATH, and look for the program you are executing. This means that when you type "java", or "javac" at the command prompt, your system will look into %JAVA_HOME%\bin", find the command and execute it. If you don't set your PATH properly, you'll end up with an error message like "Command not found".

Checking that your JDK is configured properly

Open the command prompt, go to a directory different from your JDK install directory, and type "javac -version". This should display the version of your SDK. Make sure it is correct. If an error like "Command not found" is displayed, make sure you have correctly set your PATH. Check the environment variables settings again. If the version displayed is not the same as the JDK you have installed, another installed JDK may conflict. Make sure you have set your JDK directory at the beginning of the PATH variable.

Creating your first Java program

This is where the fun begins ! Let's create a simple greetings class. Use your favorite text editor to create a new file, not a word processor. You don't need an IDE like Eclipse or IntelliJ yet. For the moment, stick to the editor you're used to. Copy/paste the following code in your editor :

Save the file in the directory of your choice. But do not save it under the JDK install directory. Instead, save it in a directory where you'd like to keep your java sources in. For example, C:\java\src. Name the file MooseGreetings.java. Be extra careful with the uppercase and lowercase. The filename must match exactly the name of your class. mOoSegReeTings.java won't work. Also be careful that your editor does not append the ".txt" extension to your file. Some filthy editors like Windows Notepad may do it, but Notepad is probably not your favorite editor !

Compiling and executing the program

(For Mac-specific instructions, go to https://coderanch.com/t/111632/Mac/JAVA-MAC-OS)

At the command prompt, go to the directory where you have saved the MooseGreetings.java file. Type "javac MooseGreetings.java", and press the enter key.

The class should be compiled, and a new file called MooseGreetings.class should be created in the same directory. If a compiler error happens, make sure you copy/pasted the above code properly. If the file is not found, make sure you're in the right directory, and that your file is properly named MooseGreetings.java. If you can't figure out why it does not compile, ask at the Beginning Java forum.

Let's execute the program. In the same directory, type the following command : "java -classpath . MooseGreetings".

Greetings for the Javaranch Moose should be printed : mooooooooooo.

That's it ! You've successfully executed your first Java program. You're now ready to get into the Java world ! If you have any questions, some friendly folks will be happy to help you at the Beginning Java forum.

What's that fancy -classpath flag used when executing a Java program ?

The -classpath flag (or -cp flag) may be used at the command prompt to tell Java where to look for .class files. Your class files may be in directories, or in JAR files. These can be set in the classpath either by setting the CLASSPATH environment variable, or by using the -classpath flag. Details about setting the CLASSPATH are in the HowToSetTheClasspath FAQ. In our example, "-classpath ." means that Java should scan the current directory (".") for class files. If you don't have a system CLASSPATH set on your computer, you can omit

-classpath .

from the instruction.