用java的JNI可以调用外部程序

解决方案 »

  1.   

    不过还要定制一个DLL供JAVA调用。如果你要详细的步骤我可以给你提供一点方法。是用VC编制DLL的
      

  2.   

    谢谢,请给我一个详细步骤。如果有VB的更好,因为VC我并不很熟悉。
      

  3.   

    Step 1: Write the Java Code
    The following Javacode segment defines a class named HelloWorld . This class declares one native method, implements a main method, and has a static code segment.
    class HelloWorld {
        public native void displayHelloWorld();
        static {
            System.loadLibrary("hello");
        }
        
        public static void main(String[] args) {
            new HelloWorld().displayHelloWorld();
        }
    }
    Declare a Native Method
    You must declare all methods, whether Java methods or native methods, within a class on the Java side. When you write a method implementation in a language other than Java, you must include the keyword native as part of the method's definition within the Java class. The native keyword signals to the Java compiler that the function is a native language function. It is easy to tell that the implementation for the HelloWorld class's displayHelloWorld method is written in another programming language because the native keyword appears as part of its method definition:public native void displayHelloWorld();
    This native method declaration in your Java class provides only the method signature for displayHelloWorld. It provides no implementation for the method. You must provide the implementation for displayHelloWorld in a separate native language source file.Step 2: Compile the Java Code
    Use the Java compiler to compile the class that you created in the previous step. Here's the command to use:
    javac HelloWorld.javaStep 3: Create the .h File
    In this step, you use the javah utility program to generate a header file (a .h file) from the HelloWorld class. The header file provides a C function signature for the implementation of the native method displayHelloWorld defined in that class.Run javah now on the HelloWorld class that you created in the previous steps.The name of the header file is the Java class name with a .h appended to the end. For example, the command shown above will generate a file named HelloWorld.h.By default, javah places the new .h file in the same directory as the .class file. Use the -d option to instruct javah to place the header files in a different directory.
    The Function DefinitionLook at the header file HelloWorld.h.
    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class HelloWorld */
    #ifndef _Included_HelloWorld
    #define _Included_HelloWorld
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
     * Class:     HelloWorld
     * Method:    displayHelloWorld
     * Signature: ()V
     */
    JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld
      (JNIEnv *, jobject);
    #ifdef __cplusplus
    }
    #endif
    #endif
    The Java_HelloWorld_displayHelloWorld function provides the implementation for the HelloWorld class's native method displayHelloWorld, which you will write in Step 4: Write the Native Method Implementation. You use the same function signature when you write the implementation for the native method.If HelloWorld contained any other native methods, their function signatures would appear here as well.Step 4: Write the Native Method Implementation
    Now, you can finally write the implementation for the native method in a language other than Java.
    Note:  Back to that "real world," C and C++ programmers may already have existing implementations of native methods. For those "real" methods, you need only ensure that the native method signature matches the signature generated on the Java side.
    The function that you write must have the same function signature as the one generated by javah
     in the HelloWorld.h file in Step 3: Create the .h File. Recall that the function signature generated for the HelloWorld class's displayHelloWorld native method looks like this:
    JNIEXPORT void JNICALL
        Java_HelloWorld_displayHelloWorld(JNIEnv *, jobject);
    Here's the C language implementation for the native method Java_HelloWorld_displayHelloWorld. This implementation is in the file named HelloWorldImp.c.
    #include <jni.h>
    #include "HelloWorld.h"
    #include <stdio.h>
    JNIEXPORT void JNICALL 
    Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj) 
    {
        printf("Hello world!\n");
        return;
    }
    The implementation for Java_HelloWorld_displayHelloWorld is straightforward. The function uses the printf function to display the string "Hello World!" and then returns.The HelloWorldImp.c file includes three header files:
    1. jni.h - This header file provides information that the native language code requires to interact with the Java runtime system. When writing native methods, you must always include this file in your native language source files.
    2. HelloWorld.h - The .h file that you generated in Step 3: Create the .h File.
    3. stdio.h - The code snippet above includes stdio.h because it uses the printf function. The printf function is part of the stdio.h library.Step 5: Create a Shared Library
    Remember in Step 1: Write the Java Code you used the following method call to load a shared library named hello into your program at runtime:
    System.loadLibrary("hello");
    Now you are ready to create this shared library.In the previous step, Step 4: Write the Native Method Implementation, you created a C file in which you wrote the implementation for the displayHelloWorld native method. You saved the native method in the file HelloWorldImp.c. Now, you must compile HelloWorldImp.c into a shared library, which you name hello to match the library name used in the System.loadLibrary method.Compile the native language code that you created in the previous two steps into a shared library. On Solaris, you'll create a shared library, while on Windows 95/NT you'll create a dynamic link library (DLL). Remember to specify the path or paths to all necessary header files. See Creating and Loading Shared Libraries for information on forming a shared library name.On Solaris, the following command builds a shared library libhello.so:
    cc -G -I/usr/local/java/include -I/usr/local/java/include/solaris \
          HelloWorldImp.c -o libhello.so
    On Win32, the following command builds a dynamic link library hello.dll using Microsoft Visual C++ 4.0:
    cl -Ic:\java\include -Ic:\java\include\win32
          -LD HelloWorldImp.c -Fehello.dll
    Of course, you need to specify the include path that corresponds to the setup on your own machine.Step 6: Run the Program
    Now run the Java application (the HelloWorld class) with the Java interpreter, as follows:
    java HelloWorld
    You should see the following output:
    Hello World!
    If you see an exception like the following, then you don't have your library path set up correctly.
    java.lang.UnsatisfiedLinkError: no hello in shared library path
            at java.lang.Runtime.loadLibrary(Runtime.java)
            at java.lang.System.loadLibrary(System.java)
            at 
            at java.lang.Thread.init(Thread.java)
    The library path is a list of directories that the Java runtime system searches when loading libraries. Set your library path now, and make sure that the name of the directory where the hello library lives is in it.