Archive for the ‘java’ category

Java socket connection timout

March 13th, 2014

There are 2 places to set the timeout option of a socket, at connection and at reading.

  • Socket.connect(SocketAddress endpoint, int timeout) connects this socket to the server with a specified timeout value. A timeout of zero is interpreted as an infinite timeout. The connection will then block until established or an error occurs. Here the timeout setting only affects the action of connecting!
  • Socket.setSoTimeout(int timeout) enable/disable SO_TIMEOUT with the specified timeout, in milliseconds. With this option set to a non-zero timeout, a read() call on the InputStream associated with this Socket will block for only this amount of time. If the timeout expires, a is raised, though the Socket is still valid. The option must be enabled prior to entering the blocking operation to have effect. The timeout must be > 0. A timeout of zero is interpreted as an infinite timeout.

Memo: tomcat configurations

December 7th, 2013

Here are some common used configurations of tomcat.

  • Add a new tomcat host. In server.xml, add these lines:
    		<Host name="" appBase="/var/www/domain_webapps_dir"
                    unpackWARs="true" autoDeploy="false">
                    <Context path="" docBase="/var/www/domain_webapps_dir/proect_name" reloadable="true" />
                    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
                            prefix="xxx_access_log." suffix=".txt"
                            pattern="%h %l %u %t "%r" %s %b" />

Ant build script for java web project with tomcat on ubuntu

December 7th, 2013

Here is a sample ant build script for java web project with tomcat on ubuntu. It should be put under the root directory of the web project.

<?xml version="1.0" encoding="UTF-8"?>
<project name="ProjectName" default="war" basedir=".">
	<property environment="env" />
	<property name="" value="ProjectName" />
	<property name="webapp.dir" value="/data/public/JavaWebapps" />
	<property name="tomcat.lib" value="/usr/share/tomcat7/lib" />
	<property name="ant.lib" value="/usr/share/ant/lib" />
	<property name="dist.dir" value="${basedir}/war" />
	<property name="webRoot.dir" value="${basedir}/WebContent" />
	<property name="src.dir" value="${basedir}/src" />
	<property name="config.dir" value="${basedir}/resources" />
	<property name="lib.dir" value="${webRoot.dir}/WEB-INF/lib" />
	<property name="build.dir" value="${basedir}/build" />
	<!-- Init classpath -->
	<path id="project.classpath">
		<fileset dir="${lib.dir}">
			<include name="**/*.jar" />
		<fileset dir="${tomcat.lib}">
			<include name="*.jar" />
		<!-- ant lib  -->
		<fileset dir="${ant.lib}">
			<include name="*.jar" />

	<!-- get the source compile classpath in a printable form -->
	<pathconvert pathsep="${line.separator}|   |-- "
	<!-- show classpath jars -->
	<target name="print_classpath">
		<echo message="|-- compile classpath"/>
		<echo message="|   |"/>
		<echo message="|   |-- ${echo.path.compile}"/>
	<target name="clear" description="Clear the project">
		<delete dir="${build.dir}" />
		<delete dir="${dist.dir}" />
		<delete file="${}/${}.war" />
		<delete dir="${}/${}" />

	<target name="init" depends="clear" description="Init the directories">
		<mkdir dir="${build.dir}/classes" />
		<mkdir dir="${dist.dir}" />

	<target name="compile" depends="init" description="Compile source files">
		<echo message="begin compile..." />
		<javac srcdir="${src.dir}" destdir="${build.dir}/classes" 
			includeantruntime="false" nowarn="on" 
			target="1.7" deprecation="true" debug="true" 
			encoding="UTF-8" classpathref="project.classpath" 
			<compilerarg line="-Xlint:unchecked" />
			<!-- <classpath refid="project.classpath" /> -->
		<copy todir="${build.dir}">
			<fileset dir="${src.dir}">
				<include name="**/*.xml" />
				<include name="**/*.properties" />
				<include name="**/*.sql" />
			<fileset dir="${config.dir}">
				<include name="**/*.xml" />
				<include name="**/*.properties" />
				<include name="**/*.sql" />
		<echo message="end compile..." />

	    <target name="pack" depends="compile"> 
	        <jar jarfile="${build.dir}/${}.jar"> 
	            <fileset dir="${build.dir}/classes"> 
	                <include name="**/*.class"/> 

	<target name="war" depends="compile" description="Package the project as .war file.">
		<echo message="begin packing war..." />
		<war destfile="${dist.dir}/${}.war" basedir="${webRoot.dir}" 
			<lib dir="${lib.dir}" />
			<classes dir="${build.dir}/classes" />
			<fileset dir="${webRoot.dir}">
				<include name="***.*" />
		<echo message="end war..." />

	<target name="deploy" depends="war" description="Deploy the war to tomcat">
		<echo message="begin deploy..." />
		<copy file="${dist.dir}/${}.war" todir="${webapp.dir}" />
		<echo message="end deploy..." />



