Tag Archives: java classpath

Java Native Interface – Java和C/C++的結合

很多情況下需要Java和C/C++結合一起來編程

例如本來用C/C++寫的lib/program
為了Multi-platform就用了Java Native Interface
外層的Java包著一個C/C++ shared lib
當轉換平台時就把shared lib轉了compilation setting重新compile
然後把shared lib file直接更換了就可以

又例如本來用Java時需要C/C++的lib
因為C/C++的效率較高
或者一個特別的情況:Cocos2D-X
用C/C++在Android和iOS兩個平台上執行
而在Android中用C/C++的方法就是Java Native Interface了

Ref: http://en.wikipedia.org/wiki/Java_Native_Interface

P.S. Wiki中用“export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.”確保shared lib linkage,但這不是必需的
在執行時以”java -classpath <path> …”也可以
以減低global variable的使用
減低global variable的原因請看: http://xahlee.info/UnixResource_dir/_/ldpath.html

Java

Note for compile Java on console

Common compilation command:
java -classpath bin:/path/a:/path/b/classes.jar -d bin -sourcepath src
/src/com/PackageNameA/*.java /src/com/PackageNameB/*.java
Explanation:
-classpath <paths>  -  a list of folders/JARs separated by “:”. All class file will be included for compilation. Usually, class path should include the destination of class file of the project to include other modules
-b <path>  -  the destination of class file to be produced
-sourcepath <path>  -  the source file folder. The java files there may be compiled by auto compilation
ended with a list of source files to be compiled, separated by space

Usually, class path should include Java Runtime library
Linux: ${JRE_PATH}/lib/rt.jar
MacOS: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar
Different version of Java may has different path of the library