控制台彩色输出:
import com.sun.jna.Native;
import com.sun.jna.win32.StdCallLibrary;
public class OutputTest {
interface api extends StdCallLibrary {
api INSTANCE = (api) Native.loadLibrary("kernel32", api.class);
int GetStdHandle(int stdHand);
boolean SetConsoleTextAttribute(int hConsoleOutput, int textAtt);
}
public static void out(String str, int color) {
int ptr = api.INSTANCE.GetStdHandle(-11);
api.INSTANCE.SetConsoleTextAttribute(ptr, color);
System.out.println(str);
}
public static void main(String[] args) {
out("hello ", 5);
out("world", 6);
}
}
jna下载: http://download.csdn.net/detail/alancaiyl/4160319
转载自:http://tieba.baidu.com/p/1694882024
必须在Windows Dos控制台上使用才有用,IDE里面不行。
在非IDE环境下引用jar包,以使用jna为例,如下:
首先是编译,OutputTest.java在d:\java下
D:\java>javac -cp d:\java\jna.jar;d:\java\platform.jar OutputTest.java
D:\java>java -classpath d:\java\jna.jar;d:\java\platform.jar;d:\java OutputTest
![](https://img-my.csdn.net/uploads/201302/27/1361954517_3310.jpg)