July 18th, 2013




  1. 在paoding-dic-home.properties文件中配置自定义词库的位置。
    • 首先配置paoding.dic.home.config-fisrt。其有2个选项:system-env和this。
      • paoding.dic.home.config-fisrt=system-env 表示使用系统变量。如果使用这个,那个需要配置环境变量 PAODING_DIC_HOME 为字典所在目录。
      • paoding.dic.home.config-fisrt=this 表示使用本配置文件里面的配置。如果使用这个,需要在本配置文件中继续配置 paoding.dic.home。
    • 配置paoding.dic.home (如果在上面选择了paoding.dic.home.config-fisrt=this才需要),也有2种方法:使用相对路径和绝对路径
      • paoding.dic.home=classpath:dic 使用相对路径。表示使用本项目的classpath中列出的任意一个文件夹,然后把dic文件夹放置其中。一般在eclipse项目中,把dic文件夹放在源码文件夹(src)中。
      • paoding.dic.home=D:/somepath/dic 使用绝对路径。
  2. 配置好路径之后,可以在dic文件夹中新建任意以.dic为后缀的词库文件。每行一个词。还是注意:必须保存为UTF-8格式。

Simple Example of Java JSON Encoding in Servlet

May 17th, 2013

Here is a simple example showing how to output json in a servlet. I’ll use the “JSON-java” package which is recommended by and can be downloaded at To encode Java objects into a JSON string, we simply use org.json.JSONObject and

import org.json.JSONArray;
import org.json.JSONObject;

 * A simple example of showing how to output json in a Servlet.
@WebServlet(description = "Test servlet showing how to output json in Java", urlPatterns = { "/services/testjson" })
public class TestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setHeader("Cache-Control", "nocache");
		PrintWriter out = response.getWriter();

		JSONObject json = new JSONObject();

		// put some value pairs into the JSON object as into a Map.
		json.put("status", 200);
		json.put("msg", "OK");

		// put a "map" 
		JSONObject map = new JSONObject();
		map.put("key1", "val1");
		map.put("key2", "val2");
		json.put("map", map);
		// put an "array"
		JSONArray arr = new JSONArray();
		json.put("arr", arr);

		// finally output the json string		

There is another package called json-simple which is also very handy and simple. More information about json-simple can be found at

Install Lucene Solar with Tomcat on Windows

May 7th, 2013

Installing the example solar web application is actually super easy. Here we suppose you already installed the Tomcat sevlet container on your computer TOMCAT_HOME (for example: D:\prog\apache-tomcat-7.0.35). If not, you can download it here and install it (at TOMCAT_HOME).

The next step is to install Solar:

  1. download Solr at:
  2. extract the solar package into, for example, solr-4.3.0
  3. copy the sample “Solr Home” directory solr-4.3.0\example\solr\ (Note that it’s the folder ‘example\solr\’ itself, not the content under it!!!) into the home directory of tomcat: TOMCAT_HOME if you start tomcat with Tomcat Monitor or into TOMCAT_HOME\bin if you start tomcat with TOMCAT_HOME\bin\startup.bat. Note that the solr hoe directory must be put in the right directory. This is very important!!! Otherwise you will get such an exception: “HTTP Status 500 – {msg=SolrCore ‘collection1′ is not available due to init failure: Could not load config for solrconfig.xml …… ”
    In a word, the solr home directory should be put in the Java current working directory. You can also “Configure the servlet container such that a JNDI lookup of “java:comp/env/solr/home” by the Solr webapp will point to the Solr home”.
  4. copy the solr war file (solr-N.N.N.war) under solr-4.3.0\dist\ as solr.war into the tomcat webapps directory TOMCAT_HOME/webapps. Tomcat will automatically deploy it.
  5. After solr.war is extracted, copy the SLF4j logging jars from solr-4.3.0/example/lib/ext into TOMCAT_HOME/lib or TOMCAT_HOME/webapps/solr/WEB-INF/lib. If this step is ignored, this exception will be thrown out: org.apache.catalina.core.StandardContext filterStart
    SEVERE: Exception starting filter SolrRequestFilter
    org.apache.solr.common.SolrException: Could not find necessary SLF4j logging jars. If using Jetty, the SLF4j logging jars need to go in the jetty lib/ext directory. For other containers, the corresponding directory should be used. For more information, see:

OK. That’s it! Now locate your web browser to http://localhost:8080/solr (Change the port if necessary) and you will see the admin page of solr.

If there are exceptions, check your tomcat logs (such as localhost.YYYY-MM-DD.log) under TOMCAT_HOME\logs\.

PS. actually to install solar on windows is not so much different from that on any other OS. :)

