自己写的jar,在idea或者其他ide上面运行正常,打成可执行jar包在windows下运行时报错。
错误: 编码GBK的不可映射字符
问题原因是编译时没有选择正确的编译参数,
javac有几个参数 -encoding表示了编译的编码所以在编译器执行时应当指明成对应的编码。
解决办法:
如果是手动编译的 就在javac后面加上参数 -encoding UTF-8
如果是使用自定义编译器的就在方法上增加参数
public static void compile(String path, String outPath) {
try {
JavaCompiler javac;
javac = ToolProvider.getSystemJavaCompiler();
int compilationResult = -1;
compilationResult = javac.run(null, PanelLog.out, PanelLog.out, "-g", "-encoding", "utf-8", "-verbose", path);
if(compilationResult != 0){
throw new RuntimeException("编译失败!");
}
PanelLog.log("编译成功!");
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
注意不要把"-encoding", "utf-8"连在一起传参,回报错的!