当前位置: 首页 > article >正文

Gradle IDEA 乱码

文章目录

    • 环境
    • 代码
    • 测试结果
      • 配置 JAVA_TOOL_OPTIONS
      • 配置 build.gradle.kts
      • 配置 idea64.exe.vmoptions
      • 无配置
    • 总结
    • 问题链接

环境

Java 环境

java version "21.0.2" 2024-01-16 LTS
Java(TM) SE Runtime Environment (build 21.0.2+13-LTS-58)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.2+13-LTS-58, mixed mode, sharing)

Gradle 环境

------------------------------------------------------------
Gradle 8.5
------------------------------------------------------------

Build time:   2023-11-29 14:08:57 UTC
Revision:     28aca86a7180baa17117e0e5ba01d8ea9feca598

Kotlin:       1.9.20
Groovy:       3.0.17
Ant:          Apache Ant(TM) version 1.10.13 compiled on January 4 2023
JVM:          21.0.2 (Oracle Corporation 21.0.2+13-LTS-58)
OS:           Windows 11 10.0 amd64

IDEA

IntelliJ IDEA 2023.3.3 (Ultimate Edition)
Build #IU-233.14015.106, built on January 25, 2024
Subscription is active until October 14, 2024.
Runtime version: 17.0.9+7-b1087.11 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 11.0
GC: G1 Young Generation, G1 Old Generation
Memory: 2048M
Cores: 32
Registry:
  debugger.new.tool.window.layout=true
  ide.experimental.ui=true
Non-Bundled Plugins:
  com.jetbrains.packagesearch.intellij-plugin (233.11799.196)
  com.intellij.ml.llm (233.14015.137)
Kotlin: 233.14015.106-IJ

代码

Main.java

package com.stu;

public class Main {

    public static void main(String[] args) {
        Main main = new Main();
        main.test00();
    }

    public void test00() {
        System.out.println("中文测试");
        System.out.println("sun.stdout.encoding = " + System.getProperty("sun.stdout.encoding"));
        System.out.println("sun.stderr.encoding = " + System.getProperty("sun.stderr.encoding"));
        System.out.println("      file.encoding = " + System.getProperty("file.encoding"));
        System.out.println("    native.encoding = " + System.getProperty("native.encoding"));
    }
}

build.gradle.kts

/*
 * This file was generated by the Gradle 'init' task.
 *
 * This is a general purpose Gradle build.
 * To learn more about Gradle by exploring our Samples at https://docs.gradle.org/8.5/samples
 */

plugins {
    id("java")
}

tasks.register("runCode", JavaExec::class) {
    //defaultCharacterEncoding = "GBK"

    // println("测试Code" + defaultCharacterEncoding)

    mainClass = "com.stu.Main"
    classpath = sourceSets["main"].runtimeClasspath

    group = "Exec"
    description = "运行指定代码"
}

// tasks.register("runStu") {
//     for (sourceSetContainer in sourceSets) {
//         println(sourceSetContainer)
//     }

//     println(sourceSets)
// }

// tasks.withType<JavaCompile>().configureEach {

// }

/*
tasks.withType(JavaExec::class.java) {
    val args:List<String> = ArrayList()
    args.addLast("-Dsun.stdout.encoding=GBK")
    args.addLast("-Dsun.stderr.encoding=GBK")
    args.addLast("-Dfile.encoding=GBK")
    this.jvmArgs = args
    println(this.jvmArgs)
}
*/

测试结果

配置 JAVA_TOOL_OPTIONS

无效

# 配置 JAVA_TOOL_OPTIONS  -Dsun.stdout.encoding=GBK -Dsun.stderr.encoding=GBK -Dfile.encoding=GBK

> Task :runCode
涓枃娴嬭瘯
sun.stdout.encoding = UTF-8
sun.stderr.encoding = UTF-8
      file.encoding = GBK
    native.encoding = GBK

配置 build.gradle.kts

有效

tasks.withType(JavaExec::class.java) {
    val args:List<String> = ArrayList()
    args.addLast("-Dsun.stdout.encoding=GBK")
    args.addLast("-Dsun.stderr.encoding=GBK")
    args.addLast("-Dfile.encoding=GBK")
    this.jvmArgs = args
    println(this.jvmArgs)
}
> Task :runCode
中文测试
sun.stdout.encoding = GBK
sun.stderr.encoding = GBK
      file.encoding = GBK
    native.encoding = GBK

配置 idea64.exe.vmoptions

无效

-Dsun.stdout.encoding=GBK
-Dsun.stderr.encoding=GBK
-Dfile.encoding=GBK
> Task :runCode
涓枃娴嬭瘯
sun.stdout.encoding = UTF-8
sun.stderr.encoding = UTF-8
      file.encoding = GBK
    native.encoding = GBK

无配置

无效


> Task :runCode
涓枃娴嬭瘯
sun.stdout.encoding = UTF-8
sun.stderr.encoding = UTF-8
      file.encoding = GBK
    native.encoding = GBK

总结

只有配置JavaExec 运行环境才能不乱码,配置了JAVA_TOOL_OPTIONS,可以在VSCode 中 PowerShell 执行任务中日志不乱码,但是执行结果还是乱码。

问题链接

Gradle 社区问题: Gradle Garbled code

Stack overflow: Why do Java programs created using Gradle generate garbled code when running


http://www.kler.cn/a/233170.html

相关文章:

  • 搭建Python2和Python3虚拟环境
  • 4.4 软件设计:UML顺序图
  • 【pytorch】常用强化学习算法实现(持续更新)
  • python装饰器的使用以及私有化
  • 系统架构设计师论文
  • 系统架构设计师论文:大数据Lambda架构
  • 图灵之旅--二叉树堆排序
  • Android 判断通知是进度条通知
  • 生成式人工智能攻击的一年:2024
  • 【Mybatis】从0学习Mybatis(2)
  • Electron基本介绍
  • [职场] 如何通过运营面试_1 #笔记#媒体#经验分享
  • centos7.9 安装rabbitmq 3.6.15 集群
  • MySQL的DDL语言
  • idea: 无法创建Java Class文件(SpringBoot)已解决
  • 部署一个在线OCR工具
  • [office] 怎么在Excel2003菜单栏自定义一个选项卡 #其他#微信#知识分享
  • Bean 的六种作用域
  • 破除Github API接口的访问次数限制
  • Android 车载应用开发之车载操作系统
  • Flink cdc3.0动态变更表结构——源码解析
  • Spring 开发 pom.xml 配置文件(通用配置)
  • C++类和对象(7)
  • 【k8s系列】(202402) 证书apiserver_client_certificate_expiration_seconds
  • 龙芯开启ssh服务——使用Putty连接
  • Web后端开发:事务与AOP