马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
处理处罚完成后的效果:
测试代码,build.gradle文件参加以下代码并实验:- task testChineseOutput {
- doLast {
- println new String('测试中文输出:你好,世界!'.getBytes('UTF-8'), 'UTF-8')
- }
- }
复制代码 全部修改步调:
1、build.gradle中的中文设置:- // ===================== Gradle 编码配置 =====================
- // 确保 Gradle 构建过程使用 UTF-8 编码
- tasks.withType(GroovyCompile) {
- options.encoding = 'UTF-8'
- }
- // 确保 Gradle 控制台输出使用 UTF-8 编码
- if (System.getProperty('os.name').toLowerCase().contains('windows')) {
- // Windows 环境下强制使用 UTF-8 编码
- System.setProperty('console.encoding', 'UTF-8')
- // 确保系统输出使用 UTF-8
- System.setProperty('file.encoding', 'UTF-8')
- System.setProperty('sun.jnu.encoding', 'UTF-8')
- // 确保 Gradle 守护进程使用 UTF-8
- System.setProperty('org.gradle.jvmargs', '-Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8')
- }
- // 确保所有任务都使用 UTF-8 编码
- tasks.withType(JavaCompile) {
- options.encoding = 'UTF-8'
- }
- // 确保测试任务使用 UTF-8 编码
- tasks.withType(Test) {
- systemProperty 'file.encoding', 'UTF-8'
- systemProperty 'sun.jnu.encoding', 'UTF-8'
- }
- // 确保资源处理使用 UTF-8 编码
- tasks.withType(ProcessResources) {
- // 资源处理编码设置
- filteringCharset = 'UTF-8'
- outputs.upToDateWhen { false }
- }
- // 确保文档生成使用 UTF-8 编码
- tasks.withType(Javadoc) {
- options.encoding = 'UTF-8'
- }
- // ===================== 编码全局配置 =====================
- tasks.withType(Test) {
- systemProperty 'file.encoding', 'UTF-8'
- }
- tasks.withType(JavaCompile).configureEach {
- options.encoding = "UTF-8"
- options.compilerArgs.addAll(['-Xlint:unchecked', '-Xlint:deprecation'])
- sourceCompatibility = JavaVersion.VERSION_17
- targetCompatibility = JavaVersion.VERSION_17
- }
- tasks.withType(JavaExec) {
- systemProperty 'file.encoding', 'UTF-8'
- systemProperty 'sun.jnu.encoding', 'UTF-8'
- defaultCharacterEncoding = 'UTF-8'
- }
- tasks.withType(Javadoc) {
- options.encoding = 'UTF-8'
- }
- // ===================== 编译兜底配置 =====================
- compileJava {
- options.encoding = 'UTF-8'
- options.compilerArgs.addAll(['-Xlint:unchecked', '-Xlint:deprecation'])
- sourceCompatibility = JavaVersion.VERSION_17
- targetCompatibility = JavaVersion.VERSION_17
- }
- java {
- buildSearchableOptions.defaultCharacterEncoding = 'UTF-8'
- sourceCompatibility = JavaVersion.VERSION_17
- targetCompatibility = JavaVersion.VERSION_17
- }
- javadoc {
- options.encoding = 'UTF-8'
- }
- // ===================== IDE运行配置 =====================
- runIde {
- // 解析JavaFX模块路径
- def javafxJars = configurations.runtimeClasspath.files.findAll {
- it.name.startsWith('javafx-') && it.name.endsWith('.jar')
- }
- def javafxModulePath = javafxJars.join(File.pathSeparator)
- // JVM参数(语法严谨,无冗余)
- jvmArgs = [
- '-Xmx2048m',
- '-Dfile.encoding=UTF-8',
- '-Dsun.jnu.encoding=UTF-8',
- "--module-path=${javafxModulePath}",
- '--add-modules=javafx.base,javafx.controls,javafx.fxml,javafx.graphics,javafx.swing,javafx.web',
- '--add-exports=javafx.web/com.sun.webkit.network=ALL-UNNAMED',
- '--add-exports=javafx.web/com.sun.webkit.graphics=ALL-UNNAMED',
- '--add-exports=javafx.web/com.sun.webkit.dom=ALL-UNNAMED',
- '--add-exports=javafx.graphics/com.sun.javafx.scene=ALL-UNNAMED',
- '--add-exports=javafx.web/com.sun.webkit=ALL-UNNAMED',
- '--add-opens=javafx.web/javafx.scene.web=ALL-UNNAMED',
- '--add-opens=javafx.web/com.sun.javafx.scene.web=ALL-UNNAMED',
- '--add-opens=javafx.web/com.sun.webkit=ALL-UNNAMED',
- '-Djavafx.media.threads=8',
- '-Dprism.forceGPU=false',
- '-Djava.net.preferIPv4Stack=true',
- '-Dsun.net.http.allowRestrictedHeaders=true',
- '-Dwebkit.media.player.useNative=true'
- ]
- // 编码环境变量(标准Map写法)
- environment([
- 'IDE_PROPERTIES_FILE_ENCODING': 'UTF-8',
- 'FILE_ENCODING': 'UTF-8'
- ])
- }
复制代码 2、gradle.properties中的编码设置:- org.gradle.jvmargs=-Dfile.encoding=UTF-8
- systemProp.file.encoding=UTF-8
复制代码 3、gradle-wrapper.properties中的编码设置:- org.gradle.jvmargs=-Dfile.encoding=UTF-8
复制代码 4、idea中VM的设置,插入
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的编码设置。
|