Reputation: 11
I have a very simple code:
package org;
import java.nio.charset.Charset;
public class Test
{
public static void main(String[] args)
{
System.out.println(Charset.defaultCharset());
}
}
I run it on Windows7 RUS JRE7.
When I run it from Eclipse 4.2 (20120614-1722) with "Debug" or "Run" (I didn't set any additional encoding preferences for Eclipse or project), I get the following result:
windows-1252
And it seems correct for me.
But when I pack this with ANT 1.8.2 into JAR with default settings:
<project name="Test" default="dist" basedir=".">
<description>
Simple build XML
</description>
<!-- set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist"/>
<target name="init">
<!-- Create the build directory structure used by compile -->
<delete dir="${build}"/>
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init" description="compile the source" >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}" includeantruntime="false"/>
</target>
<target name="dist" depends="compile" description="generate the distribution" >
<!-- Create the distribution directory -->
<delete dir="${dist}"/>
<mkdir dir="${dist}"/>
<!-- Put everything from ${build} into the test.jar file -->
<jar jarfile="${dist}/test.jar" basedir="${build}">
<manifest>
<attribute name="Main-Class" value="org.Test"/>
</manifest>
</jar>
</target>
</project>
I get the following result when running it:
>java -jar test.jar
windows-1251
I was hoping to get the same result here. What am I doing wrong? Which is the correct way to determine default charset then?
Update: The reason to ask this question was, that I don't really read or write file, where I can specify encoding manually. I simply have a String returned from javax.sound.sampled.Mixer.Info.getName() and need to display it properly in different system charsets.
Update2: It seems, that the reason is that javax.sound.sampled.Mixer.Info.getName() encodes the "System Charset" specific result with CP1251, so I need to decode it back to "System Charset". But how to find it out?
Upvotes: 1
Views: 7523
Reputation: 9579
The answer is that you should never rely on default character encoding:
How to Find the Default Charset/Encoding in Java?
Setting the default Java character encoding?
Instead, when you write or read you need to specify desired encoding explicitly.
For example, 2nd constructor should be preferred.
InputStreamReader(InputStream in)
InputStreamReader(InputStream in, String charsetName)
Upvotes: 2