论坛首页 Java企业应用论坛

被隐藏了的JNA

浏览 4706 次
该帖已经被评为隐藏帖
作者 正文
   发表时间:2010-12-01   最后修改:2011-03-14
jni没用过,接触一下jna
如果在windows下
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
public class HelloWorld {
	public interface CLibrary extends Library {
		CLibrary INSTANCE = (CLibrary) Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"), CLibrary.class);
		void printf(String format, Object... args);
	}
	public static void main(String[] args) {
		CLibrary.INSTANCE.printf("in windows\n");
		for (int i = 0; i < args.length; i++) {
			CLibrary.INSTANCE.printf("Argument %d: %s\n", i, args[i]);
		}
	}
}

如果在linux下
顺便把c复习一下,准备做一个testadd的动态库,先测一下动态库是否好使,
先写3个文件test.h test.c testadd.c
test.h
#include <stdio.h>
void add();

test.c
#include <stdio.h>
#include "test.h"
int main()
{
        printf("haha\n");
        add();
}

testadd.c
#include <stdio.h>
#include <unistd.h>
void add()
{
        char name[65];
        printf("this is add \n");
        gethostname(name, sizeof(name));
        printf("hostname=%s\n",name);
        system("ls");
}


生成libtestadd.so
测试一下是否好使
[root@redhat5 c]# gcc -fpic -shared -o libtestadd.so testadd.c
[root@redhat5 c]# gcc -o test test.c -I. -L. -ltestadd
[root@redhat5 c]# ./test 
haha
this is add 
hostname=122226
a.out     libtestadd.so  TestAddService.class          test.h
biji.txt  main.c         TestAddService.java           y.tab.c
JNA.chm   test           TestAddService$TestAdd.class
jna.jar   testadd.c      test.c
[root@redhat5 c]# 

yeh,"this is add"是libtestadd.so打印的,
现在用jna调用libtestadd.so
把jna.jar放到这个目录下
编写TestAddService.java
import com.sun.jna.Library;
import com.sun.jna.Native;
public class TestAddService {
	public TestAddService() {
	}
	public interface TestAdd extends Library {
		TestAdd INSTANCE = (TestAdd) Native.loadLibrary("testadd",TestAdd.class);
		public void add();
	}
	public static void main(String[] args) {
		TestAdd.INSTANCE.add();
		System.out.println("哦 yeh yeh jna in linux");
	}
}

[root@redhat5 c]# javac -cp .:jna.jar TestAddService.java 
[root@redhat5 c]# java -cp .:jna.jar TestAddService       
this is add 
hostname=122226
a.out     libtestadd.so  TestAddService.class          test.h
biji.txt  main.c         TestAddService.java           y.tab.c
JNA.chm   test           TestAddService$TestAdd.class
jna.jar   testadd.c      test.c
哦 yeh yeh jna in linux

完了,java定义一个c接口就完了



------------------------------
难过啊,被隐藏了,还没被喷够呢!
简单方法难道不是好方法?
webservice是很优雅,不闲麻烦?如果是同一台机器也要走http协议?
java和c的互相调用只是一个接口 ,未必要传指针吧,性能部分当然用c解决,java只是简单的调用一下怎么了?
庆幸有人告诉了ACE,算是没白丢30分
Adaptive Communication Environment(自适配通信环境),简称ACE。为一个以C++的Template技术所做成的开放源代码的可跨平台的网络应用程序的程序库套件。它提供了socket/threading/memory management等多种系统调用的面对对象的wrapper,使C++通信软件开发更加简单

libevent轻量级一些








   发表时间:2010-12-01  
这样的例子基本没意义,至少要看到解决结构、指针、指针的指针这些复杂的东西才有意义。

比如这个很简单的例子

typedef struct _Point {
  int x, y;
} Point;

Point translate(Point pt, int dx, int dy);

翻译成JNA就很壮观了:

class Point extends Structure {
    public static class ByValue extends Point implements Structure.ByValue { }
    public int x, y;
}

