Getting started with Java

Prerequisites

  • None

Steps

First off I am assuming 0 requirements so lets go out and download a text editor to write our first Java application in. I suggest Sublime 3 to start with which can be downloaded HERE and simply select your OS.

These next 2 steps are specific to windows.

  • I would recommend downloading CYGWIN because dealing with the windows terminal is pretty painful
  • Go through the installation process of Cygwin

Next up lets download the JDK (Java Developer Kit). Accept, read and then check agree to the Java SE Development Kit 8u102 and download it onto your computer.

Install the JDK by simple following the dialog.

Now that you have that all installed lets actually put your first JAVA file together.

Open up sublime and create a new file called HelloWorld.java

Inside HelloWorld.java include the following code:

class HelloWorld {
 public static void main(String[] args) {
 System.out.println("Hello World!"); // Display the string.
 }
}

Save that file up then open up cygwin.

So you are going to have to compile the HelloWorld.java file using javac HelloWorld.java. First lets make sure that you have the command ‘javac’ so type in ‘javac’ and click enter and if you get an error stating that the ‘javac’ command cant be found you need to add it to your PATH. You do this on windows by:

  • Navigate to /usr/bin via ‘cd usr/bin’ and creating a symbolic link to javac via
  • ln -s /cygdrive/c/Program\ Files/Java/jdk.1.8.0_102/bin/javac

or on mac by

  • opening up/creating a .bashrc file in ‘~/’ directory and adding in
  • export PATH="$PATH:<Whatever the path to the jdk.1.8.0_102/bin directory is>";
  • Restart cygwin

Next we can now compile the HelloWorld.java file by navigating to it on cygwin

ex: ‘/cygdrive/c/Users/BPurdy/Desktop/projects’

and executing javac HelloWorld.java.

This should create a HelloWorld.class file (which we can now execute)

Execute this file by running

java HelloWorld

and it will print out HelloWorld to the console!

 

Well done, you have now installed/compiles/and executed your first java application =). Hope this guild helped.

 

Advertisements