【问题标题】:Problems with encoding files when running standalone jar运行独立 jar 时编码文件的问题
【发布时间】:2012-07-03 15:14:58
【问题描述】:

我遇到了最奇怪的问题。我有一个将一些数据写入文件的 java 类。当我在 Eclipse 中执行它时,一切正常(以正确的编码写入数据)。但是当我将它作为独立 JAR 运行时,编码被破坏了。

我的项目中有这个(在 pom.xml 中):

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

我的 java 类在 Eclipse 中的属性(右键单击 -> 属性)文件编码设置为:UTF-8。

这是截断的 java 代码:

 //class field
    static BufferedWriter wr;
public static void main(String[] args) throws IOException {
        OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(args[0], true), "UTF-8");
        wr = new BufferedWriter(writer);
        //.processing logic, building up what needs to be in the file, line by line
        wr.close();
    }

附加到 .csv 文件的相关方法:

private void writeToCSVFile(String content){

        try {
            System.out.println(content);
            wr.write(content);
            wr.write("\n");
            wr.flush();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

再次,当我在 Eclipse 中打印时,输出看起来很好,就像它在我要附加到的文件中一样。但是当作为独立的 jar 编码运行时,由于某种原因会中断。这就是我配置相关插件的方式:

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>install</id>
                        <phase>install</phase>
                        <goals>
                            <goal>sources</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <finalName>Worker</finalName>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.test.automation.Testing</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

我真的不知道下一步该做什么。有什么想法吗?

这段代码没有任何异常,不会在任何地方失败。当我检查写入结果的文件时,在 Eclipse 中完全按照预期运行。

它也可以作为独立的 jar 使用,但只有当我检查写入结果的文件时,编码被搞砸了。

【问题讨论】:

  • 你怎么知道编码坏了?因为输出文件?

标签: java maven encoding maven-2 character-encoding


【解决方案1】:

您向我们展示的所有设置确实指定了用于编辑和编译代码的编码。

它没有说明运行代码时使用的编码。

显然,eclipse使用的jvm使用UTF-8作为它的file.encoding属性。

您的独立 jvm 对其 file.encoding 属性使用另一个值。

此外,当您读取数据时出现问题,而不是在您写入数据时,因为您明确指定了“UTF-8”编码输出。

您有 2 个解决方案:

解决方案 1:

您永远不会使用默认编码读取任何内容(始终通过指定正确的编码来创建阅读器)。

解决方案 2:

你告诉你的 jvm 它的默认编码应该是什么:

java -Dfile.encoding=UTF-8 ...

【讨论】:

    【解决方案2】:

    您必须了解您的 Eclipse 环境与独立环境不同,我将把钱花在您的 jar 文件中缺少的类上。当您在 Eclipse 中导出 jar 文件时,请确保打包引用的 jar,否则您的程序将失败。程序失败时是否有任何堆栈跟踪?

    【讨论】:

    • 我从未说过程序失败。它运行得非常顺利,完全符合我的预期
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    相关资源
    最近更新 更多