Point.ByValue translate(Point.ByValue pt, int x, int y);
...
Point.ByValue pt = new Point.ByValue();
Point result = translate(pt, 100, 100);


通过这些复杂结构的例子,JNA是不是值得学就见仁见智了。
1 请登录后投票
   发表时间:2010-12-01  
我调用 MFC 编写的 DLL,直接报出一个错来:
#
# An unexpected error has been detected by Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x78212c5b, pid=1448, tid=5092
#
# Java VM: Java HotSpot(TM) Client VM (1.6.0-rc-b66 mixed mode)
# Problematic frame:
# C  [MFC80D.DLL+0x42c5b]
#
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
#

---------------  T H R E A D  ---------------

Current thread (0x00038c00):  JavaThread "main" [_thread_in_native, id=5092]

siginfo: ExceptionCode=0xc0000005, reading address 0x00370036

Registers:
EAX=0x7400656e, EBX=0x2715158c, ECX=0x7400656e, EDX=0x00370026
ESP=0x008bf5cc, EBP=0x008bf5d4, ESI=0x008bf608, EDI=0x008bf7e8
EIP=0x78212c5b, EFLAGS=0x00010216

Top of Stack: (sp=0x008bf5cc)
0x008bf5cc:   783e97f0 00000000 008bf5ec 78211c6d
0x008bf5dc:   27132e80 008bf608 783e97f0 27132e80
0x008bf5ec:   008bf5fc 78212dd3 008bf804 008bf608
0x008bf5fc:   008bf7f8 27152402 008bf804 008bf7d4
0x008bf60c:   ec7493d5 008bf8e0 00000000 2715158c
0x008bf61c:   cccccccc cccccccc cccccccc cccccccc
0x008bf62c:   008bf608 cccccccc cccccccc cccccccc
0x008bf63c:   cccccccc cccccccc cccccccc cccccccc

Instructions: (pc=0x78212c5b)
0x78212c4b:   00 00 8b 45 08 8b 08 8b 55 08 8b 02 8b 11 8b c8
0x78212c5b:   8b 42 10 ff d0 89 45 f8 8b 4d 08 e8 35 f2 ff ff


Stack: [0x008a0000,0x008c0000),  sp=0x008bf5cc,  free space=125k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C  [MFC80D.DLL+0x42c5b]
C  [MFC80D.DLL+0x41c6d]
C  [MFC80D.DLL+0x42dd3]
C  [NetUtil.dll+0x12402]
C  [jna596.dll+0xcbe7]
C  [jna596.dll+0xc7d9]
C  [jna596.dll+0x4561]
C  [jna596.dll+0x4ec1]
j  com.sun.jna.Function.invokePointer(I[Ljava/lang/Object;)Lcom/sun/jna/Pointer;+0
j  com.sun.jna.Function.invokeString(I[Ljava/lang/Object;Z)Ljava/lang/String;+3
j  com.sun.jna.Function.invoke([Ljava/lang/Object;Ljava/lang/Class;Z)Ljava/lang/Object;+517
j  com.sun.jna.Function.invoke(Ljava/lang/Class;[Ljava/lang/Object;Ljava/util/Map;)Ljava/lang/Object;+214
j  com.sun.jna.Library$Handler.invoke(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;+341
j  $Proxy0.GetRespone(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;+20
j  CallDll.main([Ljava/lang/String;)V+7
v  ~StubRoutines::call_stub

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  com.sun.jna.Function.invokePointer(I[Ljava/lang/Object;)Lcom/sun/jna/Pointer;+0
j  com.sun.jna.Function.invokeString(I[Ljava/lang/Object;Z)Ljava/lang/String;+3
j  com.sun.jna.Function.invoke([Ljava/lang/Object;Ljava/lang/Class;Z)Ljava/lang/Object;+517
j  com.sun.jna.Function.invoke(Ljava/lang/Class;[Ljava/lang/Object;Ljava/util/Map;)Ljava/lang/Object;+214
j  com.sun.jna.Library$Handler.invoke(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;+341
j  $Proxy0.GetRespone(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;+20
j  CallDll.main([Ljava/lang/String;)V+7
v  ~StubRoutines::call_stub

---------------  P R O C E S S  ---------------

Java Threads: ( => current thread )
  0x26dd3800 JavaThread "Low Memory Detector" daemon [_thread_blocked, id=4892]
  0x26dd1000 JavaThread "CompilerThread0" daemon [_thread_blocked, id=5556]
  0x26dd0000 JavaThread "Attach Listener" daemon [_thread_blocked, id=2232]
  0x26dcf000 JavaThread "Signal Dispatcher" daemon [_thread_blocked, id=2524]
  0x26d3f000 JavaThread "Finalizer" daemon [_thread_blocked, id=2572]
  0x26d3e000 JavaThread "Reference Handler" daemon [_thread_blocked, id=5404]
=>0x00038c00 JavaThread "main" [_thread_in_native, id=5092]

Other Threads:
  0x26d3b000 VMThread [id=3156]
  0x26dd6800 WatcherThread [id=3992]

VM state:not at safepoint (normal execution)

VM Mutex/Monitor currently owned by a thread: None

Heap
PSYoungGen      total 57344K, used 1966K [0x22970000, 0x26970000, 0x26970000)
  eden space 49152K, 4% used [0x22970000,0x22b5b8d8,0x25970000)
  from space 8192K, 0% used [0x26170000,0x26170000,0x26970000)
  to   space 8192K, 0% used [0x25970000,0x25970000,0x26170000)
PSOldGen        total 458752K, used 0K [0x06970000, 0x22970000, 0x22970000)
  object space 458752K, 0% used [0x06970000,0x06970000,0x22970000)
PSPermGen       total 12288K, used 3030K [0x02970000, 0x03570000, 0x06970000)
  object space 12288K, 24% used [0x02970000,0x02c659d0,0x03570000)

Dynamic libraries:
0x00400000 - 0x00410000 D:\develop_tools\Java\jdk1.6.0\bin\java.exe
0x7c920000 - 0x7c9b6000 C:\WINDOWS\system32\ntdll.dll
0x7c800000 - 0x7c91e000 C:\WINDOWS\system32\kernel32.dll
0x6d010000 - 0x6d01a000 D:\develop_tools\Java\jdk1.6.0\bin\jli.dll
0x7c340000 - 0x7c396000 D:\develop_tools\Java\jdk1.6.0\bin\MSVCR71.dll
0x77da0000 - 0x77e49000 C:\WINDOWS\system32\ADVAPI32.dll
0x77e50000 - 0x77ee3000 C:\WINDOWS\system32\RPCRT4.dll
0x77fc0000 - 0x77fd1000 C:\WINDOWS\system32\Secur32.dll
0x6d770000 - 0x6d9a6000 D:\develop_tools\Java\jdk1.6.0\jre\bin\client\jvm.dll
0x77d10000 - 0x77da0000 C:\WINDOWS\system32\USER32.dll
0x77ef0000 - 0x77f39000 C:\WINDOWS\system32\GDI32.dll
0x76b10000 - 0x76b3a000 C:\WINDOWS\system32\WINMM.dll
0x76300000 - 0x7631d000 C:\WINDOWS\system32\IMM32.DLL
0x62c20000 - 0x62c29000 C:\WINDOWS\system32\LPK.DLL
0x73fa0000 - 0x7400b000 C:\WINDOWS\system32\USP10.dll
0x6d2f0000 - 0x6d2f8000 D:\develop_tools\Java\jdk1.6.0\jre\bin\hpi.dll
0x76bc0000 - 0x76bcb000 C:\WINDOWS\system32\PSAPI.DLL
0x6d720000 - 0x6d72c000 D:\develop_tools\Java\jdk1.6.0\jre\bin\verify.dll
0x6d380000 - 0x6d39f000 D:\develop_tools\Java\jdk1.6.0\jre\bin\java.dll
0x6d760000 - 0x6d76f000 D:\develop_tools\Java\jdk1.6.0\jre\bin\zip.dll
0x270d0000 - 0x27125000 D:\sys_files\TEMP\jna596.dll
0x77be0000 - 0x77c38000 C:\WINDOWS\system32\msvcrt.dll
0x27140000 - 0x2716f000 D:\sys_files\desktop\JAVA_Proxy\bin\NetUtil.dll
0x781d0000 - 0x78416000 C:\WINDOWS\WinSxS\x86_Microsoft.VC80.DebugMFC_1fc8b3b9a1e18e3b_8.0.50727.42_x-ww_c8452471\MFC80D.DLL
0x27180000 - 0x272a0000 C:\WINDOWS\WinSxS\x86_Microsoft.VC80.DebugCRT_1fc8b3b9a1e18e3b_8.0.50727.42_x-ww_f75eb16c\MSVCR80D.dll
0x77f40000 - 0x77fb6000 C:\WINDOWS\system32\SHLWAPI.dll
0x770f0000 - 0x7717b000 C:\WINDOWS\system32\OLEAUT32.dll
0x76990000 - 0x76ace000 C:\WINDOWS\system32\ole32.dll
0x5d360000 - 0x5d36a000 C:\WINDOWS\WinSxS\x86_Microsoft.VC80.MFCLOC_1fc8b3b9a1e18e3b_8.0.50727.4053_x-ww_0ccc058c\MFC80CHS.DLL

VM Arguments:
jvm_args: -Xms512M -Xmx512M -Xss128k -XX:+AggressiveOpts -XX:+UseParallelGC -XX:NewSize=64M
java_command: CallDll
Launcher Type: SUN_STANDARD

Environment Variables:
JAVA_HOME=D:\develop_tools\Java\jdk1.6.0
CLASSPATH=.;D:\develop_tools\Java\jdk1.6.0\lib\tools.jar;D:\develop_tools\Java\jdk1.6.0\lib\dt.jar;D:\develop_tools\Java\jdk1.6.0\lib\jardiff.jar;D:\develop_tools\Java\jdk1.6.0\lib\jnlp.jar;D:\develop_tools\Java\jdk1.6.0\htmlconverter.jar;D:\develop_tools\Java\jdk1.6.0\lib\jnlp-servlet.jar;D:\DEVELO~1\DB2\IBM\SQLLIB\java\db2java.zip;D:\DEVELO~1\DB2\IBM\SQLLIB\java\db2jcc.jar;D:\DEVELO~1\DB2\IBM\SQLLIB\java\sqlj.zip;D:\DEVELO~1\DB2\IBM\SQLLIB\java\db2jcc_license_cu.jar;D:\DEVELO~1\DB2\IBM\SQLLIB\bin;D:\DEVELO~1\DB2\IBM\SQLLIB\java\common.jar
PATH=D:\develop_tools\Java\jdk1.6.0\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Intel\WiFi\bin\;d:\IsoBuster;
USERNAME=Administartor
OS=Windows_NT
PROCESSOR_IDENTIFIER=x86 Family 6 Model 23 Stepping 10, GenuineIntel



---------------  S Y S T E M  ---------------

OS: Windows XP Build 2600 Service Pack 3

CPU:total 2 family 6, cmov, cx8, fxsr, mmx, sse, sse2

Memory: 4k page, physical 2097151k(1295464k free), swap 4194303k(4194303k free)

vm_info: Java HotSpot(TM) Client VM (1.6.0-rc-b66) for windows-x86, built on Jan  5 2006 11:41:03 by "java_re" with unknown MS VC++:1310

0 请登录后投票
   发表时间:2010-12-01  
有意义???
0 请登录后投票
   发表时间:2010-12-01   最后修改:2010-12-01
感谢楼上几位的回复,尤其感谢ray_linn,java和c的交互,兼顾性能和易用性不知道是否有一些更稳定的技术?
0 请登录后投票
   发表时间:2010-12-01  
- -!,此贴要被隐藏。
0 请登录后投票
   发表时间:2010-12-01  
ICE吧 这个简单
0 请登录后投票
   发表时间:2010-12-01  
一点都不装,java调C,可以更优雅些,比如通过webservice,没有必要调得那么生硬。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics