Although Java programs must be written on the Java receiver, we can make a hole in this host, allow Java programs to directly access the real machine.
JNI is a mechanism that allows Java programs to connect external programs, making Java programs more dynamic. The following shows a simple example, this example shows how Java programs can communicate with C Programs. In fact, PROLOG statements can also be used to enable Java programs to use Prolog, through the DLL and native methods, I have to admire the programming language of Java.
You don't need to worry about programming, because the underlying architecture is simple, just let us understand it.
Tool Introduction: I will use VISUAL C ++ 6.0 for DLL and Java SDK 1.4 to compile my java
JaVa practices
The program example is as follows (helloworld. Java ):
Class helloworld {
Public native void displayhelloworld ();
/* The native type above indicates that the Java function will use the external function */
Static {
System. loadlibrary ("hello ");
/* The Program program above indicates that I want to import the dynamic links of Hello. dll.
Will be introduced later */
}
Public static void main (string [] ARGs ){
New helloworld (). displayhelloworld ();
}
}
After you complete the above program, save the program as 'helloworld. java 'first uses 'javac helloworld. java 'This Command organizes it into helloworld. class, and then use 'javah helloworld' to generate helloworld. h, in helloworld. in H, we know that we want to implement a function java_helloworld_displayhelloworld (jnienv *, jobject). Next we will introduce how to complete this function.
As the first DLL in my life, I know that there is a kind of mechanism called DLL and dynamic link in Windows and Linux, but I have never been able to figure out how it actually works. Today I will be the first such thing. I will explain it clearly, this is my first experience.
First, activate visual c ++ 6.0, add a new option in the case, and then select Win32 dynamic-Link Library in the project, then fill in "hello" in your case and press "OK". Please select an empty DLL project, then press 'finish 'and then' OK '. We should start from the beginning.
After the above actions, we will get an empty response, and then we need to add some programs to our response, before that, we should handle our problems. h. First, find the include resource under the project of your Java JDK. It is convenient to compile the program later, copy all the cases under this project to vc98/include under your security visual c ++ 6.0 Project, and set the path to C: /program files/Microsoft Visual Studio/vc98/include, submit the information to this resource, and copy the original jawt_md.h and jni_md.h files in Win32 to C: /program files/Microsoft Visual Studio/vc98/include
After completing these preparations, we will introduce our new helloworld. H is added to our header files and a new one is added. c's commit case helloworldimp. c. Then, select 'build hello. dll ', then you will get a hello. /debug/hello. DLL, so far, if you are profitable, you should get the DLL handler.
The program example is as follows (helloworldimp. C ):
#include
#include "HelloWorld.h"
#include
JNIEXPORT void JNICALL
Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj)
{
printf("HelloWorld");
return;
}
The program example is as follows (helloworld. h)
/* DO NOT EDIT THIS FILE - it is machine generated */ #include
/* 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
Then, place hello. dll and helloworld. class in the same project, compile the Java helloworld line, and display the result of printing helloworld on the graphic surface.
The above is a small example, the purpose is to make a small reality, because no one will directly use system to print a series of self-help calls to the C statement to help you do things. out. print ("helloworld"), a little faster than commit.
Through the above, we can see that Java and C can communicate with each other using the programming method, therefore, maybe Java can interact more closely with the production system through this method.
Part2
We have used Java's JNI to Package C in Java, and used the DLL compiled by C statement to link Java programs with C, as long as we create a program in Java, we don't have to worry about what the DLL program has done. As long as we know how to call a DLL program, we can make the program better.
However, this is not my most objective. I hope that I can use the python program to compile a Java program, so I used the jpython statement, jpython is a python statement made in Java. This statement guarantees the same simplicity and convenience as the python statement, however, it is deployed on Java's memory machine. Therefore, many functions related to the convenience of processing cannot be used.
The compile method of jpython is a python statement. It is easier for people familiar with python to get started. What is special about jpython is that it can compile Java *. class import is used in your own handler. It can not only use the handler that originally existed in Python, but also use the Java handler, through Java, we can talk to C again, and we can talk to Prolog again. It seems that we have reached the point where we can say no, the global village of the program seems to be realized.
In the past, we used to package packages in the same statement. In C and Java, we used Program Design for object tracing, which included a lot of differences, now, I have found that we can package the statement. The function is not necessarily implemented in the same statement, because of the different backgrounds of each person, they are familiar with different languages, in case development, every employee is forced to use the same statement, which may be easier to integrate, but the development speed is not necessarily fast, only when everyone uses the provided statement for development can they get twice the result with half the effort.
(Java's JNI and C connection methods have been introduced in my previous article, so I will not repeat it here)
Introduction to Python statement http://www.python.org/
Introduction of ython statement http://www.jython.org/
I 'd like to explain my practices in a simple way:
DLL uses the hello. dll that has been written before, and the classes used are different from those used by helloworld. Class (previously compiled successfully)
Currently, Alibaba Cloud Security Jython is version 2.1.
You can play with it after you finish the preparation. (assume that you are playing C:/jython21)
C:/jython21> Jython
It will first initialize and set the Java environment, and then the >>> prompt means you can start
You can first compile some functions of Python, for example:
1. When you receive the message "interest (>>>)", you can calculate 2 + 3 of data. The answer is 5.
>>> 2 + 3
5
>>>
2. If you calculate 2 * (2j + 3) in analyticdb, the answer is (6 + 4j)
>>> 2 * (2j + 3)
(6 + 4j)
>>>
If you want to print "hello", print "hello" directly"
>>> Print "hello"
Hello
>>>
Environment Settings
Create a project named javalib under C:/jython21, and then set "hello. DLL and helloworld. when the class variable reaches this goal, then set your environment change. The path and classpath are both added to C:/jython21/javalib.
Too many rows
>>> Import helloworld into the left segment is to introduce the helloworld. Class class for usage.
After the operation is successful, it should appear >>> a message is displayed, waiting for you to write the next command. If the operation fails, please check helloworld. whether Java content is correct. Remember that this class must be a public class other public class helloworld {...}, after the modification, helloworld will be created again. class
>>> Test = helloworld () uses the statement left when test = new helloworld ();
Next it should appear> prompt message
>>> Test. displayhelloworld ()
>>> Helloworld
If helloworld exists, it indicates that the operation is successful.
The root author pointed out that using Java with Jython to develop your case can greatly improve the development speed, because the logic of Jython is simple, in a short description, it can be converted into a lot of work. In the end, it seems that Jython can combine Java program components into new ones. the class program provides Java Vm, which also provides. jar packages allow you to directly use Jython in Java.