我正在将一个项目从Ant转换为Maven,我遇到了一个处理UTF-8字符的特定单元测试的问题.问题是关于以下字符串:
String l_string = "?äÁÓý\n€????\n?????";
Run Code Online (Sandbox Code Playgroud)
问题是单元测试失败,因为String被读取如下:
?äÁÓý
€????
?????
Run Code Online (Sandbox Code Playgroud)
java类保存为UTF-8,我还在pom.xml中指定了UTF-8的构建编码.
这是我的pom.xml的摘录:
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.15</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.15</version>
</plugin>
</plugins>
</build>
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?如果有人能在这里帮助我,那就太好了.
更新
关于测试代码:
@Test
public void testTransformation()
{
String l_string = "?äÁÓý\n€????\n?????";
System.out.println( ">>> " + l_string );
c_log.info( l_string );
StringBuffer l_stringBuffer = new StringBuffer();
int l_stringLength = …Run Code Online (Sandbox Code Playgroud) 我正在开发一个涉及maven,java和clojure的项目.我面临的问题是,我UTF-8在我的clojure源文件中有一些字符,因为我的源代码没有被java编译器正确解释,我有点通过设置环境变量来工作JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8,但我想要的是通过MAVEN传递此属性.
我已经尝试过设置,MAVEN_OPTS=-Dfile.encoding但这似乎不起作用.
我也试过为maven的编译器插件设置配置......这样的事情:
<configuration>
<compilerArgument>-Dfile.encoding=UTF8</compilerArgument>
</configuration>
Run Code Online (Sandbox Code Playgroud)
这也不起作用.
我是在做错事,还是有别的办法.
谢谢,
RD
好的,这里有一些更详细的信息.这是我的父母pom,
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
<encoding>UTF-8</encoding> <! also tried <encoding>UTF8</encoding>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
孩子的pom没有什么有趣的事,除了...
<resources>
<resource>
<directory>src/main/clojure</directory>
</resource>
</resources>
Run Code Online (Sandbox Code Playgroud)
;; clojure代码片段会导致问题
(let [char "?"]
(not (empty? (filter #(s/contains? % char) <some-list>)))
;; The list is always empty because I never find a match if I do not set the env. variable
Run Code Online (Sandbox Code Playgroud) 我有一个简单的表单,我可以输入一些字符.这些字符被发送到一个servlet,它执行getBytes并打印字节."ã"的正确UTF-8字节是-61和-93,但我得到-52和-93.:(
我尝试了一切来理解和解决这个问题,但没有任何效果.我的机器上的所有东西都应该是UTF-8,所以我怀疑它与我使用了20年的美国国际键盘有关.
有没有聪明的灵魂从-52和-93来自何处?
在Jetty上固定:请参阅下面的答案.
Tomcat上的BROKEN:如何让tomcat从我的Mac键盘上理解MacRoman(x-mac-roman)字符集?
使用 Netbeans 9:
\n\nProduct Version: Apache NetBeans IDE 9.0 (Build incubator-netbeans-release-334-on-20180708)\nJava: 1.8.0_181; Java HotSpot(TM) 64-Bit Server VM 25.181-b13\nRuntime: Java(TM) SE Runtime Environment 1.8.0_181-b13\nSystem: Windows 10 version 10.0 running on amd64; UTF-8; en_EN (nb)\nRun Code Online (Sandbox Code Playgroud)\n\n我希望能够打印:
\n\nString text = "\xe4\xbd\xa0\xe5\xa5\xbd\xef\xbc\x81";\nSystem.out.println(text);\nRun Code Online (Sandbox Code Playgroud)\n\n结果是:
\n\n--- exec-maven-plugin:1.5.0:exec (default-cli) @ JavaApplication1 ---\n???\nRun Code Online (Sandbox Code Playgroud)\n\n我已经添加-J-Dfile.encoding=UTF-8到了配置/etc/netbeans.conf中,还添加到了VM选项中。Sources 编码选项也设置为 UTF-8。\n过去版本的 Netbeans 没有问题,这里我发现无法显示 UTF-8 字符。
我可以采取什么方式呢?
\njava ×4
utf-8 ×3
encoding ×2
clojure ×1
maven ×1
maven-2 ×1
netbeans ×1
netbeans-9 ×1
servlets ×1
system.out ×1