Sergey Karpushin
Sergey Karpushin

Reputation: 874

Is there a way to build UTF-8 source files using ant

I have UTF-8 string literals hardcoded in my java files. Eclipse builds this application correctly, so resulting class files contains those strings in UTF-8. But If use ant build.xml, resulting class files contains strings with incorrect encoding.

I already tried adding encoding="UTF-8" to the javac task, but with no success.

How it can be fixed?

p.s. I know this is quite bad practice to have string literals hardcoded in the source files, but this is situation when I need it there, so please don't suggest to extract it to the resource bundle.

Any help is greatly appreciated

Upvotes: 0

Views: 1418

Answers (3)

Sergey Karpushin
Sergey Karpushin

Reputation: 874

This is not actually an answer to original problem, but I don't want this question to be unanswered. Maybe moderators will decide to delete it. Anyway.

Looks like this is some kind of bug when using specific combination of ant, jdk, windows. I dug really deep and wasn't able to fix this in a let's say normal way.

So I decided to externalize string to properties file, which is anyway better practice...

Normally solution suggested by Mikhail Vladimirov should work fine, but not this time.

Upvotes: 0

Jayan
Jayan

Reputation: 18459

The encoding must match the encoding of the file. Your guess of utf-8 may not be correct. Please check other suitable encoding names like iso-8859-1.

Please check upvoted answers in How do I set -Dfile.encoding within ant's build.xml?

Upvotes: 0

Mikhail Vladimirov
Mikhail Vladimirov

Reputation: 13890

Proper way is

<javac ... encoding="UTF-8" ... />

If in resulting class files strings are in incorrect encoding, it means that probably your source encoding is not UTF-8, or these files are compiled by some other javac task, not the one you modified.

Upvotes: 1

Related Questions