Wednesday, February 10, 2010

How to set up Java on my computer

http://www.apl.jhu.edu/~hall/java/beginner/settingup.html

How to set up Java on my computer

The very first thing to do is install the Java Developer's Kit (jdk) onto your computer. Sometimes if you buy a Java book it comes on a CD with the book. However, usually you have to go to Sun's Java site, in order to download it. For simplicity, you can go to Downloading compilers where it will explain to you how and what kind of Java Developer's Kit to download. After you have downloaded the jdk you will have to follow their simple installation instructions. There are two ways to run a program. The basic way is to click on the start button on the bottom of the Windows desktop, click "run" and type in "CMD". If you did not set the path (see next paragraph how) you will then have to find the bin directory. In my computer I typed "cd.." until I reached C:\>, I found my Java directory (on my computer it was j2sdk1.4.1, but may be different on yours), and then the bin directory. If you have set the path, then you can run Java in any folder that you want. I then typed "edit Hello.java". "Edit" opens a simple editor (writing program), and the name of my program is called "Hello.java". Every Java class has the .java extension. I wrote up a simple code that displays to the screen "welcome to programming". I saved, exited, and then typed "javac Hello.java". Javac stands for Java compiler, and the computer then compiles (meaning converts from what you wrote into a language that the computer can understand) my programs. If I were to look at my folder, I would find a new class called Hello.class. When I compiled my "Hello.java", the compiler created a "Hello.class", which is in computer understood language. If you try to read it, it will look like garbage, because it is in a language that only the computer can read. I then type "java Hello", which tells the computer to run the class called "Hello". (Note in Java capitalization makes a difference. If your class is called Hello, and you type java hello, it will not work.) Once you are familiar with programming, a better choice would be write your program using an editor or an IDE, which makes writing code easier. Go to Downloading an editor or an IDE to figure out which one you want, and how to get one.

command console

Setting the path. The commands of "javac" and "java" (amongst others), are not system commands, they are Java commands. Meaning, that if you are in the Java bin directory, then the computer understands what these commands mean. If you are out of that directory, then the computer doesn't understand what these commands mean. In short, you do not need to set the path, but if you don't all of your programs will have to be in the bin directory. If you would want to create a file in the C directory, and call it javawork, and keep all of your code in that folder, then you need to set the path. How? The following is taken from Deitel's Java How To Program

Update the PATH variable in Windows

Next, you will set your system's PATH variable to conveniently run the Java 2 SDK executables (javac.exe, java.exe, javadoc.exe, etc.) from any directory without having to type the full path of the command. If you don't set the PATH variable, you need to specify the full path to the executable every time you run it, such as:

C:> \j2sdk1.4.1\bin\javac MyClass.java

While you do not need to set your system's PATH variable to run Java, other software you will install later expects that your PATH variable will already have been set.

It's useful to set the PATH permanently so it will persist after rebooting.

How do I set the PATH permanently?

To set the PATH permanently, add the full path of the j2sdk1.4.1\bin directory to the PATH variable. Typically this full path looks something likeC:\j2sdk1.4.1\bin. Set the PATH as follows, depending on the version of Windows you are using:

Windows NT, Windows 2000, and Windows XP - To set the PATH permanently:

  1. Choose Start, Settings, Control Panel, and double-click System. On Windows NT, select the Environment tab; on Windows 2000 select the Advanced tab and then Environment Variables. Look for "Path" in the User Variables and System Variables. If you're not sure where to add the path, add it to the right end of the "Path" in the User Variables. A typical value for PATH is:

    C:\j2sdk1.4.1\bin

    Capitalization doesn't matter. Click "Set", "OK" or "Apply".

    The PATH can be a series of directories separated by semi-colons (;). Microsoft Windows looks for programs in the PATH directories in order, from left to right. You should only have one <b>bin directory for a Java SDK in the path at a time (those following the first are ignored), so if one is already present, you can update it to j2sdk1.4.1.

  2. The new path takes effect in each new Command Prompt window you open after setting the PATH variable.

Windows 98, Windows 95 - To set the PATH permanently, open the AUTOEXEC.BAT file and add or change the PATH statement as follows:

  1. Start the system editor. Choose "Start", "Run" and enter sysedit, then click OK. The system editor starts up with several windows showing. Go to the window that is displaying AUTOEXEC.BAT

  2. Look for the PATH statement. (If you don't have one, add one.) If you're not sure where to add the path, add it to the right end of the PATH. For example, in the following PATH statement, we have added the bin directory at the right end:

    PATH C:\WINDOWS;C:\WINDOWS\COMMAND;C:\J2SDK1.4.1\BIN

    Capitalization doesn't matter. The PATH can be a series of directories separated by semi-colons (;). Microsoft Windows searches for programs in the PATH directories in order, from left to right. You should only have one bin directory for a Java SDK in the path at a time (those following the first are ignored), so if one is already present, you can update it to j2sdk1.4.1.

  3. To make the path take effect in the current Command Prompt window, execute the following:

    C:> c:\autoexec.bat

    To find out the current value of your PATH, to see if it took effect, at the command prompt, type:

    C:> path

Windows ME - To set the PATH permanently:

From the start menu, choose Programs --> Accessories --> System Tools --> System Information. This brings up a window titled "Microsoft Help and Support". From here, choose Tools --> System Configuration Utility. Click the Environment tab, select PATH, and click Edit. Now add the SDK to your path as described in step B above. After you've added the location of the SDK to your PATH, save the changes and reboot your machine when prompted.


Setting up the classpath. This is something that you will not need to do right away. However, when you start programming using packages besides for the packages which are part of Java's standard API (meaning the packages that come automatically with the jdk), then you will need to set the classpath. If you do not set the classpath, then when you try compiling a class from package "libraries" (a random name that I made up for a package name), that you either created or installed from a different Java developing program, the compiler will not know where to find this package. Therefore you will have to set your classpath to find these packages, even if these are packages that you created. You can read about how to set the classpath from sun by going to Java's tool Documentation 1.4 or Java's tool Documentation 1.5 and click on the "setting the classpath" link for your operating system.

No comments:

Post a Comment