Dads View

February 1, 2009

Java for beginners

Filed under: Java — Administrator @ 8:04 am

I’ve started a small project where I’ll need a program that I can run on both Windows system and Linux.  I had the choice of using either C++ or Java, and decided to jump into learning Java.   This is a short log of my tribulations in getting Java and Netbeans working on my Windows XP system.

My first task was to figure out what to install.  A quick search around the net makes it clear that Sun is THE authority  on Java.

Sun’s Java Page is clear as mud about what I need to download.

After a little research (since this wasn’t described on sun.com) , I discovered the following:
Java SE – is standalone Java for common platforms such as PCs
Java EE – is Java for large systems with web servers and multi system setups
Java ME – is Java for small standalone boxes (like cable boxes)

My needs more closely matched Java SE, so that’s what I chose under the “Popular Downloads” menu on the right side of the Sun page above.
This is the download screen for Java SE

A bit about how Java works:

Some computer languages such as C++ use a special program called a compiler to convert your program into the native language of your computer’s CPU(machine language).
Software companies must then develop a different compiler for every type of CPU, in some cases, dozens of different versions.
Since each of these compilers is slightly different, your C++ program may not behave the same on all these different computers.
Because of these issues the developers of Java decided to produce a compiler that would convert your program to run on only ONE type of CPU . They called this CPU the Java Virtual Machine.
However, the Java Virtual Machine is not a real piece of hardware.
In order for your Windows XP machine to execute the compiled Java program it needs another piece of software called the Java Runtime Environment (or JRE). The JRE is a program that simulates another CPU (the Java Virtual Machine) on your Windows XP (or other system).

Back to the downloading process…
Therefore, I need to download two separate components:
The Java Runtime Environment
The Java Development Kit (the compiler)

I chose JRE 6 Update 11 and JDK 6 Update 11 with NetBeans 6.5

The install went fairly smooth and then it was time to try to run a sample Java program from the command line:
c:\myprograms\java my_program_name

I get the ugly message:
Could not find the main class: my_program_name . Program will exit.

My first Java Installation problem.

I little more searching on the net gives me my answer:

My source file my_program_name.java must first be compiled into a .class file with the javac utility as follows:

javac my_program_name.java

this gives me the error message:
‘javac’ is not recognized as an internal or external command, operable program or batch file.

Turns out that Sun’s Java install doesn’t do everything it’s supposed to!

Javac is an .exe file that needs to be on the Windows XP system path to be found.

I located javac.exe in C:\Program Files\Java\jdk1.6.0_11\bin

To add a directory to the Windows XP path, do the following:

My Computer -> right click properties -> Click Environment Variables button -> Scroll down list of System variables and select “Path”

Click Edit and use right arrow to scroll to the end. Add the following:
;C:\Program Files\Java\jdk1.6.0_11\bin
Note the semicolon at the beginning of the line.
If you are using a different version of the JDK change the path to match your installation.

Click OK twice.

Close and reopen your command window

Now trying:
javac my_program_name.java
java my_program_name

and I got results!

Remember, Java is case sensitive. Make sure you are typing your Java program names correctly.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress