java in the dynamic library dll called simple method of JNative

2010-08-06  来源:本站原创  分类:Java  人气:188 

Java call DLL method is through the JNI interface, http://www.ibm.com/developerworks/cn/java/l-linux-jni/ detailed explanation here. Is probably the first to write an interface with a Java class, and then use javah to generate a xxx.h of c language header files, and then use C header files to achieve this, implement the interface to call in the other interface has been achieved.

Unzip JNative-1.3.2.zip get three documents, namely: JNativeCpp.dll, libJNativeCpp.so, JNative.jar. JNativeCpp.dll Windows under use, copying it into windows / system32 directory;
libJNativeCpp.so Linux to use the;
JNative.jar This is an expansion pack, loaded into your program can be.

A simple example

import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.exceptions.NativeException;

public class JNativeTest (

public static final int messageBox (int parentHandle, String message,
String caption, int buttons) (/ / throws NativeException, IllegalAccessException (
JNative n = null;
try (
n = new JNative ("User32.dll", "MessageBoxA"); / / ". dll" can not, the value of the constant DLL_NAME User32.dll
/ / Constructor JNative to complete loading User32.dll, and method of positioning MessageBoxA
n.setRetVal (Type.INT); / / specify the type of return parameter
int i = 0;
n.setParameter (i + +, Type.INT, "" + parentHandle);
n.setParameter (i + +, Type.STRING, message);
n.setParameter (i + +, Type.STRING, caption);
n.setParameter (i + +, Type.INT, "" + buttons); / / the specified location on the parameter types and values
n.invoke (); / / call the method
return Integer.parseInt (n.getRetVal ());
)
catch (Exception ex) (
ex.printStackTrace ();
)
finally (

if (n! = null)
try (
n.dispose ();
) Catch (NativeException e) (
/ / TODO Auto-generated catch block
e.printStackTrace ();
) Catch (IllegalAccessException e) (
/ / TODO Auto-generated catch block
e.printStackTrace ();
) / / Remember the release of
)
return 0;
)
public static void main (String [] args) throws NativeException, IllegalAccessException (
JNativeTest.messageBox (100, "which is used jnative practice", "jnativetest", 1);
)
)

Compiled C code on linux Part Description:

For different versions of linux may result in different original tapes libJNativeCpp.so libJNativeCpp.so is glic2.4 compiled under the circumstances in order to meet glic2.3, recompile the libJNativeCpp.so, under the for gcc3.4.6 glibc 2.3.
Compile options:
In JNative-1.3.2-src \ JNativeCpp \ Release directory
1, backup calls.o and asm_io.o two Object file
2, make clean
3, return to the current directory calls.o and asm_io.o two Object file
4, make

Already modified the Release directory of the makefile and subdir.mk file so that when the make clean does not delete the two files calls.o and asm_io.o

Annex: linux dynamic library search path:
export LD_LIBRARY_PATH = `pwd`: $ LD_LIBRARY_PATH

makefile file

################################################## ##############################
# Automatically-generated file. Do not edit!
################################################## ##############################

-Include .. / makefile.init

RM: = rm-rf

# All of the sources participating in the build are defined here
-Include sources.mk
-Include subdir.mk
-Include objects.mk

ifneq ($ (MAKECMDGOALS), clean)
ifneq ($ (strip $ (C + + _DEPS)),)
-Include $ (C + + _DEPS)
endif
ifneq ($ (strip $ (CC_DEPS)),)
-Include $ (CC_DEPS)
endif
ifneq ($ (strip $ (C_DEPS)),)
-Include $ (C_DEPS)
endif
ifneq ($ (strip $ (CPP_DEPS)),)
-Include $ (CPP_DEPS)
endif
ifneq ($ (strip $ (CXX_DEPS)),)
-Include $ (CXX_DEPS)
endif
ifneq ($ (strip $ (C_UPPER_DEPS)),)
-Include $ (C_UPPER_DEPS)
endif
endif

-Include .. / makefile.defs

# Add inputs and outputs from these tool invocations to the build variables

# All Target
all: libJNativeCpp.so

# Tool invocations
libJNativeCpp.so: $ (OBJS) $ (OBJS_ASM) $ (USER_OBJS)
@ Echo''Building target: $ @''
@ Echo''Invoking: GCC C + + Linker''
g + +-shared-o "libJNativeCpp.so" $ (OBJS) $ (OBJS_ASM) $ (USER_OBJS) $ (LIBS)
@ Echo''Finished building target: $ @''
@ Echo''''

# Other Targets
clean:
- $ (RM) $ (OBJS) $ (C + + _DEPS) $ (CC_DEPS) $ (C_DEPS) $ (CPP_DEPS) $ (LIBRARIES) $ (CXX_DEPS) $ (C_UPPER_DEPS) libJNativeCpp.so
- @ Echo''''

. PHONY: all clean dependents
. SECONDARY:

-Include .. / makefile.targets

subdir.mk file

################################################## ##############################
# Automatically-generated file. Do not edit!
################################################## ##############################

# Add inputs and outputs from these tool invocations to the build variables
C_SRCS + =
.. / Jni_util.c
.. / Mem.c

CPP_SRCS + =
.. / CallBack.cpp
.. / WindowProcUtil.cpp
.. / Org_xvolks_jnative_JNative.cpp

ASM_SRCS + =
.. / Asm_io.asm
.. / Calls.asm

OBJS + =
. / CallBack.o
. / WindowProcUtil.o
. / Jni_util.o
. / Mem.o
. / Org_xvolks_jnative_JNative.o

OBJS_ASM + =
. / Asm_io.o
. / Calls.o

C_DEPS + =
. / Jni_util.d
. / Mem.d

CPP_DEPS + =
. / CallBack.d
. / WindowProcUtil.d
. / Org_xvolks_jnative_JNative.d

# Each subdirectory must supply rules for building sources it contributes
%. O: ../%. cpp
@ Echo''Building file: $ <''
@ Echo''Invoking: GCC C + + Compiler''
g + +-I "/ home/gongjan/jdk1.5.0_08/include /"-I "/ home/gongjan/jdk1.5.0_08/include/linux"-O3-Wall-c-fmessage-length = 0-Wl, - -add-stdcall-alias-MMD-MP-MF "$(@:%. o =%. d) "-MT "$(@:%. o =%. d)"-o "$ @" "$ <"
@ Echo''Finished building: $ <''
@ Echo''''

%. O: ../%. asm
@ Echo''Building file: $ <''
@ Echo''Invoking: GCC Assembler''
nasm-f elf-d ELF_TYPE-o "$ @" "$ <"
@ Echo''Finished building: $ <''
@ Echo''''

%. O: ../%. c
@ Echo''Building file: $ <''
@ Echo''Invoking: GCC C Compiler''
gcc-I "/ home/gongjan/jdk1.5.0_08/include/linux"-I "/ home/gongjan/jdk1.5.0_08/include /"-O3-Wall-c-fmessage-length = 0-Wl, - -add-stdcall-alias-MMD-MP-MF "$(@:%. o =%. d) "-MT "$(@:%. o =%. d)"-o "$ @" "$ <"
@ Echo''Finished building: $ <''
@ Echo''''
Article Source: http://www.diybl.com/course/3_program/java/javajs/2008520/117063.html

相关文章
  • java in the dynamic library dll called simple method of JNative 2010-08-06

    Java call DLL method is through the JNI interface, http://www.ibm.com/developerworks/cn/java/l-linux-jni/ detailed explanation here. Is probably the first to write an interface with a Java class, and then use javah to generate a xxx.h of c language h

  • flex (actionscript) with swf dynamic library (dll) 2010-05-28

    In the following code, a class is given public Class property circleClass, while the Class attributes can be the main Library class as a property to be quoted: package { import flash.display.Sprite; public class Library extends Sprite { public var ci

  • [Change] Linux dynamic library search path 2010-07-21

    As we all know, Linux default dynamic library search path is / lib and / usr / lib. Dynamic library is created, generally copied to the two directories. When the program execution when a dynamic library, and the dynamic library has not yet loaded int

  • Using JNative achieve dynamic library called Java 2010-10-18

    As the project requirements, the need to use Java call windows of the dll files, check a little, so if JNI is a pain in the sourceforge.net search a bit "Java dll", first appeared is Jnative, decided to use it, was also tried something else, but

  • Windows and Linux systems in the dynamic library 2010-11-08

    (Rpm) Of: Liu Shidong Yang Lin 1 Introduction Dynamic libraries (Dynamic Link Library abbr, DLL) technique is often used in programming technology. The aim to reduce the size of the program, save space, improve efficiency, with high flexibility. Dyna

  • dynamic library file under linux so some understanding of 2010-12-17

    Personal creation, welcome to the wrong. Involves the ELF format, gcc compiler option to be completed, explain simple and practical, so files on Linux there is under the practical understanding. 1.so file? 2. How to generate and use a dynamic library

  • C / C + + compiler and debugger, and a static library. Dynamic library used summary 2011-05-04

    Often, a friend asked about unix is the technology section below. I compiled the following most commonly used unix system debugging tools to debug technology articles. We want to help. In addition static library, dynamic library is also relatively hi

  • jni call under linux can not find the dynamic library error occurs 2011-05-05

    Exception information: Exception in thread "main" java.lang.UnsatisfiedLinkError: no snmpNetVista in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1682) at java.lang.Runtime.loadLibrary0(Runtime.java:822) at java.lang.S

  • so file under linux dynamic library some knowledge 2010-12-17

    Personal creation, welcome means wrong. Involves the ELF format, gcc compiler options to be completed, simple and practical description of what, for so files under Linux have a practical understanding. 1.so files? (2) how to generate and use a dynami

  • Linux sketchy related to the lower reservoir and the dynamic library search path priority 2010-03-13

    (This article is aimed at dynamic link library, the content source network, re-order of, highlighted some key concerns, please) 1. <br /> Basic knowledge of the whole, the library can be used in three forms: static, shared and dynamic. Static librar

  • Easy language core support library DLL command Daquan 2010-04-21

    Easy language core support library DLL command Daquan . Version 2 . DLL command _ to take a short integer pointer, an integer type,, "lstrcpyn", public . Parameter objective indicators, short integer, transfer site . Parameter source pointer, a

  • [Change] Linux static library and dynamic library search path set 2010-07-21

    1. Connection and run-time library search path to set Library file in the connection (static libraries and shared libraries) and run (only use the shared library procedure) was used, the search path is set in the system's. General Linux system to / l

  • linux load dynamic library error "only ET_DYN and ET_EXEC can be loaded" solution 2010-08-05

    linux load dynamic library error "only ET_DYN and ET_EXEC can be loaded" The solution is to package the process g + +-fpic -I/usr/java/jdk1.6.0_19/include / -I/usr/java/jdk1.6.0_19/include/linux /-shared-o libkeyboard_encrypt_64.so-c keyboard_en

  • Unix / Linux environment to create and use static / dynamic library 2010-09-27

    The role of libraries The presence of large libraries, there are two reasons, first, the code reuse, and second, separation of declaration and implementation. Function similar to the use of the module into the library package, to make code reuse, man

  • About Linux static library and dynamic library of 2010-09-28

    1. What is a library in the windows platform and <br /> linux platforms there are a large number of libraries. Library is essentially a binary form of executable code that can be loaded into memory the operating system implementation. Because the na

  • java reflection and dynamic proxy 2010-12-26

    java reflection and dynamic proxy 1 java reflection mechanism: the run-time class information Java.lang.reflect Class class and class library with support for the concept of reflection, the library contains a Field, Method, and Constructor classes. T

  • 再次遇到的问题:Unable to load dynamic library myext.so: undefined symbol 2014-10-31

    google到了这里 http://stackoverflow.com/questions/7271792/php-warning-php-startup-unable-to-load-dynamic-library-home-lib-so-so-undef 终于解决了!!! 问题是:能编译模块生成so,但php不能加载 PHP Startup: Unable to load dynamic library myext.so: undefined symbol: Z_ADDREF_P(这里是你的

  • Java development framework: The framework of a simple summary of SSH 2009-10-09

    Java development framework: The framework of a simple summary of SSH Release Time: 2008.05.08 09:10 Source: Microsoft Author: Seville NEW YORK finishing technology community】 Struts, spring, Hibernate role at all levels 1) struts is responsible for w

  • [Change] the use of JAVA in the dynamic proxy to achieve database connection pooling 2010-03-22

    Database connection pool in the preparation of applications often need to use the module, too frequent in terms of connecting to the database on the service performance is a bottleneck, the use of buffer pool technology to eliminate this bottleneck.

  • Linux to create a static library and dynamic library 2010-03-18

    Step 1: Edit the procedures by example - hello. H, hello.c, and main.c; hello.h the library's header files. hello.c is the library's source code, which contains the public function hello, this function will be output on the screen "Hello XXX!".