Java Programming Setup

2012.04.05 14:12:15

So, since I'm starting a Java course over the internets, here's a blog entry to guide you through the setup steps to get ready for Java programming.

Alright, the setup is quite different depending on your operating system, so I divided into three categories, starting with windows, which most if not all of you will be using. There's a part at the end of this blog that everyone should follow though.

[b]WINDOWS[/size] We will be using the latest JDK1.6 version, which can be downloaded [url=http://www.oracle.com/technetwork/java/javase/downloads/jdk-6u31-download-1501634.html]here[/url]. JDK Stands for Java Development Kit. Some of you might know that there's already the JDK1.7 out, but since it's still a bit unstable, underdistributed and offers nothing new for us, we'll stay with good 'ol 1.6.

We won't be starting with an IDE out, and if you don't even know what that is, all the better. Next, download [url=http://download.tuxfamily.org/notepadplus/6.0/npp.6.0.Installer.exe]Notepad++[/url].

Install the two programs, but remember to copy the path to the JDK installation directory into your clipboard or well, just remember it. After both are installed, open the explorer and visit the JDK directory. In there should be a bin directory with a load of files and other stuff. Copy the path to this directory and open up the control panel. Set it to show all the things as icons, and open up "System". Click "Advanced System Settings". Go to "Environment Variables…". It should look something like this: [img]http://windows7update.s3.amazonaws.com/wp-content/uploads/2011/12/Microsoft-Windows-7-environment-variables.jpg[/img] In the lower list, search for "Path", select it and press "Edit…" A new popup should show and in the Value field, go to the absolute end of it with the cursor. Append a semicolon if there isn't one already. Next, paste in the path to the JDK bin directory. Confirm with ok and all the other dialogues as well.

What you did just now is tell windows to look in that directory for executables when using the command line. This allows us to use applications like javac and javaw from the command line, without having to type in the full path.

And… that's pretty much it! Now, skip on ahead to the GENERAL part for the setup.

[b]LINUX[/size] Here, you'll mostly have to check your distribution specific package manager. Nowadays, it's mostly an icedtea or openjdk package, since the Sun JDK has been abandoned. Search for the openjdk6 package or similar. Simply searching jdk or java might help as well. Otherwise, get a text editor of your choice. Novices might refer to gedit or kate, experienced people to emacs or vim.

And that's all already. The path should be updated automatically.

[b]MAC OS X[/size] For this, you'll have to either check your Mac OS X installation DVD or search for a JDK download online. I won't get into it, since I don't remember how to do it, but it should be fairly straightforward.

As for a text editor, TextWrangler is a good choice for now.

[b]GENERAL[/size] After installing your OS-specific JDK stuff and getting a neat editor, it's time to prepare our project folder and make sure everything's running well.

First, create a JAVA folder at a location of your choice. In here, we'll make subfolders for each project and everything else. Inside it, create a folder called IntroTest or something like that. Now, open up your text editor and paste the following into it: [code] public class Test{ public static void main(String[] args){ System.out.println("Hello to the world, the universe and everything beyond!"); } } [/code] You don't need to understand what it does at the moment, we'll get into it in the tutorial. Save this file as Test.java inside the IntroTest folder. Make absolutely sure to name it that way and nothing else, keep the capitalization and everything, or Java will complain. Once you got it saved, open up the command prompt or terminal. Windows V/7 users: Start menu, search for: cmd , hit enter. A black window with some text should appear. Linux users: Open your terminal emulator of choice. Mac OS X users: Open the Terminal app, which is somewhere in your applications folder.

What you're seeing now will in the future be referred to as the terminal. It's a purely text driven console, that allows you to do some very effective work though. Either way, here's some of the basic commands you need to remember: WINDOWS-SPECIFIC: dir Lists all files in the current directory. LINUX/MAC-SPECIFIC: ls Lists all files in the current directory. GENERAL: [code] cd [i]path[/i] Changes the current path you are in. The path can be read left to the cursor. javac [i]file.java[/i] Compiles the file you specified into a file.class that can be executed. java [i]file[/i] Runs the specified class file/program. [/code]

To the left of the blinking cursor you'll see some information as to A: Where you are and B: Who you are. This depends a bit on your setup, but usually you'll have the current path and your username displayed in the prompt. For linux or mac, let's assume it shows: linus@linuz: ~ % This means, I'm user linus on the machine linuz in the directory ~ (Which is my user home directory). Of course, your output might differ from this. For windows, it'll probably show something like: C:\Users\linus\> This means I'm currently on the path C:\Users\linus\

With cd, as mentioned before, you can switch the directory. When starting up the command prompt, you first always need to switch to our JAVA directory that we created earlier on. F.e. if you created it in your home directory, simply type [i]cd JAVA[/i] and hit enter. It should show something like ~/JAVA or C:\Users\linus\JAVA now. If you placed it in any other directory, switch to that. If you need to change to a directory above the current one, use [i]cd ..[/i] (yes, that's two dots.) In case you're lost, use [i]ls[/i] or [i]dir[/i] to give you a list of the files in your current folder, which might give you a clue. Once you're in the JAVA directory, type [i]cd IntroTest[/i] and then [i]javac Test.java[/i] . This should compile our file into something we can run. Once it's done, type in [i]java Test[/i], which should show [i]Hello to the world, the universe and everything beyond/i . If that all works, then you have successfully set up your environment and are ready to code! If not, check that you did everything right and ask me for help or write a comment here.

Written by shinmera