75

Windows下用命令行编译Java文件问题总结

 5 years ago
source link: https://wenshixin.gitee.io/blog/2018/09/30/Windows下用命令行编译Java文件问题总结/?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

在 Windows 下,由于默认字符编码的问题,在命令行中手动编译 Java 源文件会出现这样和那样的问题,这里做一个总结,给出每个问题的解决方法。

错误: 编码GBK的不可映射字符

文件中有中文就会出现这个错误。

解决方法:

1、在编译命令中加入编码设置,即命令改为 javac -encoding utf-8 Java源文件

2、将Java源文件用记事本打开另存为 ANSI 编码的文件,不过不推荐这种方式。

错误: 非法字符: ‘\ufeff’

utf-8编码也分 utf-8 with bom 和 utf-8 no with bom 两种,Windows 下使用记事本保存为 utf-8 的文件,会给你加上 bom,有没有 bom 简单的来说就是文件开头有没有 \ufeff 字符,而标准的 utf-8 是不需要 bom 的,Linux 下utf-8 不带 bom。

解决方法:

修改文件的编码为 utf-8 无 bom,可以借助一些可以修改文件编码的编辑器(如 Sublime Text、Notepad++等),别用记事本修改编码了。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK