i**p 发帖数: 902 | 1 【 以下文字转载自 JobHunting 讨论区 】
发信人: isup (No), 信区: JobHunting
标 题: C++ JNI code to invoke native method – header an
发信站: BBS 未名空间站 (Tue Jul 14 11:25:50 2015, 美东)
1. Java class implements the interface,for example, Test.java
2. C++ JNI code to invoke native methods – header and implementation
大家怎么理解这个要求?native method本身就是JNI code,难道问的是JNI code调用
JAVA object的native method?那也是调用自己啊。 |
|
i**p 发帖数: 902 | 2 1. Java class implements the interface,for example, Test.java
2. C++ JNI code to invoke native methods – header and implementation
大家怎么理解这个要求?native method本身就是JNI code,难道问的是JNI code调用
JAVA object的native method?那也是调用自己啊。 |
|
w*******e 发帖数: 285 | 3 我现在需要在hive里面用我自己的serde,然后我希望serde能用jni调用我线程的c++程
序,这样我就不需要用java重写。
我现在遇到的问题是首先运行add jar 可以把 serde本身的jar 文件加进去,但是再用
add file 把我的jni xxx.dll或者libxxx.so加上运行就总是找不到class。我在网上
search了半天也没有找到类似的,请问有什么例子可以参考吗?我似乎看到过peking2
有一篇blog讲过hadoop call jni的方法,但是现在找不到在哪里了?多谢帮忙。
add jar xxxSerDe.jar
add file xxx.dll
CREATE EXTERNAL TABLE ...
ROW FORMAT SERDE 'com.cloudera.hive.serde.XXXSerDe' |
|
b******e 发帖数: 1861 | 4 【 以下文字转载自 Java 讨论区 】
发信人: bigmouse (bigmouse), 信区: Java
标 题: 急问,如何创建wince版的jni library
发信站: BBS 未名空间站 (Sat Mar 17 23:14:57 2007)
我有一个应用是java+jni+c library的。以前我是在windows下做的。现在要移植到
wince上。应该怎么办。要怎么创建wince版的jni library呢。sun java me可以么。 |
|
i**p 发帖数: 902 | 5 1. Java class implements the interface,for example, Test.java
2. C++ JNI code to invoke native methods – header and implementation
大家怎么理解这个要求?native method本身就是JNI code,难道问的是JNI code调用
JAVA object的native method?那也是调用自己啊。 |
|
b******e 发帖数: 1861 | 6 我有一个应用是java+jni+c library的。以前我是在windows下做的。现在要移植到
wince上。应该怎么办。要怎么创建wince版的jni library呢。sun java me可以么。 |
|
m*p 发帖数: 1331 | 7 【 以下文字转载自 Java 讨论区 】
发信人: mbp (Mac Book Pro), 信区: Java
标 题: JNI 诡异问题求教
发信站: BBS 未名空间站 (Sun Jul 24 23:15:57 2011, 美东)
最近需要用到jni,在java里面调用已有的cpp代码, 自己在学,
http://java.sun.com/developer/onlineTraining/Programming/JDCBoo
新建了ReadFile.java文件之后,在windows console用
javac ReadFile
编译通过,生成了一个ReadFile.class文件
但是诡异的是,下一步是需要生成一个 header文件,命令如下:
JavaNativeTest\src>javah ReadFile
error: cannot access ReadFile
class file for ReadFile not found
javadoc: error - Class ReadFile not found.
Error: No classes were speci... 阅读全帖 |
|
x*x 发帖数: 156 | 8 最近做了一个小的proj, 把一个 C++的库用jni包起来,然后在Java中调用;可是在JVM
中,内存总是不断涨到16GB, 实际上C++用的内存不过400 MB. 是不是我在compile
JNI的包的时候需要一些设置? |
|
D********g 发帖数: 30 | 9 有一点在用JNI的时候一定要注意,在把Java的type转换成native type以后,等native
type的变量用完以后,要调用相应的release函数释放内存,否则有可能导致(不是一
定会)内存泄漏而可能导致你这样的问题。比如:
JNI C++ side:
const char *nativeString = env->GetStringUTFChars(javaString, 0);
//Do something with the nativeString
//DON'T FORGET THIS LINE!!!
env->ReleaseStringUTFChars(javaString, nativeString);
同样, env->GetIntArrayElements(),env->ReleaseIntArrayElements()等等也是。
JVM |
|
D********g 发帖数: 30 | 10 In this case, you want to delete result, not delete localref.
So just use:
delete result;
Why?
Because result points to an object that is locally allocated in native
method, this is not managed by VM and only with native method itself. So you
want to free up memory upon exit from the native method. Otherwise, memory
leaks! However, for the local reference, you don't have to worry about that
since it is automatically freed upon exit from native method. See JNI
official docs for more information:
... 阅读全帖 |
|
x**n 发帖数: 29 | 11 没用JNI,不过看你写一个独立class可以,而
整合到vbj里面不可以的话,并且有大内存的分配.
试试设置运行时候的虚拟机的参数。
java -X
-Xms set initial Java heap size
-Xmx set maximum Java heap size
-Xss set java thread stack size
-Xprof output cpu profiling data
"SIGSEGV 11 segmetation violation
si_signo [11] : SEGV"
好象是数据越界了,访问了不属于它的地址. |
|
j******f 发帖数: 228 | 12 All,
I'm developing a Java program using JNI on HP-UX.
My java program needs to call some native C functions. I've created a shared
library (say, LibA) for those functions. These C functions need to call some
other functions within some static libraries. In other words, it is necessary
to link the shared library LibA with the static libraries. All these are done
successfully. However, I have the following problem:
When I execute the java program, which invokes a function F in LibA, and when
F i |
|
m*p 发帖数: 1331 | 13 最近需要用到jni,在java里面调用已有的cpp代码, 自己在学,
http://java.sun.com/developer/onlineTraining/Programming/JDCBoo
新建了ReadFile.java文件之后,在windows console用
javac ReadFile
编译通过,生成了一个ReadFile.class文件
但是诡异的是,下一步是需要生成一个 header文件,命令如下:
JavaNativeTest\src>javah ReadFile
error: cannot access ReadFile
class file for ReadFile not found
javadoc: error - Class ReadFile not found.
Error: No classes were specified on the command line. Try -help.
明明ReadFile.class, ReadFile.java都在当前目录src\下,不知道为何会出现此问题?
谢谢!! |
|
b******e 发帖数: 13 | 14 I create a simple jni function taking a jint argument, then print that value
out. But when i call this function in java passing a java int variable to
it, it prints out some random number. However if i change jint to jdouble
and pass java double variable, it works fine. Any ideas why? Thanks. |
|
|
|
|
m*****k 发帖数: 731 | 18 他就是想要JNI调c++写的library 吧? |
|
x*x 发帖数: 156 | 19 how to do jvm dump/snapshot when the memory is growing out of bound? the
JNI interface is generated by swig now, and it still have the same problem. |
|
x*x 发帖数: 156 | 20 Thanks a lot! I do have quite a few functions in JNI for env->
GetStringUTFChars, now all are paired with ReleaseStringUTFChars.
There is another set of functions on env->NewStringUTF(), see the example
code here:
=========================
std::vector< std::string >::value_type *result = 0 ;
result = a_function_to_allocate_mem(jobject arg1, int arg2);
jresult= env->NewStringUTF(result->c_str());
return jresult;
=========================
should I also pair it with
env->DeleteLocalRef(arg1_... 阅读全帖 |
|
|
i**p 发帖数: 902 | 22 是不是在问如何用 JNI code创建一个JAVA object,然后通过这个object调用native
method。 |
|
g***e 发帖数: 2 | 23 I am doing JNI stuff on Unix.
Got a coredump.
What is the debug tool good for this matter?
I don't know how to read the txt file that java throws, HELP! |
|
z*******3 发帖数: 13709 | 24 原来如此
也就是说python所谓的实时计算是假的
通过调用c写的lib来实现所谓的实时计算
其核心代码不是python写的,如果是python写的的话
会很慢,但是因为python可以很容易地调用c的lib
所以造成一个错觉,使得python看上去实时计算做得很快一般
java也可以通过jni来调用这些lib
不过jni调用的c库本身跟平台有绑定
所以这多少破坏了java的平台无关的特性
所以一般不建议搞java的人用jni
甚至很多人都不知道有jni的存在
the |
|
z*******3 发帖数: 13709 | 25 原来如此
也就是说python所谓的实时计算是假的
通过调用c写的lib来实现所谓的实时计算
其核心代码不是python写的,如果是python写的的话
会很慢,但是因为python可以很容易地调用c的lib
所以造成一个错觉,使得python看上去实时计算做得很快一般
java也可以通过jni来调用这些lib
不过jni调用的c库本身跟平台有绑定
所以这多少破坏了java的平台无关的特性
所以一般不建议搞java的人用jni
甚至很多人都不知道有jni的存在
the |
|
z****e 发帖数: 54598 | 26 哎,真神就这样,放个炮就跑,我倒是很有兴趣跟大神聊聊天呢
互通有无,来吧,要不我先抛个引子,给大神看看我们在java版讨论的最新成果
发信人: zhaoce073 (迟到早退不思上进的蜥蜴), 信区: Java
标 题: Re: 我来回答一下投行IT的问题
发信站: BBS 未名空间站 (Wed Feb 27 19:14:12 2013, 美东)
原来如此
也就是说python所谓的实时计算是假的
通过调用c写的lib来实现所谓的实时计算
其核心代码不是python写的,如果是python写的的话
会很慢,但是因为python可以很容易地调用c的lib
所以造成一个错觉,使得python看上去实时计算做得很快一般
java也可以通过jni来调用这些lib
不过jni调用的c库本身跟平台有绑定
所以这多少破坏了java的平台无关的特性
所以一般不建议搞java的人用jni
甚至很多人都不知道有jni的存在
the |
|
z****e 发帖数: 54598 | 27 哎,真神就这样,放个炮就跑,我倒是很有兴趣跟大神聊聊天呢
互通有无,来吧,要不我先抛个引子,给大神看看我们在java版讨论的最新成果
发信人: zhaoce073 (迟到早退不思上进的蜥蜴), 信区: Java
标 题: Re: 我来回答一下投行IT的问题
发信站: BBS 未名空间站 (Wed Feb 27 19:14:12 2013, 美东)
原来如此
也就是说python所谓的实时计算是假的
通过调用c写的lib来实现所谓的实时计算
其核心代码不是python写的,如果是python写的的话
会很慢,但是因为python可以很容易地调用c的lib
所以造成一个错觉,使得python看上去实时计算做得很快一般
java也可以通过jni来调用这些lib
不过jni调用的c库本身跟平台有绑定
所以这多少破坏了java的平台无关的特性
所以一般不建议搞java的人用jni
甚至很多人都不知道有jni的存在
the |
|
i**p 发帖数: 902 | 28 There are lots of examples that Android C/C++ native calls Java APIs.
However, all of these examples I have read are the Java APIs call native
first and then the native calls other Java APIs by using the passed JNI-ENV.
How could C/C++ native calls Java APIs without the passed JNI-ENV, or by
anyway to get the JNI-ENV?
Can you give an example or a link for it?
Thanks! |
|
i**p 发帖数: 902 | 29 【 以下文字转载自 Java 讨论区 】
发信人: isup (No), 信区: Java
标 题: Android C/C++ native calls Java APIs
发信站: BBS 未名空间站 (Fri Jul 20 01:22:38 2012, 美东)
There are lots of examples that Android C/C++ native calls Java APIs.
However, all of these examples I have read are the Java APIs call native
first and then the native calls other Java APIs by using the passed JNI-ENV.
How could C/C++ native calls Java APIs without the passed JNI-ENV, or by
anyway to get the JNI-ENV?
Can you give an example or a link for it?
Th... 阅读全帖 |
|
N******K 发帖数: 10202 | 30 在图像法分析领域 涉及到大矩阵运算 Java本身语言不支持 连续存储的多维矩阵(数
组)
所以Java要搞有效的线性代数运算 1. 从轮子开始造 2. JNI调用 eigen/Lapack等c++/
fortran库
就算用2, 大矩阵运算JNI本身的时间消费可以忽略,但是小矩阵用JNI还不如Java写
for循环。这个问题,还得自己造轮子来解决。
即是从轮子开始造,纯Java写的线性代数库比C++/fortran的慢了n倍(有人测试过10倍)
如果就搞搞小图片,你用纯Java的倒也无所谓。 搞大图像的复杂分析,比如配准、分
割、跟踪、识别,比如医学图像分析,C++无论从速度,还是现有的库的支持,都胜过
Java
再说机器学习领域
libsvm就是c++的 现在最火的spase coding得靠C++, deep neural network
learning 得靠C++和GPU
Java搞网站系统应用很好,但是到了算法这个层面,就是不如C++ |
|
d***q 发帖数: 1119 | 31 这个效率问题还是要看具体的例子。
如果仅仅是单纯 计算一下 然后返回。那么使用jni也没啥问题。但是如果有些场景需
要做大量的交互, 不停在边界上进出,这时jvm没法很好优化, 效率反而还不如直接
用java。
我几年前搞过一些gis的东西。之前有一套系统的计算用c++写,然后用jni来调用,问
题特别多。最后全部用java写,现在用了几年也没啥问题,之前还担心性能会降低,后
来发现还好了,没有想象中的慢。
现在对于这种场景,我的做法是把这些 要计算的封装一下,做个服务,用个rpc之类的
call一下就好。不通过jni来调用。容易调试,反正计算部分的耗时要远大于call的开
销。用rpc也无所谓。
如果是一些cpu intensive & io intensive都有的,这时候直接用java来写90%也能
满足,如果还不能满足,就只有上c++了,整个都替换,不过这种极端的情况甚为少见。 |
|
N******K 发帖数: 10202 | 32 在开发交互式算法 更不能用java和c++混合 而应该用单一语言 那就是c++ 这样设置
断点差错也方便
比如用 jna或者jni 调用c++库 出现的问题:
JNA does not support mapping of c++ classes, so if you're using c++ library
you will need a jni wrapper
If you need a lot of memory copying. For example, you call one method which
returns you a large byte buffer, you change something in it, then you need
to call another method which uses this byte buffer. This would require you
to copy this buffer from c to java, then copy it back from java to c. In
this case jni w... 阅读全帖 |
|
d****i 发帖数: 4809 | 33 哈哈,你又来挖语言坑,其实C++和Java就是父子关系,各有所长,Java的JNI确实搞的
不好,以后希望能把JNI重新设计一下,这样现在很多native层的语言都可以通过JNI在
Java中直接调用。 |
|
g*****g 发帖数: 34805 | 34 轮子不断出现,跟需要JNI是俩码事。Hadoop相关的轮子一堆,有几个用到JNI的?
Hadoop本身有一些JNI,那主要是为了其他语言可以访问HDFS。 |
|
d*x 发帖数: 9 | 35 The best I could do was using JNI to invoke the Solaris high resolution
timer. That gave me a resolution of around 30us(the cost mostly being
the JNI overhead and Solaris System call overhead).
On i386 platforms, you can directly get hardware clock from a register.
This register exists on Pentium+ machines, it records every tick of the
CPU. So for a 1GHz box, that is a nanosec resolution timer(well, not
exactly, there will be some overhead in reading the register). This wouldn't
help in Java tho |
|
r**i 发帖数: 2328 | 36 I used to call C/C++ methods from Java side, now I want to use JNI to call
Java methods from C/C++. The code segment is like below:
#include
#include
#include
#ifdef _WIN32
#define PATH_SEPARATOR ';'
#else
#define PATH_SEPARATOR ':'
#endif
int main()
{
JavaVMOption options[1];
JavaVM *jvm;
JavaVMInitArgs vm_args;
long status;
JNIEnv *env;
jclass cls;
jmethodID mid;
jint square;
jboolean not;
options[0].optionString = "-Djava.class.path=.";
//memset(&vm_args, 0, size |
|
F****n 发帖数: 3271 | 37 I would say use JNI. These COM-Java bridge stuffs are really buggy. I have
used them before one time and will never touch them. Actually JNI is very good
and powerful from the functional perspective.
need
this
whether
to |
|
B******N 发帖数: 445 | 38 你这叫杀鸡用宰牛刀。Corba基本要死掉了个人觉得。
如果不想分开的话。当然用JNI call dll。复杂的business logic要在dll里。调用不能
太频凡。JNI还是expensive的。
oracle
purpose, we
to
tool |
|
v**d 发帖数: 186 | 39
it is much more difficult to write complex network program in c++ ......
it is not hard to use JNI, i just hate to use JNI as that looks really stupid
told you that it is not me who makes the decision......
and commerical applications are far more complex than those course projects,
and lot of $$ have been invested on the dev and maintenance, no one can
easily migrate a huge project from c++ to java in a short time (FYI there are
more than 1,000 c++ classes in the product i am working on) |
|
Z****e 发帖数: 2999 | 40 我猜那个plug-in用了JNI,结果crash了JVM,从JNI的native library里面crash JVM还
是挺容易的,呵呵... |
|
z***h 发帖数: 405 | 41 JNI非常容易crash JVM,稍微有点memory problem就有可能crash JVM
IBM JRE implementation does have some thing different with Sun JRE, JNI
implementation 里就有一些不一样。 |
|
S****h 发帖数: 558 | 42 Well. Back from the interview. They told me that they have the calculation
in C++, wrapped with JNI. Each node runs one instance (JNI+C++) and the
parallel is managed by java. |
|
g*****g 发帖数: 34805 | 43 JNI is your friend, and if you feel JNI is too hard for you,
maybe webservice will do. |
|
f**y 发帖数: 138 | 44 Believe it or not signal handling is not that easy. If all I want are
some web applications, certainly I don't need to worry about signal
handling. I have seen lots of applications with broken signal
handling particularly in C/C++ where a common mistake is to call
syslog in signal handler.
A way I could implement the signal handling in my jni is to install
a simple handler. All the handler doing upon receiving signals is to
set global flags. The java code then calls the jni repeatedly to
poll th |
|
D***h 发帖数: 183 | 45 【 以下文字转载自 Java 讨论区 】
发信人: Depth (等待), 信区: Java
标 题: error C2223: left of '->GetEnv' must point to struct/union
发信站: BBS 未名空间站 (Sat Aug 7 13:59:12 2010, 美东)
error C2223: left of '->GetEnv' must point to struct/union
直接copy的代码,应该不可能错,jni.h里面也是把JavaVM定义为struct了,为什么还会
有这样的错?
#include
jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
JNIEnv* env = NULL;
jint result = -1;
if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
return -1;
}
jniRegisterNativeMethods |
|
g*****g 发帖数: 34805 | 46 给微软抹粉真是嘛都敢说。
http://www.javaworld.com/javaworld/jw-10-1997/jw-10-lawsuit.htm
Specifically, Microsoft made the choice last week to ship products it claims
are fully Java 1.1 compliant, but which failed to pass the Java 1.1
compatibility tests the company received from Sun in February.
Specifically, Microsoft does not support the Java Native Interfaces (JNI)
or the Remote Method Invocation (RMI), and it has altered the Core Java
class libraries with about 50 methods and 50 fields that are not part... 阅读全帖 |
|
d***q 发帖数: 1119 | 47
jython现在还无法和python比较性能, 我有个协议的解析程序。jython 只有大约 1/3
-1/2的性能。如果jython下一个版本能有效利用 jvm的dynamic invoke指令,那么说不
定会
有所提高。
jython用jni来调用c就太迂回了,先不说jni大多数时候效率并不高,而且如果要用c,
干吗要用jython? 如果是cpu-intensive,这里面也要看是什么类型的计算,有部分可
以用java写,性能也不差。 |
|
|
x****u 发帖数: 44466 | 49 当年的M$的com机制比JNI强几条大街,JNI就是TMD临时工作品。
问题是当年SUN的主张就是你不是和尚不能摸尼姑。 |
|