记载一次idea上gradle项目标中文乱码题目

[复制链接]
发表于 2026-1-13 13:11:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
处理处罚完成后的效果:

 测试代码,build.gradle文件参加以下代码并实验:
  1. task testChineseOutput {
  2.   doLast {
  3.     println new String('测试中文输出:你好,世界!'.getBytes('UTF-8'), 'UTF-8')
  4.   }
  5. }
复制代码
全部修改步调:
1、build.gradle中的中文设置:
  1. // ===================== Gradle 编码配置 =====================
  2. // 确保 Gradle 构建过程使用 UTF-8 编码
  3. tasks.withType(GroovyCompile) {
  4.     options.encoding = 'UTF-8'
  5. }
  6. // 确保 Gradle 控制台输出使用 UTF-8 编码
  7. if (System.getProperty('os.name').toLowerCase().contains('windows')) {
  8.     // Windows 环境下强制使用 UTF-8 编码
  9.     System.setProperty('console.encoding', 'UTF-8')
  10.     // 确保系统输出使用 UTF-8
  11.     System.setProperty('file.encoding', 'UTF-8')
  12.     System.setProperty('sun.jnu.encoding', 'UTF-8')
  13.     // 确保 Gradle 守护进程使用 UTF-8
  14.     System.setProperty('org.gradle.jvmargs', '-Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8')
  15. }
  16. // 确保所有任务都使用 UTF-8 编码
  17. tasks.withType(JavaCompile) {
  18.     options.encoding = 'UTF-8'
  19. }
  20. // 确保测试任务使用 UTF-8 编码
  21. tasks.withType(Test) {
  22.     systemProperty 'file.encoding', 'UTF-8'
  23.     systemProperty 'sun.jnu.encoding', 'UTF-8'
  24. }
  25. // 确保资源处理使用 UTF-8 编码
  26. tasks.withType(ProcessResources) {
  27.     // 资源处理编码设置
  28.     filteringCharset = 'UTF-8'
  29.     outputs.upToDateWhen { false }
  30. }
  31. // 确保文档生成使用 UTF-8 编码
  32. tasks.withType(Javadoc) {
  33.     options.encoding = 'UTF-8'
  34. }
  35. // ===================== 编码全局配置 =====================
  36. tasks.withType(Test) {
  37.   systemProperty 'file.encoding', 'UTF-8'
  38. }
  39. tasks.withType(JavaCompile).configureEach {
  40.   options.encoding = "UTF-8"
  41.   options.compilerArgs.addAll(['-Xlint:unchecked', '-Xlint:deprecation'])
  42.   sourceCompatibility = JavaVersion.VERSION_17
  43.   targetCompatibility = JavaVersion.VERSION_17
  44. }
  45. tasks.withType(JavaExec) {
  46.   systemProperty 'file.encoding', 'UTF-8'
  47.   systemProperty 'sun.jnu.encoding', 'UTF-8'
  48.   defaultCharacterEncoding = 'UTF-8'
  49. }
  50. tasks.withType(Javadoc) {
  51.   options.encoding = 'UTF-8'
  52. }
  53. // ===================== 编译兜底配置 =====================
  54. compileJava {
  55.   options.encoding = 'UTF-8'
  56.   options.compilerArgs.addAll(['-Xlint:unchecked', '-Xlint:deprecation'])
  57.   sourceCompatibility = JavaVersion.VERSION_17
  58.   targetCompatibility = JavaVersion.VERSION_17
  59. }
  60. java {
  61.   buildSearchableOptions.defaultCharacterEncoding = 'UTF-8'
  62.   sourceCompatibility = JavaVersion.VERSION_17
  63.   targetCompatibility = JavaVersion.VERSION_17
  64. }
  65. javadoc {
  66.   options.encoding = 'UTF-8'
  67. }
  68. // ===================== IDE运行配置 =====================
  69. runIde {
  70.   // 解析JavaFX模块路径
  71.   def javafxJars = configurations.runtimeClasspath.files.findAll {
  72.     it.name.startsWith('javafx-') && it.name.endsWith('.jar')
  73.   }
  74.   def javafxModulePath = javafxJars.join(File.pathSeparator)
  75.   // JVM参数(语法严谨,无冗余)
  76.   jvmArgs = [
  77.           '-Xmx2048m',
  78.           '-Dfile.encoding=UTF-8',
  79.           '-Dsun.jnu.encoding=UTF-8',
  80.           "--module-path=${javafxModulePath}",
  81.           '--add-modules=javafx.base,javafx.controls,javafx.fxml,javafx.graphics,javafx.swing,javafx.web',
  82.           '--add-exports=javafx.web/com.sun.webkit.network=ALL-UNNAMED',
  83.           '--add-exports=javafx.web/com.sun.webkit.graphics=ALL-UNNAMED',
  84.           '--add-exports=javafx.web/com.sun.webkit.dom=ALL-UNNAMED',
  85.           '--add-exports=javafx.graphics/com.sun.javafx.scene=ALL-UNNAMED',
  86.           '--add-exports=javafx.web/com.sun.webkit=ALL-UNNAMED',
  87.           '--add-opens=javafx.web/javafx.scene.web=ALL-UNNAMED',
  88.           '--add-opens=javafx.web/com.sun.javafx.scene.web=ALL-UNNAMED',
  89.           '--add-opens=javafx.web/com.sun.webkit=ALL-UNNAMED',
  90.           '-Djavafx.media.threads=8',
  91.           '-Dprism.forceGPU=false',
  92.           '-Djava.net.preferIPv4Stack=true',
  93.           '-Dsun.net.http.allowRestrictedHeaders=true',
  94.           '-Dwebkit.media.player.useNative=true'
  95.   ]
  96.   // 编码环境变量(标准Map写法)
  97.   environment([
  98.           'IDE_PROPERTIES_FILE_ENCODING': 'UTF-8',
  99.           'FILE_ENCODING': 'UTF-8'
  100.   ])
  101. }
复制代码
2、gradle.properties中的编码设置:
  1. org.gradle.jvmargs=-Dfile.encoding=UTF-8
  2. systemProp.file.encoding=UTF-8
复制代码
3、gradle-wrapper.properties中的编码设置:
  1. org.gradle.jvmargs=-Dfile.encoding=UTF-8
复制代码
4、idea中VM的设置,插入
  1. -Dfile.encoding=UTF-8
复制代码

 5、idea的setting设置:

6、idea的gradle设置:

7、run plugin 等启动器的设置,参加VM参数-Dfile.encoding=UTF-8:

 
 
走到这一步,如果中文乱码题目已经处理处罚好,分析你已经很荣幸了,但实际上并没有效。哈哈哈哈
终极方案,情况变量+体系重启+重编译:

 参加情况变量:
GRADLE_OPTS  -Dfile.encoding=utf-8
JAVA_TOOL_OPTIONS  -Dfile.encoding=UTF-8
 -------------------------------------------------完事------------------------------------------------------
以上就是个人历时三周,各种查资料,各种AI的心路历程。
重要缘故起因就是,个人开辟风俗UTF-8的编码,但是实际上CMD上实验下令 chcp 会发现,体系默认的是GBK

 以是必要各种修改,最快捷的办法照旧先 chcp 查一下当前体系默认编码,然后直接去修改IDEA的编码设置。 
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表