Tuesday, August 21, 2012

Tomcat – Java.Lang.OutOfMemoryError: PermGen Space


Tomcat production server sometime will hit the following java.lang.OutOfMemoryError: PermGen space error.
java.lang.OutOfMemoryError: PermGen space
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
It’s usually happened when the Tomcat start and stop few times. It’s just funny, however you can fine tune it with some minor changes in the Tomcat configuration setting. By default, Tomcat assigned very little memory for the running process, you should increase the memory by make change in catalina.sh or catalina.bat file.

How To Fix It?

1) Find where is Cataline.sh located. We need to make some changes in “catalina.sh” file. 
P.S Cataline.sh is located at \tomcat folder\bin\catalina.sh
2) Assign following line to JAVA_OPTS variable and add it into catalina.sh file.
JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 
-server -Xms1536m -Xmx1536m
-XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m 
-XX:MaxPermSize=256m -XX:+DisableExplicitGC"
Partial example of the catalina.sh file
#   JSSE_HOME       (Optional) May point at your Java Secure Sockets Extension
#                   (JSSE) installation, whose JAR files will be added to the
#                   system class path used to start Tomcat.
#
#   CATALINA_PID    (Optional) Path of the file which should contains the pid
#                   of catalina startup java process, when start (fork) is used
#
# $Id: catalina.sh 609438 2008-01-06 22:14:28Z markt $
# -----------------------------------------------------------------------------
 
JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms1536m 
-Xmx1536m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m 
-XX:MaxPermSize=256m -XX:+DisableExplicitGC"
 
 
# OS specific support.  $var _must_ be set to either true or false.
cygwin=false
os400=false
darwin=false
case "`uname`" in
CYGWIN*) cygwin=true;;
OS400*) os400=true;;
Darwin*) darwin=true;;
esac
 
# resolve links - $0 may be a softlink
PRG="$0"
3) Done. Restart Tomcat.
You should change the “Xms” and “PermSize” value base on your server capability.

Tuesday, July 24, 2012

CentOS minimal 安装笔记

ref: http://blog.csdn.net/lixianlin/article/details/7293691


现在CentOS最新版是6.2,提供了最小化安装的iso镜像(32位的285M,64位的322M),以前5.x版中是没有单独提供的,对于我们这些搞开发来的,是个福音,不管是下载,还是安装,都省去不少时间,存储空间都节省了不少。
下了个64位的,用WMware(7.1.4版)安装,一路下来,除了中间提示缺少gcc、kernel-devel、make等包外(最小化安装版嘛,少些包,很正常,选择"Continue"跳过),安装相当顺利,全自动的。
注意:安装的时候,如果想使用图形安装界面,内存设置必须大于652M。
The installer needs at least 392MB of memory to work. Text mode will automatically be used if the system has less than 652MB of memory.
安装完了,自动重启,结果悲剧了,启动不了,提示下面的信息:
Installing VMware Tools, please wait...
mount: special device /dev/hda does not exist
mount: block device /dev/sr0 is write-protected, mounting read-only
./run_upgrade.sh: line 21: file: command not found
仔细想了想,安装过程中未出现什么错误,验证了一下iso文件的hash,对的,安装包是完整的。
google了一下,没找到相关的资料,于是只好自己动手...
重新检查了各项设置,发现硬件设置中多了一个CD/DVD(IDE),装载了autoinst.iso,另一个CD/DVD 2(IDE)装载了安装镜像CentOS-6.2-x86_64-minimal.iso,重新建了个虚拟机,发现在选择安装镜像文件那里,有个提示“CentOS 64-bit detected.This operating system will use Easy Install.“,猜测问题可能是因为这个快速安装引起的(因为现在安装的镜像是一个最小化安装版,并不是完整版)。
猜测归猜测,实践了才知道。所以又新建了个虚拟机,在安装镜像选择那里,选择“I will install the operating system later.“,创建完后,硬件设置中设置安装镜像,一路手动安装下来,比自动安装稍微慢了一点,安装完毕,手动重启,启动成功,看来刚才猜测是对的。
安装过程最好配置好网络,因为这个最小化版不包含图形界面的网络设置工具,手动配置不太方便,以下是我上面的配置:
vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
NM_CONTROLLED=yes
ONBOOT=yes
HWADDR=00:0c:29:5b:1c:de
TYPE=Ethernet
BOOTPROTO=none
IPADDR=192.168.1.223
NETMASK=255.255.255.0
PREFIX=24
GATEWAY=192.168.1.1
DNS1=192.168.1.1
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="System eth0"
UUID=5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03
USERCTL=no
vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=localhost.localdomain
vi /etc/resolv.conf
nameserver 192.168.1.1
网络配置好了,就可以用yum来安装一些常用的软件:
yum install vim
yum install wget
yum install zip
yum install unzip
yum install man
yum install gcc
yum install make
yum install setuptool
yum install system-config-network-tui
yum install system-config-firewall-tui
......
安装图形界面:
yum groupinstall "X Window System"
yum groupinstall "Chinese Support"
yum groupinstall "KDE Desktop"
安装完成后,用"init 5"即可启动图形界面。

