文章目录
-
- 环境
- 代码
- 测试结果
-
- 配置 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 a服务器托管网bout 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 s服务器托管网ourceSets) {
// println(sourceSetContainer)
// }
// println(sourceSets)
// }
// tasks.withType().configureEach {
// }
/*
tasks.withType(JavaExec::class.java) {
val args:List = 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:ListString> = 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.fwqtg.net
超文本标记语言 HTML 在一个客户程序主窗口上显示出的万维网文档称为页面 (page)。 页面制作的标准语言:HTML。 超文本标记语言 HTML (HyperText Markup Language) 是一种制作万维网页面的标准语言,它消除了不同计算机之间…