First Time Linux


Java is a very popular, established language, with a wide variety of applications including server-side web programming, applets running in browsers, and applications running just about everywhere - it can run on a huge range of hardware from mobile phones and handheld devices through desktops and servers through to massive mainframes.

To get started with java programming, you first need to get hold of a development kit, called a JDK or SDK. Unlike some other languages, like perl or bash scripts, running java is a two-step process - firstly the code is compiled into a runnable form, and then the compiled code is executed. Therefore if all you want to do is run other people's java, you just need a runtime environment, or JRE, but if you want to write your own you need the compiler that comes with the JDK.

In these days of free (and Free) software, there are open source implementations of java software as well as the official versions provided by Sun. However if you have no particular aversion to having non-free software on your system, you might as well get the official thing.

Well, actually, there are two official things, one is provided by Sun itself and made available from their Sun J2SE page (current version is at J2SE 1.5.0). The second is provided by (currently under JDK-1.4.2 in 'downloads'). Blackdown take the binaries from Sun and port them to various architectures for linux. And as Sun don't support the Java3D libraries for linux, I needed the Blackdown version of Java3D and so took their version of the JDK as well. With Blackdown the download is a .bin which installs smoothly (although it isn't an rpm).

Update: Since Sun released the java code into the open-source community, the installation process is more straightforward - for Mandriva there are now official rpms available from the Mandriva repositories (eg java-1.6.0-sun- and java-1.6.0-sun-devel- And of course the other flavours of java runtimes, including the really really free ones, are also still available.

As an extra note, itself seems to have died, which makes getting java3d a lot more difficult than it was.

Checking the install

The JDK is a set of command-line tools, so open a console and check that the JDK is available:

$ java -version
java version "1.4.2"
Java(TM) 2 Runtime Environment, Standard Edition (build Blackdown-1.4.2)
Java HotSpot(TM) Client VM (build Blackdown-1.4.2-fcs, mixed mode)

In this case it's showing a Blackdown version of the JDK 1.4.2. If this command doesn't work, and it reports "command not found", it's possible you need to include the path to the JDK binary directory in your $PATH environment variable. Check with a fully-qualified path to the java executable:

$ /usr/share/j2sdk1.4.2/bin/java -version

If this works, you can add the directory to your $PATH:

$ PATH=$PATH:/usr/share/j2sdk1.4.2/bin

and then just call java directly. You can add this path-setting command to your bash file ~/.bashrc so it gets added for each new console you open.

The first program

So let's write our first java program and make sure that the compiler and runtime environment both work. Make a new directory and use a text editor to make a new text file in this directory. It doesn't matter which editor you use, whether a GUI like KWrite or Kate, or a console-based one like vi or emacs. The file should have the following contents:

class TestJava
	public static void main(String[] args)
		System.out.println("Java is working!");

Now save this file as - the filename must match the class name TestJava which we specified in the first line of the file.

Now that we've got our source code for our (very simple!) program, we need to compile it to make a runnable file. Open a console and go to the directory in which you saved the file Now use the command javac to compile the program:

$ javac

This should generate a new file in the same directory called TestJava.class. If it didn't work, make sure the path to the java binaries is included in your $PATH, or reference it explicitly as above.

If the .class file was successfully created, you can run this inside the java runtime environment using the java command:

$ java TestJava

(note that you don't include the ".class" part of the file, just the class name). Even if you've never seen java before, you can probably guess what it does, but if not I won't ruin the surprise!

Compilation vs Interpretation

According to some, java isn't a proper compiled language, because the compiled code is still interpreted by the runtime engine (rather than just executing as native, machine-specific instructions). This means that it necessarily is slower and not as optimised (hence not as good) as real compiled languages like C++, Delphi or COBOL. While it's true that the 'compiled' java code is not compiled down to a native instruction set, and is indeed interpreted by the JRE, the JRE is usually sufficiently clever to do the compilation itself of crucial code segments, so that repeated calls will indeed call natively compiled code. The performance difference between technologies has been shown to be much less than the effect of good or bad coding practices using the same language, whether it's interpreted or compiled.

Because the .class file isn't created specifically for a single operating system and machine architecture, it means that it can be simply copied onto another, different machine, without being changed or regenerated, and (as long as it doesn't use OS-specific functions that aren't available on the second box) it will just run within the second machine's JRE. That is a huge advantage, as any natively-compiled program would have to at least be recompiled, and more probably heavily rewritten in order to run on a different architecture.

GUI applications and applets

All the classes for developing GUI applications and applets are included in the standard libraries so (unlike with C++) you don't need to install an extra window toolkit to develop full interactive applications. Of course, you can use external, non-standard toolkits if you want, like for example SWT. If you can get a console application working with Mandriva, the techniques for using the extra GUI stuff like AWT, Swing, applets and so on are all standard and cross-platform, so we won't go through any more examples here, but just refer you to the excellent online tutorials (see links below).

Editor vs IDE

In the above example we just used a simple text editor and command-line tools to compile and run our java program. Using just these simple tools it's possible to write all sorts of things, from simple console applications like the one above, to GUI applications with windows and buttons etc, to applets which run within browsers, server-side web applications and beyond. But it's a clumsy way of doing things, and an IDE (an Integrated Development Environment) can provide lots of helpful tools to make it a lot easier and more pleasurable.

For example, it can provide auto-completion of method calls, tooltips on parameters and variables, highlighting of syntax errors as you type, tree-based views of packages and classes, clever searching tools, and much more. The comprehensive eclipse IDE is the clear favourite here, with lots of easy-to-use features. It's available for a wide variety of platforms (although not as rpm) from their downloads page. Note that you still need the JDK as well, this isn't included with eclipse.

More info

Java's home is at, which includes the programming reference (descriptions of classes and their methods) and a friendly java tutorial with plenty of examples and explanations.

Also see Wikipedia's pages on java and eclipse.