使用Eclipse和Maven进行Java编码 [英] Java encoding with Eclipse and Maven

查看:112
本文介绍了使用Eclipse和Maven进行Java编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常看到编码出现问题.因此,我写下了该指令集以执行所有必需的操作,以使工作正常(使用编码). 此设置与Eclipse有关,但也将指导maven设置.

I have often seen problems arise with encoding. Therefore I have written down this instruction set to do all the needed actions in order to make things work (with encoding). This set is related to Eclipse but it will also guide with maven settings.

在Java文件中使用斯堪的纳维亚字母时,编码问题最为棘手(åäö,它们在运行时具有实际含义).

The issue with encoding is most problematic when using scandinavian letters in java files (åäö, and they had actual meaning on runtime).

一个例子是在java文件中有一个常量变量,该常量包含一个scandic字母,用于从传入流中识别一个值(其中以UTF-8表示).

An example case is having a constant variable in a java file, that contains a scandic letter and it is used to identify a value from incoming stream (wich is in UTF-8).

底层操作系统也可能是Windows,默认情况下使用的是cp1252.

Also the underlying OS may be Windows and they use cp1252 by default.

例如以下代码:

@Test
public void scandicTest() {
    System.out.println("scandics: åäö");
}

正确配置所有内容后(例如在eclipse中),运行此测试将产生:

When everything is configured correctly (e.g. in eclipse), running this test will produce:

scandics: åäö

但是,如果您通过Maven(从命令行或在eclipse => mvn test中)运行此代码,则将具有:

But if you run this via Maven (from command line or in eclipse => mvn test), you will have:

scandics: ���

首先,需要在eclipse中以及在maven pom.xml中更改编码,以正确读取和存储文件,并在保存文件/运行测试时让eclipse使用正确的编码. 但是,即使当Maven和生成的Java代码处理传入流(已编译并运行测试)时,即使读入的文件正确(包含Scandic字母),Java文件本身中的常量值仍会损坏.

First of all, the encoding needs to be changed in eclipse and also in the maven pom.xml to read and store files correctly and for the eclipse to use correct encoding when saving the files / running tests. However the constant value in the java file itself remains corrupted even that the files read in are correct (containing the scandic letters) when the Maven and the resulting java code handled the incoming streams (compiled & run the tests).

即使正确设置了其他所有设置,System Java仍使用特定于操作系统的默认编码.因此,您不能在项目中进行全部配置,还必须对OS-JVM进行配置.

The System Java still uses a OS specific default encoding even that everything else is set correctly. For this reason you can not configure all within the project, you must do it for the OS-JVM also.

推荐答案

我将解释为此所需的所有编码步骤,即使该公共"部分已经有多个答案(至少对于步骤2).我的特殊情况是解决步骤3.

I will explain all the the encoding steps needed for this, even that there are multiple answers for this "common" part already (at least for step 2). My particular case is to resolve step 3.

  1. 配置日食:

  1. Configure the eclipse:

  • 打开:窗口>首选项
  • 在搜索字段中输入编码"
  • 会有很多条目,但首先选择常规>工作区"
  • 找到文本文件编码",然后选择:其他> UTF-8
  • 您还希望/需要也为所有常规>内容类型"
  • 设置编码.
  • 从右侧面板中选择文本"项(将打开文件类型列表),然后浏览所有类型.将其默认编码"设置为"UTF-8"
  • 单击更新"按钮以保留更改.
  • 对于在搜索中找到的所有其他条目和项目,您可能还需要执行此操作.
  • 例如网页> CSS文件>编码" | ISO 10646/Unicode(UTF-8)
  • 全部设置后,Eclipse应该在编码方面表现正常.
  • Open: Window > Preferences
  • Type 'encoding' in the search field
  • There will be lots of entries, but first select the 'General > Workspace'
  • Find the 'Text file encoding' and select: Other > UTF-8
  • You also want/need to set the encoding also for all the 'General > Content Types'
  • Select 'text' item from the right hand panel (will open a list of file types), and browse through all the types. Set their 'Default encoding' to 'UTF-8'
  • Click the 'update' button to persist the change.
  • You may need to do this also for all the other entries and items found with the search.
  • E.g. 'Web > CSS Files > Encoding' | ISO 10646/Unicode(UTF-8)
  • When all set, the Eclipse should behave properly with the encoding.

在maven.pom.xml中设置编码

Set the encoding in maven.pom.xml

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

  • 您可能还需要为所有插件设置编码.

    • You may need to set the encoding for all plugins also.

      <plugin>
        ...
        <configuration>
          <encoding>UTF-8</encoding>
          ...
        </configuration>
      </plugin>
      

      <plugin>
        <executions>
          <execution>
            <configuration>
              <encoding>UTF-8</encoding>
              ...
            </configuration>
            ...
          </execution>
        </executions>
      </plugin>
      

      尽管我不确定后者是否是强制性的,或者实际上是否采用默认值.

      Though i am not sure if the latter is mandatory or if it will take the default actually.

      配置操作系统

      • 您需要将环境变量JAVA_TOOL_OPTIONS设置为值-Dfile.encoding=UTF8
      • You need to set environment variable JAVA_TOOL_OPTIONS with value -Dfile.encoding=UTF8

      如评论中所建议,以下是有关转换文件的更多信息:
      您应该注意,所有文件都必须具有UTF-8编码才能起作用.如果您使用给定的配置通过eclipse编辑所有内容,则它们将为UTF-8.
      如果收到应使用代码处理的文件,则可能需要转换该文件.您只需在Eclipse中打开它并再次保存文件即可(您可能需要添加和删除字符以启用保存功能).
      如果可以使用NotePad ++,则有一个编码"菜单用于转换文件. 转换文件时,scandic有时可能会损坏,因此转换后需要手动检查它们.

      As suggested in comment, here's some more info for converting a file:
      You should note that all the files must have the UTF-8 encoding in order them to work. If you edit everything via eclipse with the given configuration, they will be as UTF-8.
      If you receive a file that you should process with your code, you may need to convert that. You can simply do that by opening it in eclipse and saving the file again (you may need to add and remove a character to enable saving).
      If you can use NotePad++, there is an 'encoding' menu for converting the file. When converting a file, the scandics may get corrupted sometimes, so you need to check them manually after conversion.

      还有一件事.保存在其他工具中的文件可能具有BOM. (字节顺序标记).该字符"是不可见的,例如,某些解析器无法读取包含该字符的XML文件. 您可以通过以下方式删除BOM表标记:在eclipse中打开文件,并将光标设置在文件中的第一个字符之前,然后在选项卡上单击退格键".没有任何变化,但实际上已删除了角色,然后文件开始工作.

      And one more thing. The files saved in other tools, may have the BOM. (Byte Order Mark). This 'character' is invisible and for example an XML file containing this can not be read in by some parsers. You can remove the BOM mark by opening the file in eclipse and setting the cursor before the first character in the file, then tab once the 'backspace'. Nothing changes, but the character gets actually removed and the file works then.

      NotePad可能会插入BOM标记,因此请勿将其用于编辑XML文件!

      NotePad may insert the BOM-mark, so do not use it for editing XML files!

      这篇关于使用Eclipse和Maven进行Java编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