Sunday, June 30, 2013

Display smiley face character in Java

Example to display smiley face character (☺) in Java:

Display smiley face character in Java
Display smiley face character in Java


package javaex_smileyface;

/**
 * @web http://java-buddy.blogspot.com/
 */
public class JavaEx_SmileyFace {

    public static void main(String[] args) {
        System.out.println('\u263A');
        System.out.println('☺');
    }
}


remark: Not all Operating Systems can display extended Unicode symbols, regardless of bundling the fonts with the programs. If it can't display the Unicode symbol, it displays a Square instead.

Monday, June 24, 2013

Field Guide to Java Collections

Field Guide to Java Collections

This session presents a survey of the principal families (list, set, map, and queue) of the Java collections ecosystem and their major and minor members. It examines differences between the collection families and family members, with special attention to the preferred environment of each collection species. Exotic, rarely seen creatures such as SynchronousQueue and ConcurrentSkipListMap are also included. The discussion of the recent evolutionary advances to collections and insights from the fossil record will inform and inspire all those who delight in these most ubiquitous of Java classes.

Sunday, June 23, 2013

Setup RxTx jar and .so for Ubuntu

The former post describe how to "Install RXTX on Ubuntu". It simple download the files to your system, but Java not know where is it! To work with installed RxTx, you have to setup Build Path to compile the code, and copy the binaries (.so files) to java library path.

Setup Java Build Path in NetBeans:

If build path not set correctly, error of "package gnu.io does not exist" will be thrown.

package gnu.io does not exist
"package gnu.io does not exist" thrown without Build Path set
- Right click your project, select Properties.

Setup project properties
Setup project properties
- Select Libraries in Categories, Compiler tab and click Add JAR/Folder button.

Add JAR/Folder
Add JAR/Folder
- Browse to select RXTXcomm.jar, should be in /usr/share/java folder, and click OK to accept.

Add RXTXcomm.jar
Add RXTXcomm.jar
- Click OK again to finish. It should be compiled without error.

Copy the binaries (.so files) to java library path:

Without corresponding .so files in java library path, you will get error of "UnsatisfiedLinkError: no rxtxSerial in java.library.path" in run time, like this:

java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDriver
Exception in thread "main" java.lang.UnsatisfiedLinkError: no rxtxSerial in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1878)
at java.lang.Runtime.loadLibrary0(Runtime.java:849)
at java.lang.System.loadLibrary(System.java:1087)
at gnu.io.CommPortIdentifier.<clinit>(CommPortIdentifier.java:123)
at serialtest.SerialTest.initialize(SerialTest.java:41)
at serialtest.SerialTest.main(SerialTest.java:109)

UnsatisfiedLinkError
UnsatisfiedLinkError

- Refer to the post "Get property of java library path" to know where is the library folder.

- Copy the binaries librxtxSerial.so and librxtxParallel.so (should be in /usr/lib/jni/) to one of the library folder (for example: /opt/java/jre/lib/i386/).

$sudo cp /usr/lib/jni/librxtxSerial.so /opt/java/jre/lib/i386/librxtxSerial.so
$sudo cp /usr/lib/jni/librxtxParallel.so /opt/java/jre/lib/i386/librxtxParallel.so