More details can be found on the Solr Wiki:

Java with UTF-8 encoding (especially with Chinese)

May 5th, 2013

Here are just some memos on how to deal with non-unicode characters (like Chinese) in java.

  • Decode characters from URL in a servlet:
    String s = request.getParameter("mykeywords");
    s = new String(s.getBytes("ISO-8859-1"), "UTF-8");

    But if the uri encoding is specified in server.xml as:
    <Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>,
    there is no need to decode it. Simply use:
    String s = request.getParameter("mykeywords"); and s will be utf-8 encoded.

  • Send response with UTF-8:
  • Connect mysql with UTF-8:
    private static final String DB_URL = "jdbc:mysql://DB_HOST:3306/DB_SCHEMA?useUnicode=true&characterEncoding=utf8";
  • To encode JSP files with UTF-8, put this at the beginning of a JSP file:
    <%@ page contentType="text/html;charset=UTF-8" %>
  • With SpringMVC, for GET requests, set <Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>,.
    And for POST requests, set this as the first filter in web.xml:
  • And finally, make sure every file itself is stored as UTF-8.

java client with multi entry

August 9th, 2011

The command to execute different jar entry:
java -cp project.jar p1.AnotherEntry

javaps – list java processes on linux

July 28th, 2011

here is a script that you can use to list java processes on linux.

here is code:

# Copyright (c) 2000 Alexander V. Konstantinou 
# Permission to use, copy, modify, distribute and sell this software
# and its documentation for any purpose is hereby granted without fee,
# provided that the above copyright notice appear in all copies and
# that both that copyright notice and this permission notice appear
# in supporting documentation. Alexander V. Konstantinou makes no
# representations about the suitability of this software for any
# purpose. It is provided "as is" without express or implied warranty.
# Description: prints out the java processes running on a Linux host.
# Optional "-noflags" argument controls verbosity
# Requirements: Linux host with a /proc filesystem
# Updates:
# $Id: javaps,v 1.3 2002/01/21 02:32:12 akonstan Exp $


SYSNAME=`uname -s`

if [ "$SYSNAME" != "Linux" ]; then
echo "$0: must be executed on Linux system (found $SYSNAME)" >&2
exit 1

if [ ! -d "/proc" ]; then
echo "$0: Linux host does not appear to have a /proc filesystem" >&2
exit 1

if [ -n "$1" ]; then
if [ "$1" = "-noflags" ]; then
echo "Usage: $0 { -noflags }" >&2
exit 1

echo "PID Process"

# For every file in the /proc file system

FILES=`/bin/ls -1 /proc`

for FILE in $FILES; do

# Check if it is a process directory and that we have read access
if [ -d "/proc/$FILE" -a -r $STATUS_FILE -a -r $CMDLINE_FILE -a "$FILE" != "/proc/self" ]; then

PROCESS_NAME=`grep 'Name:' $STATUS_FILE | awk '{print $2}'`

# Only interested in java processes
if [ "$PROCESS_NAME" = "java" ]; then
PARENT_PID=`grep 'PPid' $STATUS_FILE | awk '{print $2}'`

# Figure out if process has a parent that is a java process
if [ $PARENT_PID -eq 1 ]; then
PARENT_NAME=`grep 'Name:' /proc/$PARENT_PID/status | awk '{print $2}'`
if [ "$PARENT_NAME" != "java" ]; then

# If root java process print out
if [ $ISROOT -ne 0 ]; then
PROCESS_CMDLINE="`cat $CMDLINE_FILE | tr '00' ' '`"

if [ $NOFLAGS -eq 0 ]; then
if [ $IGNORE_ARG -eq 0 ]; then
if [ $ARG = "-classpath" -o $ARG = "-cp" ]; then
elif [ -z "`echo $ARG | grep '^-D'`" -a -z "`echo $ARG | grep '^-X'`" ]; then

setting java environment under ubuntu

May 20th, 2010

you can set all the environment vars in /etc/environment.

for example:



use bash> source /etc/environment to refresh the system variables.