Tuesday, May 15, 2012

1. Convert project to JavaEE project.
ref: http://www.mkyong.com/java/how-to-convert-java-project-to-web-project-in-eclipse/
Here is the steps to convert Java Project to Web Project in Eclipse
1) Create a empty dynamic web project and click on the .project file
2) Copy “web project” facet buildCommand and paste within “java project” .project buildSpec tag
 

 org.eclipse.wst.common.project.facet.core.builder
 
 

3) Copy “web project” facet nature and paste within “java project” .project natures tag
org.eclipse.wst.common.project.facet.core.nature
P.S Be careful of the .project xml tag, make sure you paste into a correct location.
4) Save it.
5) Right click on Java Project, click properties.
6) Select Project Facets and click modify project button.
7) Click Dynamic Web Module and Java check box.
Finished, Now your java project has been converted into a web project.
2. Config the WebContent folder
Update org.eclipse.wst.common.component file in .settings directory and wb-resource elements:

3. Add User lib's *.jar files into *war


The jar dependencies for Eclipse to include in a war file not placed in WEB-INF/lib is not managed by the usual "project export/imports" but by a separate panel (3.6: Deployment Assembly, 3.5: Java EE Module Dependencies) in the preferences of the dynamic web project.

      


Tuesday, May 8, 2012

Using make and writing Makefiles

Ref: http://www.cs.swarthmore.edu/~newhall/unixhelp/howto_makefiles.html#creating


Using make and writing Makefiles

make is a Unix tool to simplify building program executables from many modules. make reads in rules (specified as a list of target entries) from a user created Makefile. make will only re-build things that need to be re-built (object or executables that depend on files that have been modified since the last time the objects or executables were built).
GNU make Manual. A complete reference for writing makefiles from simple to advanced features.
Also, CMake, is a cross platform build system. Among other things, cmake will generate makefiles for you. It is particularly useful for large projects, for builds that use lots of libraries, and for dealing with platform-specific compilation issues. It automatically generates (often hard to read and debug) makefiles for different platforms. For small projects, writing makefiles by hand is likely easier. See Andy Danner's Cmake documentation for more information on cmake.

Using make

  1. Create a Makefile listing the rules for building the executable the file should be named 'Makefile' or 'makefile'. This only has to be done once, except when new modules are added to the program, the Makefile must be updated to add new module dependencies to existing rules and to add new rules to build the new modules.
  2. After editing program file(s), rebuild the executable by typing make:
     % make 
    
        A specific target in the Makefile can be executed by typing:
     % make target_label
    
        For example, to execute the rm commands in the example makefile below, type:
     % make clean
    

Creating a Makefile

A Makefile typically starts with some variable definitions which are then followed by a set of target entries for building specific targets (typically .o & executable files in C and C++, and .class files in Java) or executing a set of command associated with a target label.The following is the generic target entry form:
# comment
# (note: the  in the command line is necessary for make to work) 
target:  dependency1 dependency2 ...
       command

for example:
#
# target entry to build program executable from program and mylib 
# object files 
#
program: program.o mylib.o
 gccx -o program program.o mylib.o

An example simple Makefile for a Java

#
# A simple makefile for compiling three java classes
#

# define a makefile variable for the java compiler
#
JCC = javac

# define a makefile variable for compilation flags
# the -g flag compiles with debugging information
#
JFLAGS = -g

# typing 'make' will invoke the first target entry in the makefile 
# (the default one in this case)
#
default: Average.class Convert.class Volume.class

# this target entry builds the Average class
# the Average.class file is dependent on the Average.java file
# and the rule associated with this entry gives the command to create it
#
Average.class: Average.java
        $(JCC) $(JFLAGS) Average.java

Convert.class: Convert.java
        $(JCC) $(JFLAGS) Convert.java

Volume.class: Volume.java
        $(JCC) $(JFLAGS) Volume.java

# To start over from scratch, type 'make clean'.  
# Removes all .class files, so that the next make rebuilds them
#
clean: 
        $(RM) *.class
Here is an example of a Java makefile that uses more advanced makefile syntax.

An example simple Makefile for a C (or C++)

