2013年9月26日 星期四

用Maven執行Java程式

Eclipse 用習慣了,IDE提供方便的操作,也讓我變笨了.

在命令列使用maven要執行JAVA console 程式,有一個好用的plug-in :  exec-maven-plugin

 

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<mainClass>com.clickap.test.NeoSpring</mainClass>
</configuration>
</plugin>
 
使用mvn exec:java 命令就可以執行了
mvn-exec-java

 


提示1


如果你跟我一樣對 pom.xml的schema不熟,不知道plugin要放在哪裡, 這裡有完整的結構.

<project>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<mainClass>com.clickap.test.NeoSpring</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
 

提示2


如果程式需要加*參數*

<project>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>com.clickap.test.NeoSpring</mainClass>
<arguments>
<argument>argument1</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</project>
 
 

2013年9月24日 星期二

影像處理的日子

這張圖很熟悉吼~

lenna

本尊出現在 IS&T 50週年慶

無意中找到這篇在交大的網站上  http://www.csie.nctu.edu.tw/~zlxuan/lena/lenna.html 

是不是勾起了很多回憶 Smile

2013年9月13日 星期五

Maven專案在Eclipse Run on Server

想在Eclipse 操作Maven 2 專案, 現在有比較方便的外掛  :  M2E – WTP
內置圖片 4
M2E-WTP
目前是 1.0.0版 http://wiki.eclipse.org/M2E-WTP/New_and_Noteworthy/1.0.0

0.安裝方法
我直接用 Install New Software方式安裝 網址 : http://www.eclipse.org/m2e-wtp/download/
勾選 m2e 及 m2e-wtp 兩項.
因為我的Helios已經安裝M2E 1.4版了, 所以只要選取 m2e-wtp 這項即可.

1.安裝後, 新增Maven專案


2.在Filter用關鍵字 webapp
選取org.apache.maven.archetypes

3.給命名空間(Group Id)及專案名稱(Artifact Id)


神奇的事發生了, 在專案名稱按右鍵,選取Run on Server
你已經設定好的Tomcat
執行結果

太好了, 以後可以用熟悉的方式 Eclipse 在  Maven 的 Web專案用  Run on Server 了.

要注意的是 M2E-WTP 跟 m2eclipse-wtp 不能同時安裝.

參考自http://www.eclipse.org/m2e-wtp/download/

2013年9月3日 星期二

硬碟空間不夠?

當然清除回收桶, 新增移除程式一定有幫助的.
CCleaner , Sweep 之類的軟體也不錯(google就找到一堆了)
我要介紹的是 SpaceSniffer 如圖


SpaceSniffer
啟動後等一下, 它後幫你把各個資料夾所佔的空間圖形化, 是不是一目了然?
這裡可以下載

分享我的經驗(Windows7)
ProgramData\Microsoft\Search\Data 高達12G, 使用控制台->索引->進階 按下重新索引(Rebuild)後剩下84MB.
ProgramData\Microsoft\OEMOffice14 直接砍掉
ProgramData\Microsoft\HelpLibrary 直接砍掉, 但是在 C:\Program Files\Microsoft Help Viewer\v1.0\HelpLibManager.exe.config 加上一行 <add key="FirstTimeRun" value="True" /> , 下次執行時再詢問.

Java檢查記憶體使用情形

主要是因為出現了 java.lang.OutOfMemoryError: Java heap space

 

JDK6以後附了 Java VisualVM (jvisualvm.exe) 在bin的目錄內, 這裡有詳細說明;

1.Overview

先把你要檢查的Java執行起來, 然後打開jvisualvm(如圖), 在左邊Applications內點選你的JVM(我是用OC4J), 就可以在Overview看到JVM的運作環境及設定的參數.

 

jVisualVm

因為要測heap , 你會發現上圖我有加了一些參數, 這裡說明一下:

