Java and other languages ​​(Java, C and Jython)

zhaozj2021-02-16  166

Although Java's program must be executed on the Java virtual machine, we can play a hole in this machine, allowing the Java program to directly communicate actual machine.

JNI is a mechanism that enables Java's program to external external programs, so that the Java program is more expandable, and the following will demonstrate a simple example. In this example, how Java is in line with C language, in fact, ProLog language can also take advantage of this Make the Java program to achieve this task using ProLog, through the DLL and NATIVE. After such tests and experiments, I have to admire the Java's program language.

You don't need to worry about the program, because the programming architecture under the bottom is quite simple, mainly just let us experience.

Tool introduction: I will use Visual C 6.0 to make DLL, use Java SDK 1.4 to compile my Java

ja

VA part of the actual

The program code is as follows (HelloWorld.java):

Class HelloWorld {Public Native Void DisplayHelloWorld (); / * The Native Site above the upper number is to notify the Java using the external function * / static {system.loadLibrary ("Hello"); / * Above the program code representative I want to load Into the dynamic link of Hello.dll, Hello.dll's production I will introduce later * /} public static void main (String [] args) {new helloworld (). DisplayHelloWorld ();}}

When you complete the above-mentioned program, use the 'javac helloworld.java' to use 'javac helloworld.java', then use 'javah helloworld' to generate helloworld.h, in HelloWorld In. H we know that we have to do a function java_helloworld_displayhelloworld (jnienv *, jobject); next I will show how to complete this function.

The first DLL of the actual life, before I know that there is a mechanism called DLL, dynamic linking under Windows and Linux, but I have never been understood how it is doing, I will have the first one like this. I will Try to describe something clear because this is my first experience.

First start Visual C 6.0, select Add, then select the Win32 Dynamic-Link Library this option in Project, then fill in the name of your item 'Hello', press 'OK', select an Empty DLL Project this Option, then press 'Finish' to press 'OK', we have to start with the head.

After the above action, we will get an empty project information, then we need to add some programs to our project, before this, we will handle our .H file, first come to the directory of the Java JDK to find it. Include's data clip, in order to make a composing program, copy all files in this directory to you install Visual C 6.0 directory in VC98 / Include, the preset path is C: / Program Files / Microsoft Visual Studio / VC98 / Include, copy the information into this folder and copy Jawt_MD.H and JNI_MD.H originally in Win32 to C: / Program Files / Microsoft Visual Studio / VC98 / include completed these pre-action, Let's add HelloWorld.h we have just generated to our header file, and add a .C file helloworldimp.c, then go to the function table above the build Hello.dll ', then you will Get one Hello.dll /debug/hello.dll under the project directory, so so, if you should get a DLL file.

The program code 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 code 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, in the same directory, execute the Java HelloWorld, and the results of the print helloworld on the screen appear.

The above is a small example, the purpose is just a small experiment, because no one will help you do things because there is a person who wants to print a string of self-rescue to call the C language, and will use system.out.print directly. HelloWorld "), a little faster.

Through the above small experiments, we know that Java and C have a way interoperable, so maybe Java can be more closely interacted with the operating system through this manner.

Part2

After the previous exercise, we have used Java's JNI to combine the C packet in Java, using the DLL written by C language to combine the Java program with C, we will write a Java program, not to pay for the DLL file. Things, we only need to know how to use the DLL file to write well.

But this is not my ultimate goal, I hope I can use the Python program to make a package in the Java program, so use JPython, JPython is a Python language in Java, this language is the same as Python language. Simple and convenient features, only it is on the Java virtual machine, so many functions about the job system can use JPython's syntax is Python language. People who are familiar with Python should be easy, and more special It is possible to use the * .class import from Java to do it in your own library, which can not only use the library that originally exists in Python, but also use Java's library. Through Java, we can communicate with the C language, we can also communicate with the proLog language, which seems to have reached the point where language barrier is reached, and the geophilic village of the program seems to be realized.

It used to be packaged in the same language, using the object-oriented programming of C, Java, package into a lot of categories, now I find one thing, we seem to be packaged in the language, the function is not necessarily the same A language goes to do, because everyone's background is different, the language is familiar with the language, forced each member to use the same language in the project, but it is more easy to integrate, but the development speed is not necessarily fast, only in everyone is using it. The language habits will develop to achieve the effect of halving.

(Java's JNI and C-Riovisions I have made detailed introductions in the previous article, here you don't repeat)

Introduction to Python language

http://www.python.org/

Introduction to Ython language

http://www.jython.org/

I simply explain my approach:

DLL files have been compiled with Hello.dll before use, and the category of the category HelloWorld.class (before compiling)

Install Jython is currently version 2.1

After you finish, you can play it first (assuming you are under C: / Jython21)

C: / jython21> jython

It will be initiated first, and set the Java environment, then appear >>> The prompt symbol represents you can start actions.

You can try some of Python original features, examples:

1. You are tiry a math operation 2 3 answers at prompt information (>>>) is 5

>>>

2 3

5

>>>

2. If you type a negative calculation 2 * (2J 3), the answer is (6 4J)

>>>

2 * (2J 3)

(6 4J)

>>>

If you want to print Hello, type Print "Hello" directly

>>>

Print "Hello"

Hello

>>>

Environment setting

Create a directory in c: / jython21, then copy hello.dll and helloworld.class under this directory, then set your environment variable, Path and ClassPath increase C: / Jython 21 / Javalib

Test execution

>>>

Import HelloWorld ← Left This sentence is to introduce the HelloWorld.class category

After success, you should appear >>> prompt message, wait for you to enter the next instruction, if you are not successful, please check if helloWorld.java content is correct, remember this category must be a public category public class helloworld {...}, correction Regenerate HelloWorld.class

>>>

Test = HelloWorld () ← This sentence is equivalent to test = new helloworld (); then you should appear >>> prompt message

>>>

Test.displayHelloWorld ()

>>> HelloWorld

If the HelloWorld representative appears successfully

According to experts, it is pointed out that you can develop your project with Java to develop your project, because Jython's syntax is simple, with a short description, you can achieve a lot of work, and finally it seems to combine it itself in Jython. Java's program group is translated into new .CLASS files to provide Java VM, which is also available. Jar files allow you to enjoy Jython directly in Java.

转载请注明原文地址:https://www.9cbs.com/read-8666.html

New Post(0)