Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What would make Ant execute 'javac' in Eclipse with UTF-8 instead of my system default Cp1252?

The last week I've been trying to figure out why some stream decoding my newly adopted application is doing was giving me some major encoding problems. Finally I figured out that the problem was that the JARs/WAR being built with Ant and deployed to the server were being compiled with the javac task using the encoding UTF-8 instead of the system default of CP1252.

This seems to be caused mainly by having many hard coded strings/chars for these special characters.

This was easily resolved by either of the following steps:

  • changing the encoding for the eclipse project to be UTF-8 to match the byte code on the server
  • setting the encoding for the javac task to be CP1252 to build the WAR file to match the client byte code
  • and strangely enough just running Ant from the command prompt without designating any encoding.

So why is Ant in Eclipse changing to UTF-8? Is this configurable? Where do I configure it?

System

  • Windows XP
  • Eclipse 3.5
  • Ant 1.7.1
  • Java 1.6.0_11
like image 563
codeLes Avatar asked Aug 17 '09 23:08

codeLes


3 Answers

Ant, run from Eclipse, using all the same versions (except I have Java 1.6.0_15) treats my Java source files as Windows-1252. My workspace and projects are using the default settings.

UTF-8 to match the byte code on the server

I'm not sure what you mean by this - you mean the encoding of the source files, surely. The bytecode is a structured set of instructions; string literals built into the class files are always UTF-8.


I would use Unicode escape sequences to make my files more encoding-agnostic. You can use tools like native2ascii or the java.nio.charset API to help with this.

like image 106
McDowell Avatar answered Sep 18 '22 17:09

McDowell


When using Ant on the command line, it automatically uses the system default encoding, which seems to be windows-1252 on your system.

When using Ant from Eclipse, it reads the local encoding property of the source files/folders to determine which one must be used. This property is in the Resource page of the Properties dialog, available when right-clicking on a source folder.

When nothing is specified, the workspace wide default encoding is used. It is configurable from withing the Window>Preferences dialog.

Hope this helps.

like image 41
Chucky Avatar answered Sep 20 '22 17:09

Chucky


Something similar is happening on my computer. It's Windows, and the text file encoding configuration in Eclipse is set at the default (Cp1252).

Ant from the command-line does not throw a fit when compiling code with "strange" characters (e.g., í, ñ, ü). But the same Ant task from inside Eclipse does.

I've had to configure the Ant task in Eclipse with this parameter in the JRE tab:

-Dfile.encoding=Cp1252

The javac part of the Ant task works fine now.

like image 39
Ivan Maeder Avatar answered Sep 21 '22 17:09

Ivan Maeder