2012年10月26日 星期五

Bash shell string Manual and simple usage

Introduction:
It is often use the shell variable to do something.


1. Count the length
stringZ=abcABC
${#stringZ}


2. Substring
${stringZ:0}





Reference:
linux_String_Manual





2012年10月25日 星期四

Executable Ant with other other jar in on jar with java.lang.NoClassDefFoundError.

1. Problem:
   How do you execute a jar file, and the other jar as the library in this executable jar? If you execute this executable jar with java -jar this.jar , it will appear a exception with java.lang.NoClassDefFoundError. But Why? it is also happened when you write down the Class-Path property with this reference library's jar in the manifest.mf file.

2. Why:
  Because JVM didn't execute a jar with loadding it's classpath in this jar,
it means even your jar has the lib folder with necessary jar, it didn't work.


3. Solution:
  (A) The simple way is to unzip the lib in jar, and then execute the jar. Because it extract the lib folder in the executable Jar, and use java to execute it, the classpath will reference lib/*, then all class will find in your executeaJar. The condition is you need wirte the reference lib in the manifest.mf.
        unzip canExecute.jar  lib/*;
        java -jar canExecute.jar;

  (B) Eclipse provide a exporting the executable jar function. Its method is include a classloader called JarRsrcLoader, and firstly jvm will load this loader which can load other jar. then load you class. But this method is depends on your IDE.

  (C) A package called One-Jar is support some solution. But it need modify your source call to load other jar file and your manifest.


Reference:
JVM with classloader
JVM with classloader written by caterpillar
JVM with claspath
One-JAR_intro
One_Jar_Office
ClasLoader with third-part
Manifest Intro 
Ant with manifest
Ant with manifest having lib
Ant with manifest_3_relavent the jar_other jar


2012年10月23日 星期二

getResource() function with classpath

1. Why use getResource() function ? 
    Sometimes there is a need for you to get file, but in java the constructor of File class need the absolute path, but it is depends on the machine, it will be a problem when you didn't know the actual parent path in other computer.
    Therefore, you need a method supporting defining the different root staring path, and can get the file. That is the answer of the  getResource function.


2. where is the relevant searching path in getResource()?
  1.  package as the staring path
  2.  classpath as the staring path

3. Example
File file3 = new File(Test.class.getResource("file3.txt").getFile())



Reference:
http://gavin-chen.iteye.com/blog/261151
 

2012年10月12日 星期五

記憶體漏失 Memory Leak

記憶體漏失 Memory Leak


Memory Leak造成的原因是某個被配置(allocated)的記憶體無法在被參照(referenced),也無法被釋放(released);那塊被配置的 記憶體就有如記憶體孤兒般,無法被系統再使用,所以要看一個程式有否Memory Leak,很簡單的方法就是去看作業系統(For standalone application)或是Application Server(如果是Web Application)的實體記憶體使用圖,如果隨著時間增加,記憶體的使用量呈現明顯增加的趨勢,這個程式就極有可能有潛在的Memory Leak問題。
 
Memory Leak在C/C++語言中是很常見的人為過失,因為C/C++並沒有自動Garbage Collector (垃圾收集器)的機制,程式設計師必需在使用完資源後,人為釋放資源。雖然在Java、C#有自動Garbage Collector的機制,Memory Leak的機會大幅下降,但仍然不能完全倖免,只要程式設計師不小心仍然會造成Memory Leak

Reference
 Memory Leak








rhino for print and readln function

1. what is rhino ??


Rhino is a javascript interpreter which use java language to develop.
In rhino you can import java package and call java package.

2. rhino shell

you can enter js, and type help() to view the function.

3. rhino print

can use print("expression") function;

4. rhino input





var readln = (typeof readline === 'function') ? (readline) : (function() {
     importPackage(java.io);
     importPackage(java.lang);
     var stdin = new BufferedReader(new InputStreamReader(System['in']));

     return function() {
         return String(stdin.readLine());  // Read line, 
     };                                    // force to JavaScript String
 }());
 
 
 
Reference:
 input
 output



2012年10月2日 星期二

Newline thransform Linux \n to \r\n



Command:
sed -e 's/$/\r/'



Reference:
http://superuser.com/questions/419865/how-can-i-replace-all-r-n-with-n-using-either-komodo-edit-on-osx-snow-leopard