May 24 2006

HOWTO: Getting started with Java

Tag: HOWTO, JavaDan @ 7:22 pm

This is a rough quick start guide. It was written to help a relative of mine who was starting to learn Java.

  1. Get the Java Development kit here: Java Platform, Standard Edition (Java SE). The current version at the time of writing is Java 5
  2. Once installed set the following environment variable in order to ease use of the development tools on the command line. This environment variable is used by various tools including Ant and Tomcat also.

    JAVA_HOME=[set this to the path you installed Java in]

    The default install location for Java 5 on Windows is C:\Program Files\Java\jdk1.5.0_06.

    To set permanent environment variables on Windows XP/2000:

    1. Right-click on My Computer icon
    2. Select Properties from the context menu
    3. In the dialog that appears click on the ‘Advanced’ tab
    4. At the bottom of this tab there is a button labelled ‘Environment Variables’ click it
    5. In the dialog that appears create the new environment variable in the bottom box labelled ‘System variables’ - that will set the variable for all users of the system.

    This process is completely different on Unix/Linux systems, I can describe the setup process for Redhat and Ubuntu systems in a comment if someone needs it.

  3. Next make sure the Java executable binaries are on your system path, this is so that you can use the Java tools to compile your code. On Windows modify the definition of the Path environment variable to include this at the very end (semi-colon at the beginning is required):

    ;%JAVA_HOME%\bin

    When the system loads %JAVA_HOME% will expand to the value you defined in point 2.

  4. Test the setup from above by opening a command console (Start > Programs > Accessories > Command Prompt) and type:

    java -version

    You should see the following output:


    java version "1.5.0_06"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
    Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode)

    If you don’t get that type set to list the environment variables and check that the two variables you set in steps 2 and 3 are ok.

  5. Now you are all set to start learning. I recommend starting with the Java Tutorial.
  6. For alternate/advanced coverage see Bruce Eckel’s free book “Thinking In Java”.

Advice

  • Don’t use an IDE to start coding Java. It may get you started quickly but IDEs hide a lot of the real details of how a technology works. Eventually all good developers need to understand these details; it’s quicker and easier to do this from the start. Get yourself a decent text editor and learn to use the command line tools. Once you understand how Java works an IDE such as Eclipse can be very empowering.
  • Decent editors are (IMHO on Windows): JEdit, Notepad2, Vim, of course there are many others ;)
  • When you get fed-up typing javac to compile your code learn to use Ant
  • Learn as much as you can about Object Orientated Programming

Enjoy.

This was written quickly so if you notice any mistakes or things which could be improved please add a comment and I’ll fix it.


May 06 2006

Changing screen resolution in Linux

The company I work for has recently released a Java application which makes use of the Java Fullscreen Exclusive Mode API. It works great on Windows but because of a longstanding Java bug it is unable to change the display size and suspend windowed mode on Linux. Luckily the fix for this issue has been included in Java 6 Mustang so I’ll have to download a new snapshot and test it out with that.

Anyway, while reading through the bug listing I found some hints on changing the X server screen resolution, which were a great find for me as I was wondering how I could get MPlayer to play movies in proper fullscreen mode and hadn’t justified the time to research the solution.

To change the screen resolution on Linux you need to send an instruction to the X server to switch to another of its preconfigured modes. One way is to use the Ctrl+Alt+NumPad[+|-] key combination which iterates either up (numpad +) or down (numpad -) through the available modes. The only draw back to this on my platform (Ubuntu 5.10 Breezy Badger) is that when switching to smaller resolutions it didn’t resize the Gnome desktop and open windows it just acted more like a global magnifying glass to the desktop which made it rather difficult to work in any new mode.

The best way I found is to use the RandR extension which provides a command line tool called xrandr to manipulate the screen modes. I’m not sure how widely available this extension is on other distributions but it was certainly present on my Ubuntu installation without me having to install it.

To use it to change the resolution you first need to get a list of available modes to change to. To see a list open a console and type xrandr -q. This is what gets listed for my machine:

daniel@coltrane:~$ xrandr -q
 SZ:    Pixels          Physical       Refresh
*0   1440 x 900    ( 373mm x 231mm )  *60
 1   1024 x 768    ( 373mm x 231mm )   60
 2    800 x 600    ( 373mm x 231mm )   60
 3    640 x 480    ( 373mm x 231mm )   60
 4   1152 x 864    ( 373mm x 231mm )   60
 5    640 x 400    ( 373mm x 231mm )   60
 6    512 x 384    ( 373mm x 231mm )   60
 7    400 x 600    ( 373mm x 231mm )   60
 8    320 x 480    ( 373mm x 231mm )   60
 9    320 x 400    ( 373mm x 231mm )   60
Current rotation - normal
Current reflection - none
Rotations possible - normal
Reflections possible - none

You can see that the current mode is highlighted with ‘*’. The first column in the table is the index number of the screen mode. My default has an index of 0. To change to another mode simply look up the index of the mode you want and type xrandr -s [index]. I needed to swap to 800×600 so I just typed xrandr -s 2 and the screen switched mode and most importantly also updated the layout of my desktop to fit the new screen size. To get back to the default I just typed xrandr -s 0.

After swapping to 800×600 MPlayer correctly took the entire screen in its fullscreen mode for the video clip I was playing and ran just as well as on Windows - no need to reboot back into XP to play my clips anymore! :D