1

有人可以告诉我发生了什么吗,因为我的测试结果一直很奇怪。因此,我在 IntelliJ 中有一个 maven 项目,如果我在 IDE 中运行此代码(注意西里尔字符串),则输出符合预期。

JsonObject json = new JsonObject();
json.addProperty("message", "тест");
System.out.println("TEST:"+json.get("message").getAsString());

IDE 中的输出:TEST:тест

但是如果我将项目打包在一个 jar 文件中,JsonObject 的编码会以某种方式搞砸,上面的代码会导致:

jar 在控制台中的输出:TEST:????

我尝试过的事情:

  • 强制 JVM 使用 -Dfile.enconding=UTF-8 只是为了看看这是否会影响它,但没有任何改变。

  • 获取 json.get("message").getAsString() 的字节并创建一个具有指定编码的新字符串 new String(bytes, StandardCharsets.UTF_8)

  • 检查项目的 IntelliJ 运行参数(未指定任何内容)

  • ASCII 值高于 127 的所有字符都存在此问题(提到 ASCII,因为 UTF 和大多数编码从 ASCII 继承前 127 个字符值)

  • 这不是与控制台相关的问题,因为支持 UTF-8 的 UI 元素仍然存在同样的问题,这是一个示例: http: //prntscr.com/lmca64。这仍然只存在于导出 jar 的应用程序上!

你知道什么可能导致这种不一致的行为吗? 链接到 GSON 库

更新*:经过进一步测试,这里是以下代码的输出:

    JsonObject json = new JsonObject();
    json.addProperty("message", "тест");
    byte[] jsonBytes = json.get("message").getAsString().getBytes();
    for(byte b : jsonBytes) {
        System.out.print(b+" ");
    }
    System.out.println();
    byte[] stringBytes = "тест".getBytes();
    for(byte b : stringBytes) {
        System.out.print(b+" ");
    }

IDE输出:

-47 -126 -48 -75 -47 -127 -47 -126
-47 -126 -48 -75 -47 -127 -47 -126

编译后的 jar 控制台输出:

63 63 63 63
63 63 63 63

这表明两个字符串是相同的,但是一旦打包到一个 jar 中就会发生一些事情。 十进制到文本转换器

这是我的 pom.xml。在以错误的编码导出 jar 时,我可能做错了什么吗?

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<groupId>me.ivstiv</groupId>
<artifactId>Trapdoor-client</artifactId>
<version>1.0</version>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <finalName>Trapdoor-client-1.0-fat</finalName>
                <archive>
                    <manifest>
                        <mainClass>core.Main</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <appendAssemblyId>false</appendAssemblyId>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>8</source>
                <target>8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencies>
    <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.8.5</version>
    </dependency>
</dependencies>

4

0 回答 0