ubuntu 在 R40e 上 還有 Debian 在 Sempron 2600 上

顯示具有 java 標籤的文章。 顯示所有文章
顯示具有 java 標籤的文章。 顯示所有文章

2014年12月3日 星期三

ant 就是 Make ..

用來build java project 的 tool : apache ant
角色上就跟用來 build c project 的 Make 一樣。

那為什麼要特別做一個 tool 呢?
有什麼是 Make 做不到的 ?

這一篇有說明:

大概是因為啟動 javac 的時間很長,所以compile java 傾向於一次 compile 很多 java file.
和 c 一次compile 一個差異很大。

加上 target-depends rules 的更動,不太容易處理階層性的 source code.
所以才重寫了 make 的 tool

所以,就把 ant 當 make 就好了


make 是吃 Makefile
ant 是吃 build.xml (default)

是 xml 格式。
大約也跟 makefile 一樣,定義變數,定義 target, 然後有 action

同意也有 include 的功能。
還有一些定義另外分別開來,叫 property file, 或 config 檔,。

啟動ant 的 command 大概就是:
ant -v -f build.xml
加上 -v 是output message,好方便 verify 動作。


android software developement kit 也使用 ant 作為 build system (雖然後來又改用 grandle)
所以sdk 中有一個 folder: sdk/tools/ant, 裡面有一些寫好的 xml,
讓 developer 直接使用,不用自己再寫 build.xml



ant 雖然是為java 而做,但是他在包裝 jar 時,不會自動 copy resource file (png, wav,..etc)。
所以 jar 中用 getResource("icon.png"), 常常會出現 nullpointerexception.
因為 ant 在包 jar 時,沒有把 icon.png 也包進去。

一般來說,resource 所在位置,就是要跟使用 getResource( ) 的那個 class 在相同的目錄。
ref: http://stackoverflow.com/questions/804150/ant-compile-doesnt-copy-the-resources

就是在 jar 的 action 加上.. https://github.com/checko/androidscreencast_org/commit/fe9083623469a2e8e533179226766b84bfcd09a9

2014年12月2日 星期二

install run-java6 on Debian Jessie

去 http://ghaffarian.net/downloads/Java/JDK/ 下載 jdk-6u45-linux-x64.bin (因為到 oracle 下載要註冊).

ref: http://unix.aspcode.net/view/635395087004115229152482/install-java-sdk-6-wheezy

debiand 沒辦法直接提供 oracle java package (因為 license),但是提供了把 Orcale java 包裝成 deb 的 tool:
apt-get install java-package
包裝 jdk-6u45-linux-x64.bin:
make-jpkg jdk-6u45-linux-x64.bin
安裝剛剛包好的 deb:
dpkg -i oracle-java6-jdk_6u45_amd64.deb

Selecting previously unselected package oracle-java6-jdk.
(Reading database ... 169950 files and directories currently installed.)
Preparing to unpack oracle-java6-jdk_6u45_amd64.deb ...
Unpacking oracle-java6-jdk (6u45) ...
Setting up oracle-java6-jdk (6u45) ...
update-alternatives: using /usr/lib/jvm/jdk-6-oracle-x64/jre/bin/ControlPanel to provide /usr/bin/ControlPanel (ControlPanel) in auto mode
update-alternatives: using /usr/lib/jvm/jdk-6-oracle-x64/jre/lib/amd64/libnpjp2.so to provide /usr/lib/mozilla/plugins/libjavaplugin.so (mozilla-javaplugin.so) in auto mode

會裝在 /usr/lib/jvm/jdk-6-oracle-x64

然後就是設定 default :
sudo update-java-alternatives -s jdk-6-oracle-x64
然後會 complain 一下 這個 folder 有一些 tool 沒提供。

2012年12月12日 星期三

Messge.java

Message.java

在這個 class 裡,實做了一個小小的 Messge cache.

當你 new 一個 Messge,用完,把他 recycle,不是 free 掉,
這樣,下一個需要 new Message 時,就直接從 recycle pool 拿,不用 free 掉。

2012年12月3日 星期一

java native interface : jni

Java call c function 的歸定。
就是在 C 寫 function 讓 java call

要作 java call C function ,有兩種方法:
  • 利用javah 產生的 header,實做 c function 。 -- 這是用 java vm 的標準 mapping 方式
  • 自己在 c function 寫 JNI_OnLoad( ), 在裡面寫明 mapping 方式。

ref:
  • http://www.xyplot.com/jni.simple.htm
  • http://hi.baidu.com/jfojfo/item/1011ac02d8e0cd813d42e27b
  • http://en.wikipedia.org/wiki/Java_Native_Interface


第一種方法,javah, 標準方式

在 Wiki 寫得很清楚,有嚴格的function name , argument mapping 的規定,
甚至 c 的 header file 都是由 java toolchain 產生的。

可以follow wiki 的 example 作法。

步驟是
  • 先寫 java code,c 的function 要加上 native
  • 用 javac compile 出 class
  • 用 javah 從 class 產生 c 的 header
  • follow c header,新開一個 c source, implement function
  • 用 gcc compile c files


照著作時發生兩個問題:

1. compile c code 時,出現 error
error: jni_md.h: No such file or directory
這個google 一下就可以知道,是 jni_md.h 的位置,是在 include/linux 下,不是 include 下,所以把 include/linux 也加到 include path 就可以。

gcc -I${JAVA_HOME}/include -I${JAVA_HOME}/include/linux -shared libHelloWorld.c -o libHelloWorls.so
然後是..

2.-fPIC

relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
解決方法就和 error 的說明一樣,compile option 加 -fPIC就可以:
gcc -fPIC -I${JAVA_HOME}/include -I${JAVA_HOME}/include/linux -shared libHelloWorld.c -o libHelloWorld.so 這樣就可以了。

標籤

網誌存檔