1

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?

1 Answers1

2

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) 
Community
  • 1
  • 1
Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101
  • The problem is that I don't read or write - there, yes, I can specify the encoding manually. I get a String from javax.sound.sampled.Mixer.getName() and need to display it properly with system encoding. – Aleksei Riabikov Jan 25 '13 at 16:27
  • @RyabikovAleksey, to display where? Console, GUI? What encoding `getName` is supposed to produce? – Nikolay Kuznetsov Jan 25 '13 at 16:33
  • I have a GUI application, if it plays role. I suppose it produses CP1251. http://docs.oracle.com/javase/1.4.2/docs/api/javax/sound/sampled/Mixer.Info.html#getName() – Aleksei Riabikov Jan 25 '13 at 16:38
  • I used this code to convert: result=new String(result.getBytes(Charset.defaultCharset()), "CP1251"); but it fails in JAR, but works fine in debug. – Aleksei Riabikov Jan 25 '13 at 16:40
  • @RyabikovAleksey, in which GUI component you want to show? Use `UTF-8` – Nikolay Kuznetsov Jan 25 '13 at 16:41
  • I display it in swing JComboBox, but setting UTF-8 gives incorrect result. – Aleksei Riabikov Jan 25 '13 at 16:44
  • I think that the reason is that javax.sound.sampled.Mixer.Info.getName() encodes the OS Charset specific result with CP1251, so I need to decode it back to "OS Charset" – Aleksei Riabikov Jan 25 '13 at 16:55
  • @RyabikovAleksey, I believe it should not. Have you tried not converting at all? Just get and show? In Eclipse try to see implementation. – Nikolay Kuznetsov Jan 25 '13 at 16:59
  • Not converting does not work either, otherwise there would be no question :-) Looking in implementation gives that Info.name is only set in constructor, but constructor call search gives nothing... – Aleksei Riabikov Jan 25 '13 at 17:11