Reputation: 3729
I have a simple Java project. I do write code using Eclipse 4.2. Also I have use ant to build and launch my application. Thre result of my launch is a file. I do read an input file and produce output file.
The problem is: I get result in different encodings. When I do launch app through Eclipse I get output file in one encoding. When I do run the same app using ant I get result output file in other encoding.
Eclipse and ant use the same jdk to build and run application...
What do I do wrong... ?
I think that problem is somewere else. Please see a part of my build.xml:
<target name="compile" depends="init">
<javac destdir="${bin.dir}" classpathref="project.classpath"
source="1.7"
target="1.7"
debug="yes"
encoding="UTF-8"
srcdir="${src.dir}">
</javac>
<copy todir="${bin.dir}" >
<fileset dir="${src.dir}" excludes="**/*.java" />
</copy>
</target>
As you can see, encoding is specified. Source files are really in UTF-8.
Here is a code to launch main class:
<target name="prepare_inserts" depends="dist" description="Run preparation of SQL INSERT statements.">
<java classname="${main.class}" fork="true">
<classpath>
<path refid="project.classpath" />
<pathelement location="${dist.dir}/xBankImportDWHTestData-${DSTAMP}.jar" />
</classpath>
</java>
</target>
If i do run this target I get result file in wrong encoding. If I do run this file from Eclipse (right click on java file -> run as.. -> Java class) Everyting is ok. I did set encoding to UTF-8 in Eclipse project properties... What else can I try?
Upvotes: 0
Views: 943
Reputation: 718758
What do I do wrong...
What you did wrong was to rely on the default encoding. This depends on the environment in which the application is launched. With Ant you are most likely getting the OS (or shell) default encoding. With Eclipse, the default encoding is determined by Eclipse preferences and / or the launcher configuration. (Look in the "Common" tab of the launcher configuration for the setting that takes precedence.)
So the fix is one of the following:
Make sure that the default encoding is the same; e.g. by changing your application's Eclipse launch configuration.
Change your code to use the overload of FileWriter, OutputStreamWriter or whatever that takes the encoding name or Charset as a parameter. And make sure your code is consistent in what parameter it supplies.
Upvotes: 2