#
# This is an example Makefile for a countwords program.  This
# program uses both the scanner module and a counter module.
# Typing 'make' or 'make count' will create the executable file.
#

# define some Makefile variables for the compiler and compiler flags
# to use Makefile variables later in the Makefile: $()
#
#  -g    adds debugging information to the executable file
#  -Wall turns on most, but not all, compiler warnings
#
CC = gcc
CFLAGS  = -g -Wall

# typing 'make' will invoke the first target entry in the file 
# (in this case the default target entry)
#
default: count

# To create the executable file count we need the object files
# countwords.o, counter.o, and scanner.o:
#
count:          countwords.o counter.o scanner.o
  $(CC) $(CFLAGS) -o count countwords.o counter.o scanner.o

# To create the object file countwords.o, we need the source
# files countwords.c, scanner.h, and counter.h:
#
countwords.o:   countwords.c scanner.h counter.h
                $(CC) $(CFLAGS) -c countwords.c

# To create the object file counter.o, we need the source files
# counter.c and counter.h:
#
counter.o:      counter.c counter.h
                $(CC) $(CFLAGS) -c counter.c

# To create the object file scanner.o, we need the source files
# scanner.c and scanner.h:
#
scanner.o:      scanner.c scanner.h
                $(CC) $(CFLAGS) -c scanner.c

# To start over from scratch, type 'make clean'.  This
# removes the executable file, as well as old .o object
# files and *~ backup files:
#
clean:
                rm -f count *.o *~

Another makefile (using makedepend and more advanced make syntax)

This is an easier to use and modify makefile, but it is slightly more difficult to read than the simple one:
#
# 'make depend' uses makedepend to automatically generate dependencies 
#               (dependencies are added to end of Makefile)
# 'make'        build executable file 'mycc'
# 'make clean'  removes all .o and executable files
#

# define the C compiler to use
CC = gcc

# define any compile-time flags
CFLAGS = -Wall -g

# define any directories containing header files other than /usr/include
#
INCLUDES = -I/home/newhall/include  -I../include

# define library paths in addition to /usr/lib
#   if I wanted to include libraries not in /usr/lib I'd specify
#   their path using -Lpath, something like:
LFLAGS = -L/home/newhall/lib  -L../lib

# define any libraries to link into executable:
#   if I want to link in libraries (libx.so or libx.a) I use the -llibname 
#   option, something like (this will link in libmylib.so and libm.so:
LIBS = -lmylib -lm

# define the C source files
SRCS = emitter.c error.c init.c lexer.c main.c symbol.c parser.c

# define the C object files 
#
# This uses Suffix Replacement within a macro:
#   $(name:string1=string2)
#         For each word in 'name' replace 'string1' with 'string2'
# Below we are replacing the suffix .c of all words in the macro SRCS
# with the .o suffix
#
OBJS = $(SRCS:.c=.o)

# define the executable file 
MAIN = mycc

#
# The following part of the makefile is generic; it can be used to 
# build any executable just by changing the definitions above and by
# deleting dependencies appended to the file from 'make depend'
#

.PHONY: depend clean

all:    $(MAIN)
        @echo  Simple compiler named mycc has been compiled

$(MAIN): $(OBJS) 
        $(CC) $(CFLAGS) $(INCLUDES) -o $(MAIN) $(OBJS) $(LFLAGS) $(LIBS)

# this is a suffix replacement rule for building .o's from .c's
# it uses automatic variables $<: the name of the prerequisite of
# the rule(a .c file) and $@: the name of the target of the rule (a .o file) 
# (see the gnu make manual section about automatic variables)
.c.o:
        $(CC) $(CFLAGS) $(INCLUDES) -c $<  -o $@

clean:
        $(RM) *.o *~ $(MAIN)

depend: $(SRCS)
        makedepend $(INCLUDES) $^

# DO NOT DELETE THIS LINE -- make depend needs it

Monday, April 30, 2012

Ubuntu

1. Install Apache
$ sudo apt-get install apache2
www folder is at /var/www
2.Install ssh server
sudo apt-get install openssh-server
https://help.ubuntu.com/10.04/serverguide/openssh-server.html



Thursday, April 26, 2012

How to install network card driver in linux

1. Check the physical network card
lspci
2. Check the startup device loading information
dmesg
3. clear network interfaces /etc/sysconfig/network-scripts
4. enable the dirver

sudo modprobe 8139too
dmesg | grep 8139
ifconfig

5. restart network service
/etc/init.d/network restart
service network restart

http://ubuntuforums.org/showthread.php?t=1754699
http://www.cyberciti.biz/faq/linux-command-to-remove-virtual-interfaces-or-network-aliases/