-XX:+HeapDumpOnOutOfMemoryError   在出現 OutOfMemoryError 時會產生一個 hprof的檔案(e.g. java_pidxxxxx.hprof, 預設會寫在JVM所在路徑, 也可以加 -XX:HeapDumpPath=yourpath ), 方便我們檢查記憶體使用的情況; 使用方法請看這裡 .

hprof 檔可以用 Eclipse memory analyzer (mat, http://www.eclipse.org/mat/ ) 打開來看, 建議載 Stand-alone Eclipse RCP Applications 的版本比較方便(如圖)

mat-1

-Xms512m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=256m  這幾個參數相信大家都很熟悉, 我就不多說了; 只是 Permanent Size 本來大家都說很小(只要64m), 可是經過 jVisualVM測過, 我發現我的 AP 一下就超過了, 所以我把它改為 128m 起跳; heap size 也把它加到 512m.

 

 

2.切換到Monitor還有圖(chart)

jVisualVm chart

 

接下來就是觀察記憶體使用情形, 然後跟著調整及修正了.

參考自 http://javarevisited.blogspot.sg/2011/09/javalangoutofmemoryerror-permgen-space.html

2013年9月2日 星期一

Difference between instance class and local variables in Java

參考自 http://javarevisited.blogspot.tw/2012/02/difference-between-instance-class-and.html 

There are lot of differences between instance variable, class variable and local variable in Java and knowing them will help you to write correct and bug free Java programs. Java is full featured programming language and provides different kind of variables like static variable also called Class variable since it belongs to whole Class, non static also called instance variable and local variables which varies in scope and value. Thank god Java doesn't have any register variable or auto scope like C otherwise it would have so much detail to remember. static variables are common source of error in may multi-threadedjava program and does require a bit of carefulness while using it. On the other hand instance variable and local variable has less sharing visibility than static variable.

Instance Variable vs Class Variable in Java

let's first see difference between instance variable and class variable also known as non static vs static variable in java. Instance variable are per instance (object) basis. If you have 5 instance of one class you will have five copies of instance variable. these are also referred as non static variable and initialized when you create instance of any object using new() operator or by using other methods like reflection e.g. Class.newInstance(). On the other hand Class variables are declared using static keyword and they have exact same value for every instance. static or class variable are initialized when class is first loaded into JVM memory unlike instance variable which initialized when instance is created. Static variables are similar to global variable in C and can be used to store data which is static in nature and has same value for all instance, but at same static variable also cause subtle concurrency bugs if updated by multiple threads. you can read more about static keywords in my post secrets of static keyword in Java .

Instance variable vs local variable in Java

Now let's see difference between instance variable and local variable.local variables are local in scope and they are not visible or accessible outside there scope which is determined by {} while instance variables are visible on all part of code based on there access modifier e.g. public , private or protected.only public can be accessed from outside while protected and private can be accessed from subclass and class itself. Access modifier can not be applied to local variable and you can not even make them static. only modifier which is applicable to local variable is final and only final local variables are visible inside anonymous class. value of instance variable is limited to instance, one instance can not access value of other instance in Java.

Best practices related to local, instance and static variable in Java

Its good to know some of the best practices related to declaration and use of Variables in Java while learning differences among different type of variable in Java:

  1. Always name your variable as per Java Bean naming convention.
  2. By default give private access to your member variables (both static and instance) and provide more access step by step e.g. from private to protected to package to public. This way you will be following encapsulation principle.
  3. Always declare local variable where you use instead of declaring it on top of method or block.
  4. Don't hide instance or static variable by giving same name to local variable. this may result in subtle programming bugs.
  5. Be consistent with your variable naming convention don't mix different convention from different language e.g. some programming language use first word to denote type of variable e.g. bExit to denote boolean Exit variable or iNumber to denote integer Number variable. Though they are good but mixing simple names as per Java Bean naming convention with this will only lead confusion.
That’s all on difference between local vs instance vs class variable in Java. correct understanding of there scope, value and accessibility is key to write java program.