小伙伴们在初学Java的时候一般都是采用Eclipse或其他IDE环境,中英文混合时的对齐问题想必都或多或少地困扰过大家。
比如下面的代码和在Eclipse中的显示效果:
Java字符串格式构建代码:
public String toString() {
String str = String.format("%-8s%-4d\t%-8s\t%.2f", name, level, getLevelName(), face);
return str;
}
跟我们设想的并不一样。网上有个比较简单的解决方案,就是在%s后添加\t:
public String toString() {
String str = String.format("%-8s\t%-4d\t%-8s\t%.2f", name, level, getLevelName(), face);
return str;
}
效果如下:
对于没有强迫症的小伙伴,本文结束,大家按照上面的解决方案修改代码即可。
JNI,即Java Native Interface,Java本地接口。是Java平台提供的调用本地C/C++代码进行互操作的API。
/**
* 后宫佳丽
* @author 窖头
*
*/
public class Beauty {
private static String[] levelNames = {"秀女", "答应", "常在", "贵人", "嫔", "妃", "贵妃", "皇贵妃", "皇后", "皇太后", "太皇太后", "太皇太后还要往后"};
private String name;
private int level;
private String levelName;
private double face; //颜值,可以通过图像AI获取
public Beauty(String name, int level, double face) {
this.name = name;
setLevel(level);
this.levelName = getLevelName();
this.face = face;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLevel() {
return level;
}
public void setLevel(int level) {
if(level < 0 || level > levelNames.length) {
this.level = 1;
return;
}
this.level = level;
}
public String getLevelName() {
if(level < 0 || level > levelNames.length) {
return levelNames[0];
}
return levelNames[level - 1];
}
public void setLevelName(String levelName) {
this.levelName = levelName;
}
public double getFace() {
return face;
}
public void setFace(double face) {
this.face = face;
}
}
/**
* 使用单例模式的打印类
* @author 窖头
*
*/
public class Printer {
private static Printer printer = null;
private Printer() {}
/**
* 调用native方法打印后宫佳丽的信息
* @param beauty
*/
public native void printf(Beauty beauty);
public static Printer getInstance() {
if(null == printer) {
printer = new Printer();
}
return printer;
}
}
下图是在Eclipse中创建的工程和class:
win+r -> cmd,进入工程根目录的bin目录,输入以下指令:
//包名及类名请根据自己的定义进行修改
javah -jni com.xuetang9.kenny.util.Printer
这里如果出现错误,请检查并重新配置Java的环境变量
获得头文件:com_xuetang9_kenny_util_Printer.h
头文件以包名_方法名的方式命名,内容如下:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_xuetang9_kenny_util_Printer */
#ifndef _Included_com_xuetang9_kenny_util_Printer
#define _Included_com_xuetang9_kenny_util_Printer
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_xuetang9_kenny_util_Printer
* Method: printf
* Signature: (Lcom/xuetang9/kenny/entity/Beauty;)V
*/
JNIEXPORT void JNICALL Java_com_xuetang9_kenny_util_Printer_printf
(JNIEnv *, jobject, jobject);
/** 自定义函数:将Java传来的字符串转换为GB2312以便显示 */
char* jstringToWindows(JNIEnv *, jstring);
/** 自定义函数:将gb2312转换为UTF8/16,以便传回给Java能够正常显示 */
jstring WindowsTojstring(JNIEnv* env, const char * );
//关于为什么使用两个自定义转换函数请参见:http://wiki.xuetang9.com/?p=5270
#ifdef __cplusplus
}
#endif
#endif
在头文件旁创建C++源文件:com_xuetang9_kenny_util_Printer.cpp
文件名不变,后缀名修改为cpp,实现代码如下:
#include "com_xuetang9_kenny_util_Printer.h"
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <Windows.h>
using namespace std;
JNIEXPORT void JNICALL Java_com_xuetang9_kenny_util_Printer_printf(JNIEnv * env, jobject jobj, jobject jbeauty)
{
jclass beautyClass = env->GetObjectClass(jbeauty); //获得Java传来的后宫佳丽对象
//获得属性句柄(ID)
jfieldID nameFid = env->GetFieldID(beautyClass, "name", "Ljava/lang/String;");
jfieldID levelFid = env->GetFieldID(beautyClass, "level", "I"); //整型为I,double类型为D
jfieldID levelNameFid = env->GetFieldID(beautyClass, "levelName", "Ljava/lang/String;");
jfieldID faceFid = env->GetFieldID(beautyClass, "face", "D");
//获得name属性的值
jstring jNameField = (jstring)env->GetObjectField(jbeauty, nameFid);
jint jLevelField = (jint)env->GetIntField(jbeauty, levelFid);
jstring jLevelNameField = (jstring)env->GetObjectField(jbeauty, levelNameFid);
jdouble jFaceField = env->GetDoubleField(jbeauty, faceFid);
//const char * cNameField = env->GetStringUTFChars(jNameField, NULL);
//const char * cLevelNameField = env->GetStringUTFChars(jLevelNameField, NULL);
//C++中的打印格式控制,左对齐,单独设置每个元素的宽度
cout.setf(ios::left);
cout << setw(12) << jstringToWindows(env, jNameField);
cout << setw(4) << jLevelField;
cout << setw(8) << jstringToWindows(env, jLevelNameField);
cout << setw(7) << jFaceField << endl;
//释放字符串所占的空间
//env->ReleaseStringUTFChars(jNameField, NULL);
//env->ReleaseStringUTFChars(jLevelNameField, cLevelNameField);
}
//字符串转换函数,了解做什么的即可
/**
* 将Java传来的UTF8/16编码转换为C/C++能够正常显示的GB2312编码
*/
char* jstringToWindows( JNIEnv *env, jstring jstr )
{
int length = (env)->GetStringLength(jstr);
const jchar* jcstr = (env)->GetStringChars(jstr, 0);
char* rtn = (char*)malloc(length*2 + 1);
int size = 0;
size = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)jcstr, length, rtn,(length*2+1), NULL, NULL);
if( size <= 0 )
return NULL;
(env)->ReleaseStringChars(jstr, jcstr);
rtn[size] = 0;
return rtn;
}
/**
* 将C/C++中的GB2312编码转换成UTF8/16编码
*/
jstring WindowsTojstring( JNIEnv* env, const char* str )
{
jstring rtn = 0;
int slen = strlen(str);
unsigned short * buffer = 0;
if( slen == 0 )
{
rtn = (env)->NewStringUTF(str );
}
else
{
int length = MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, NULL, 0 );
buffer = (unsigned short *)malloc( length*2 + 1 );
if( MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, (LPWSTR)buffer, length ) >0 )
rtn = (env)->NewString( (jchar*)buffer, length );
}
if(buffer) free( buffer );
return rtn;
}
配置好MinGw的环境变量后,键入下面的命令:
g++ -m64 -static-libgcc -static-libstdc++ -I"C:\Program Files\Java\jdk1.8.0_201\include" -I"C:\Program Files\Java\jdk1.8.0_201\include\win32" -shared -o Printer.dll com_xuetang9_kenny_util_Printer.cpp
1、路径C:\Program Files\Java\jdk1.8.0_201\include和 C:\Program Files\Java\jdk1.8.0_201\include\win32 分别包含了JNI的头文件,<jni.h>和<jni_md.h>,请大家根据自己机器配置的不同,自行修改路径
2、-m64表示生成64位dll库文件
书写Java测试类:
import java.io.File;
import com.xuetang9.kenny.entity.Beauty;
import com.xuetang9.kenny.util.Printer;
public class TestPrinter {
public static void main(String[] args) {
//请大家自行修改成自己机器的路径
String path = "C:\\Users\\窖头\\eclipse-workspace\\PrintMsgByCpp\\bin\\Printer.dll";
File file = new File(path);
//加载本地dll库
System.load(file.getAbsolutePath());
Beauty[] beauties = new Beauty[5];
for(int i = 0; i < beauties.length; i++) {
beauties[i] = new Beauty();
}
beauties[0] = new Beauty("貂蝉1号", 5, 86.25);
beauties[1] = new Beauty("a赵飞燕b", 6, 76.25);
beauties[2] = new Beauty("ab西施bc", 7, 56.25);
beauties[3] = new Beauty("北岸初晴", 8, 66.25);
beauties[4] = new Beauty("龙a女d", 9, 96.25);
for(int i = 0; i < beauties.length; i++) {
//调用本地C++方法打印对象的内容
Printer.getInstance().printf(beauties[i]);
}
}
}
如果直接在Eclipse中运行这个main方法,会抛出异常:java.lang.UnsatisfiedLinkError: %1 不是有效的 Win32 应用程序
反正未来我们开发完成的程序也不可能在Eclipse中执行,所以我们直接在控制台下执行并观察
结果:
java com.xuetang9.kenny.TestPrinter
显示效果非常完美,大功告成!