Java--jni (Example Console (64-bit) Clear screen

Source: Internet
Author: User

Because Java is the lowest level of the JVM, so simple console Java program can not perceive the JVM again under the circumstances of the operating system,

The DLL (Dynamic Linkable Library, dynamically-linked library) file for Java backend calls can be implemented via JNI (Java Native Interface) technology.

However, calls outside of Java are often not ported to other platforms, and security exceptions can be thrown in applets. Implementing native code will make your Java application fail to pass 100% pure Java testing. However, there are several guidelines to consider if you must perform a local call:

1. Encapsulate all your local methods into a class that calls a single DLL. For each target operating system platform, you can use a DLL that is specific to the appropriate platform version. This minimizes the impact of native code and helps to take into account the migration issues that are needed later.

2. The local method is as simple as possible. Try to minimize the reliance of your local methods on third-party (including Microsoft) run-time DLLs. Keep your local methods as independent as possible to minimize the overhead required to load your DLLs and applications. If a runtime DLL is required, it must be supplied with the application.

Write an example of a Java program calling C + + clear screen function

1, write Java class with native

 Public class Cls {    //  Declaration Local method public    nativevoid  Clear ();     Static {        // Load dynamic-link library        DLL// to ensure that the loaded ClsCpp.dll is in your installed jdk\jre\ Bin under directory        system.loadlibrary ("Clscpp");}    }

2, in the CMD environment, using the Javah command, the Cls.class compiled into Cls.h file

The resulting Cls.h file is as follows:/*Do not EDIT this file-it are machine generated*/#include"Jni.h"//Note the original command here # # # <jni.h> changed to # include "Jni.h"/*Header for Class Cls*/#ifndef _included_cls#define _included_cls#ifdef __cplusplusexternC{#endif/** CLASS:CLS * method:clear * Signature: () V*/Jniexportvoidjnicall java_cls_clear (jnienv*, jobject); #ifdef __cplusplus} #endif #endif

3 , creating DLL Library Item Type


(1) in the VS2010 environment, create a C + + Win32 project called Clscpp. Selecting DLLs at the application type of the wizard

Copy the jni_md.h from the files jni.h and Win32 folders in the Include folder of your machine's JDK directory to the root of the Clscpp project and add them to the project.

Here my copy has a problem, cause I can't find the file, I was newly created the file and then the code copied the past

New source file Cls.cpp

" jni.h "  "Cls.h"<stdio.h><stdlib.h>//not understand the meaning of this code for the time being  void jnicall java_cls_clear (jnienv *, Jobject) {       system ("Cls ");  // In C + +, you can invoke the clear screen command in the operating system directly }

Finally, he will generate DLL files in debug

But that's not what we want, because our machines are almost 64.

We want to modify the properties of the Cls.cpp file

First right-click Properties

Change it to 64 bits, and in the solution folder you'll see our newly created X64 folder, and we'll be able to find 64-bit DLLs along x64\debug\.

(4) Get ClsCpp.dll and copy it to the Jdk\jre\bin folder installed on your machine.

(5) as soon as you add the Cls.java class in 1, you can use the local method clear () in the CLS class to clear the screen ( this clear-screen function can only be performed in CMD

     Public Static void Main (String args[]) {        finalnew  Cls ();        System.out.println ("Hello,world");         New Scanner (system.in);         = sc.nextline ();        Clearscreen.clear ();    }

Reference: documentation for http://www.cnblogs.com/Alandre/p/4456719.html and Lin Qingxian teachers

Java--jni (Example Console (64-bit) Clear screen

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.