All notes


JDK - Java Development Kit.
JRE - Java Runtime Environment.
Java SE - Java Standard Edition.
Java EE - Enterprise Edition.
Java ME - Micro Edition, for mobile environments.

Sun JDK is made by Sun.
OpenJDK is a competing open source implementation.
GCJ is the GNU java compiler.

The JRE contains the JVM as well as class libraries and any other tools necessary to run your Java program.
The JVM is simply the program that interprets your byte code and executes it on the host machine.

java, javaw, javaws

java: java application executor which is associated with a console to display output/errors

javaw: (java windowed) application executor not associated with console. So no display of output/errors. Can be used to silently push the output/errors to text files. Mostly used to launch GUI based application

javaws: (java web start) to download and run the distributed web applications. again No console is associated.

Language Reference



Final variable

SO: how final keyword works.

You are always allowed to initialize a final variable. The compiler makes sure that you can do it only once.

Note that calling methods on an object stored in a final variable has nothing to do with the semantics of final. In other words: final is only about the reference itself, and not about the contents of the referenced object.

Java has no concept of object immutability; this is achieved by carefully designing the object, and is a far-from-trivial endeavor.

Try to do = new ArrayList(); in the main method and you will get compilation error. The reference foo is binded to just one final object of ArrayList. It cannot point to any other ArrayList.

Final classes and methods

oracleDocs: final.

You can declare some or all of a class's methods final. You use the final keyword in a method declaration to indicate that the method cannot be overridden by subclasses. The Object class does this—a number of its methods are final.

Methods called from constructors should generally be declared final. If a constructor calls a non-final method, a subclass may redefine that method with surprising or undesirable results.

Note that you can also declare an entire class final. A class that is declared final cannot be subclassed. This is particularly useful, for example, when creating an immutable class like the String class.


SO: why does java have transient fields.

The transient keyword in Java is used to indicate that a field should not be serialized.

class GalleryImage implements Serializable
    private Image image;
    private transient Image thumbnailImage;

    private void generateThumbnail()
        // Generate thumbnail.

    // At the time of deserialization, the readObject method is called to perform any operations necessary to restore the state of the object back to the state at which the serialization occurred.
    private void readObject(ObjectInputStream inputStream)
            throws IOException, ClassNotFoundException


OracleDocs: atomic.

We have already seen that an increment expression, such as c++, does not describe an atomic action. Even very simple expressions can define complex actions that can decompose into other actions. However, there are actions you can specify that are atomic:

Atomic actions cannot be interleaved, so they can be used without fear of thread interference.

Using simple atomic variable access is more efficient than accessing these variables through synchronized code, but requires more care by the programmer to avoid memory consistency errors. Whether the extra effort is worthwhile depends on the size and complexity of the application.


Here's a list of decompilers as of 03/2015:


JNI: Java Native Interface.

JNA: Java Native Access. Java Native Access is a community-developed library that provides Java programs easy access to native shared libraries without using the Java Native Interface.

Call c function from Java

