Barcamp in Phnom Pen

Leave a comment

You are invited to join barcamp in Phnom pen on 3-4 October 2009 — 7:30AM – 5:00PM.
Barcamp Site
Barcamp Phnom Pen


Ubuntu Linux Install Sun Java Development Kit ( JDK ) and Java Runtime Environment ( JRE )

Leave a comment

Question :
How do I install Sun Java Development Kit (JDK) and Java Runtime Environment (JRE) under Ubuntu Linux? It appears that there are multiple JRE installed by default under Ubuntu. How do I select and use Sun JRE only? Can you explain steps required to set the environment to run java programs or apps?

Ubuntu Linux 7.10 has following packages from Sun:
=> sun-java6-bin : Sun Java Runtime Environment (JRE) 6

=> sun-java6-demo : Sun Java Development Kit (JDK) 6 demos

=> sun-java6-jdk : Sun Java Development Kit (JDK) 6

=> sun-java6-jre : Sun Java Runtime Environment (JRE) 6

Open a shell prompt (terminal) and type the following to install JDK and JRE:

$ sudo apt-get install sun-java6-bin sun-java6-jre sun-java6-jdk

Setup the default Java version

Ubuntu Linux comes with update-java-alternatives utility to updates all alternatives belonging to one runtime or development kit for the Java language. To select, Sun’s JVM as provided in Ubuntu 7.10, enter:

$ sudo update-java-alternatives -s java-6-sun

You also need to edit a file called /etc/jvm. This file defines the default system JVM search order. Each JVM should list their JAVA_HOME compatible directory in this file. The default system JVM is the first one available from top to bottom. Open /etc/jvm

$ sudo vi /etc/jvm

Make sure /usr/lib/jvm/java-6-sun is added to the top of JVM list


At the end your file should read as follows:


Save and close the file.

Setup the environment variable

You also need to setup JAVA_HOME and PATH variable. Open your $HOME/.bash_profile or /etc/profile (system wide) configuration. Open your .bash_profile file:

$ vi $HOME/.bash_profile

Append following line:

export JAVA_HOME=/usr/lib/jvm/java-6-sun
export PATH=$PATH:$JAVA_HOME/bin

Save and close the file.

Test your new JDK
Type the following command to display version:

$ java -version


java version "1.6.0_03"
Java(TM) SE Runtime Environment (build 1.6.0_03-b05)
Java HotSpot(TM) Server VM (build 1.6.0_03-b05, mixed mode)

Try – first java program

$ vi

Append code:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");

Save and close the file. Compile and execute program by typing following two instructions:

$ javac
$ java HelloWorld


Hello, World!

JMF Webcam app with saving JPEG


Since so many people ask for this code, but never get it, I figured I’d repost it. I didn’t write it, but I added the jpeg saving part, and modified it for my device, which you will have to do to get it to work. Just go into JMFRegistry and get the device name from your webcam and then edit the code. Also, keep in mind that some webcams don’t work with JMF. You have to have a webcam that supports VFW or WDM interface.
And here’s the code:

import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import com.sun.image.codec.jpeg.*;
public class SwingCapture extends Panel implements ActionListener 
  public static Player player = null;
  public CaptureDeviceInfo di = null;
  public MediaLocator ml = null;
  public JButton capture = null;
  public Buffer buf = null;
  public Image img = null;
  public VideoFormat vf = null;
  public BufferToImage btoi = null;
  public ImagePanel imgpanel = null;
  public SwingCapture() 
    setLayout(new BorderLayout());
    imgpanel = new ImagePanel();
    capture = new JButton("Capture");
    String str1 = "vfw:Logitech USB Video Camera:0";
    String str2 = "vfw:Microsoft WDM Image Capture (Win32):0";
    di = CaptureDeviceManager.getDevice(str2);
    ml = di.getLocator();
      player = Manager.createRealizedPlayer(ml);
      Component comp;
      if ((comp = player.getVisualComponent()) != null)
    catch (Exception e) 
  public static void main(String[] args) 
    Frame f = new Frame("SwingCapture");
    SwingCapture cf = new SwingCapture();
    f.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
    f.setSize(new Dimension(320,550));
  public static void playerclose() 
  public void actionPerformed(ActionEvent e) 
    JComponent c = (JComponent) e.getSource();
    if (c == capture) 
      // Grab a frame
      FrameGrabbingControl fgc = (FrameGrabbingControl)
      buf = fgc.grabFrame();
      // Convert it to an image
      btoi = new BufferToImage((VideoFormat)buf.getFormat());
      img = btoi.createImage(buf);
      // show the image
      // save image
  class ImagePanel extends Panel 
    public Image myimg = null;
    public ImagePanel() 
    public void setImage(Image img) 
      this.myimg = img;
    public void paint(Graphics g) 
      if (myimg != null) 
        g.drawImage(myimg, 0, 0, this);
  public static void saveJPG(Image img, String s)
    BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = bi.createGraphics();
    g2.drawImage(img, null, null);
    FileOutputStream out = null;
      out = new FileOutputStream(s); 
    catch ( io)
      System.out.println("File Not Found"); 
    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
    catch ( io) 

More detail

Date picker built-in Java 7 (SwingX)

Leave a comment

JXDatePicker is a date picker that is built-in class in SwingX in JDK7.
Here is snippet code of JXDatePicker:

final JLabel label = new JLabel();
label.setText("Choose Date by selecting below.");
final JXDatePicker datePicker = new JXDatePicker(System.currentTimeMillis());
datePicker.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
frame.getContentPane().add(label, BorderLayout.NORTH);
frame.getContentPane().add(datePicker, BorderLayout.CENTER);

Test performance in Java

Leave a comment

Here is the good website for me to make a performance test in Java
Click here to visit the site.

Consuming a REST Web Service

Leave a comment

Representation State Transfer (REST) is a lite-weight approach for communicating with other applications using HTTP and XML. While SOAP let’s you define your own operations, with REST services there are essentially only four operations: GET, POST, PUT and DELETE. Given it’s simplicity, REST services are becoming very popular building block on web applications.

Since REST focusses on HTTP and XML, there isn’t a need for any specialized java libraries for accessing REST services. The JAVA platform already has the basic libraries in place for handling HTTP requests (* or org.apache.commons.httpclient.*) and processing XML (JAXP). Therefore nothing additional is required.

As far as consuming REST services from Spring applications, and in particular applications produced with Skyway Builder, the standard HTTP and XML processing logic available to java developers can be used. A developer can write java code and/or Spring beans for making the REST calls and processing the results. Additionaly a developer can leverage the Groovy support in Skyway.

Here’s an example java class that calls a REST service using Apache commons httpclient library.

Sample java code invoking REST services

 * Created on September 12, 2009, 12:33 PM
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.

package javaapplicationsimpletest;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

 * @author Seven
public class SampleWebServiceGet {
    public String callRestService(String request) throws IOException {
        HttpClient client = new HttpClient();
        GetMethod method = new GetMethod(request);        
        // Provide custom retry handler is necessary
                new DefaultHttpMethodRetryHandler(3, false));
        // Send GET request
        int statusCode = client.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            System.err.println("Method failed: " + method.getStatusLine());
        byte[] responseBody = method.getResponseBody();
        return new String(responseBody);

We need download “commons-httpclient, commons-logging, commons-codec” from


Leave a comment

Older Entries Newer Entries