preface
This article will explain how to use the CLS command line clear function in Java by using JNA code, code has been synchronized to GitHub, in addition to this article is not specifically to explain the use of JNA, if you need to learn to check the official website of GitHub example. I’ll start by writing sample code to show how to use JNA to call a local DLL to clear the screen (which might be used when writing a command line snake game).
writedll
file
Since this article is not a tutorial on how to write DLLS, and I’m not a professional programmer of the C family language, I’ll start by showing you directly in code (the DLL created in this article can be found on GitHub) :
First open VS2019, then select Create a new project, search for and select a (DLL) dynamically linked library with exported items, then click Next:
Then configure your own project directory and project name, click Create:
Looking at the project structure, we can see that we have already created some sample code for us by default, so we won’t change it. Then we will start adding our own CLS functionality. For later demonstration purposes, we will add another one that prints Hello, world! The function.
First go to the clsscreen.cpp file (which will be named differently depending on the project name in your previous step) :
We added the following code:
#include <stdlib.h>
#include <iostream>
using namespace std;
CLSSCREEN_API void cls(a) {
system("cls");
}
CLSSCREEN_API void sayHello(a) {
cout << "Hello, world!";
}
Copy the code
Then open the clsscreen.h file and change it:
We added the following code:
extern "C" CLSSCREEN_API void cls(a);
extern "C" CLSSCREEN_API void sayHello(a);
Copy the code
Then we can generate DLL files!
First, modify it as shown below:
Then click Build to select build solution.
Then we can find the DLL we generated under the x64 -> Release folder in our project path:
The introduction ofJNA
In order to reduce the learning burden, we will just create regular Java projects and not create Maven projects.
First we need to import two JAR packages and add our DLL files to the project:
Then write our interactive interface code:
public interface ClsScreen extends Library {
ClsScreen DLL = Native.load(System.getProperty("user.dir") + "/clsscreen.dll", ClsScreen.class);
void cls(a);
void sayHello(a);
}
Copy the code
Then we can start using our CLS and sayHello functions!
First test the sayHello function:
Successful, then we will test our main character CLS function. First we will write our test code:
public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("Please enter your name:");
System.out.println("Welcome," + sc.next());
System.out.println("Clear the screen? (y/n)");
if (Objects.equals(sc.next(), "y")) {
ClsScreen.DLL.cls();
}
System.out.println("Quit or not? (y/n)");
if (Objects.equals(sc.next(), "y")) break; }}}Copy the code
CLS in the Terminal window of IDEA does not clear the screen, but is a garbled code:
When we type y and press Enter, the screen is cleared:
And you’re done!
Tips
In fact, we can Jar this feature so that we can use it freely in the future. I will not introduce the specific steps here, if you have any doubts, welcome to exchange and discuss.
conclusion
Hopefully, this article has provided you with a brief introduction to the use of JNA and how to use THE CLS screen clearing function in Java.