Write a simple Java application that calls a C function to print "Hello World!". The process consists of the following steps:

  1. Create a class ( that declares the native method. Use javac to compile the HelloWorld source file, resulting in the class file HelloWorld.class.
  2. Use javah -jni to generate a C header file (HelloWorld.h) containing the function prototype for the native method implementation.
  3. Write the C implementation (HelloWorld.c) of the native method. Compile the C implementation into a native library, creating Hello-World.dll or Use the C compiler and linker available on the host environment.
  4. Run the HelloWorld program using the java runtime interpreter. Both the class file (HelloWorld.class) and the native library (HelloWorld.dll or are loaded at runtime.

javah -jni HelloWorld

# Find the locations of the jni.h and jni_md.h
find /usr -name jni.h
# /usr/lib/jvm/java-7-openjdk/include
find /usr -name jni_md.h
# /usr/lib/jvm/java-7-openjdk/include/linux

# Add the include path
g++ -I/usr/lib/jvm/java-7-openjdk/include -I/usr/lib/jvm/java-7-openjdk/include/linux HelloWorld.c -shared -fPIC -o

nm | grep -i hello
# 0000000000000987 t _GLOBAL__sub_I_HelloWorld.c
# 0000000000000900 T Java_HelloWorld_print
# Java_HelloWorld_print: Class HelloWorld, Function print().

java HelloWorld
java -Djava.library.path=. HelloWorld

Declare the Native Method

class HelloWorld {

    private native void print();
    public static void main(String[] args) {
        new HelloWorld().print();
    static {

The HelloWorld class definition begins with the declaration of the print native method. This is followed by a main method that instantiates the Hello-World class and invokes the print native method for this instance. The last part of the class definition is a static initializer that loads the native library containing the implementation of the print native method.

There are two differences between the declaration of a native method such as print and the declaration of regular methods in the Java programming language. A native method declaration must contain the native modifier. The native modifier indicates that this method is implemented in another language. Also, the native method declaration is terminated with a semicolon, the statement terminator symbol, because there is no implementation for native methods in the class itself. We will implement the print method in a separate C file.

Before the native method print can be called, the native library that implements print must be loaded. In this case, we load the native library in the static initializer of the HelloWorld class. The Java virtual machine automatically runs the static initializer before invoking any methods in the HelloWorld class, thus ensuring that the native library is loaded before the print native method is called.

Compile the HelloWorld Class

After you have defined the HelloWorld class, save the source code in a file called Then compile the source file using the javac compiler: javac This command will generate a HelloWorld.class file in the current directory.

Create the Native Method Header File

Next we will use the javah tool to generate a JNI-style header file that is useful when implementing the native method in C. You can run javah on the Hello-World class as follows: javah -jni HelloWorld.

The name of the header file is the class name with a ".h" appended to the end of it. The command shown above generates a file named HelloWorld.h. We will not list the generated header file in its entirety here. The most important part of the header file is the function prototype for Java_HelloWorld_print, which is the C function that implements the HelloWorld.print method: JNIEXPORT void JNICALL Java_HelloWorld_print (JNIEnv *, jobject);.

Ignore the JNIEXPORT and JNICALL macros for now. You may have noticed that the C implementation of the native method accepts two arguments even though the corresponding declaration of the native method accepts no arguments. The first argument for every native method implementation is a JNIEnv interface pointer. The second argument is a reference to the HelloWorld object itself (sort of like the "this" pointer in C++). We will discuss how to use the JNIEnv interface pointer and the jobject arguments later in this book, but this simple example ignores both arguments.

Write the Native Method Implementation

The JNI-style header file generated by javah helps you to write C or C++ implementations for the native method. The function that you write must follow the -prototype specified in the generated header file. You can implement the Hello-World.print method in a C file HelloWorld.c as follows:

#include <jni.h>
#include <stdio.h>
#include "HelloWorld.h"   

JNIEXPORT void JNICALL   Java_HelloWorld_print(JNIEnv *env, jobject obj)
     printf("Hello World!\n");

The C program includes three header files: jni.h -- This header file provides information the native code needs to call JNI functions. When writing native methods, you must always include this file in your C or C++ source files. stdio.h -- The code snippet above also includes stdio.h because it uses the printf function. HelloWorld.h -- The header file that you generated using javah. It includes the C/C++ prototype for the Java_HelloWorld_print function. 2.6 Compile the C Source and Create a Native Library.

Remember that when you created the HelloWorld class in the file, you included a line of code that loaded a native library into the program: System.loadLibrary("HelloWorld");

Now that all the necessary C code is written, you need to compile Hello-World.c and build this native library.

Different operating systems support different ways to build native libraries. On Solaris, the following command builds a shared library called cc -G -I/java/include -I/java/include/solaris HelloWorld.c -o

The -G option instructs the C compiler to generate a shared library instead of a regular Solaris executable file. Because of the limitation of page width in this book, we break the command line into two lines. You need to type the command in a single line, or place the command in a script file. On Win32, the following command builds a dynamic link library (DLL) HelloWorld.dll using the Microsoft Visual C++ compiler: cl -Ic:\java\include -Ic:\java\include\win32 -MD -LD HelloWorld.c -FeHelloWorld.dll.

The -MD option ensures that HelloWorld.dll is linked with the Win32 multithreaded C library. The -LD option instructs the C compiler to generate a DLL instead of a regular Win32 executable. Of course, on both Solaris and Win32 you need to put in the include paths that reflect the setup on your own machine.

Run the Program

At this point, you have the two components ready to run the program. The class file (HelloWorld.class) calls a native method, and the native library (Hello-World.dll) implements the native method.

Because the HelloWorld class contains its own main method, you can run the program on Solaris or Win32 as follows: java HelloWorld.

You should see the following output: "Hello World!"

It is important to set your native library path correctly for your program to run. The native library path is a list of directories that the Java virtual machine searches when loading native libraries. If you do not have a native library path set up correctly, then you see an error similar to the following:

java.lang.UnsatisfiedLinkError: no HelloWorld in library path
        at java.lang.Runtime.loadLibrary(
        at java.lang.System.loadLibrary(
        at HelloWorld.main( 

Make sure that the native library resides in one of the directories in the native library path. If you are running on a Solaris system, the LD_LIBRARY_PATH environment variable is used to define the native library path.

If you are running on a Windows 95 or Windows NT machine, make sure that HelloWorld.dll is in the current directory, or in a directory that is listed in the PATH environment variable.

In Java 2 SDK 1.2 release, you can also specify the native library path on the java command line as a system property as follows: java -Djava.library.path=. HelloWorld.

The "-D" command-line option sets a Java platform system property. Setting the java.library.path property to "." instructs the Java virtual machine to search for native libraries in the current directory.


Hibernate vs MyBatis

Hibernate 是当前最流行的O/R mapping框架,它出身于,现在已经成为Jboss的一部分。 Mybatis 目前属于apache的一个子项目。Object-relational mapping (ORM, O/RM, and O/R mapping tool) in computer science is a programming technique for converting data between incompatible type systems in object-oriented programming languages.

Hibernate实现了POJO 和数据库表之间的映射,以及SQL 的自动生成和执行。程序员甚至不需要对SQL 的熟练掌握, Hibernate/OJB 会根据制定的存储逻辑,自动生成对应的SQL 并调用JDBC 接口加以执行。
iBATIS 的着力点,则在于POJO 与SQL之间的映射关系。然后通过映射配置文件,将SQL所需的参数,以及返回的结果字段映射到指定POJO。

Could not find or load main class


Unzip war

# Remember to add a destDir for the unzipping.
unzip file.war -d destDir


SO: what is reflection.

The ability to inspect the code in the system and see object types is not reflection, but rather Type Introspection. Reflection is then the ability to make modifications at runtime by making use of introspection. The distinction is necessary here as some languages support introspection, but do not support reflection. One such example is C++.
Reflection is a language's ability to inspect and dynamically call classes, methods, attributes, etc. at runtime.