Take the 2-minute tour ×

I have a very simple code:

package org;

import java.nio.charset.Charset;

public class Test
  public static void main(String[] args)

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:


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=".">
        Simple build XML

    <target name="init">
        <delete dir="${build}"/>
        <mkdir dir="${build}"/>

        <javac srcdir="${src}" destdir="${build}" includeantruntime="false"/>

    <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}">
                <attribute name="Main-Class" value="org.Test"/>

I get the following result when running it:

>java -jar test.jar

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?

share|improve this question
add comment

1 Answer

The answer is that you should never rely on default character encoding:

How to Find 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) 
share|improve this answer
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. –  Ryabikov Aleksey 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. docs.oracle.com/javase/1.4.2/docs/api/javax/sound/sampled/… –  Ryabikov Aleksey 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. –  Ryabikov Aleksey 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
show 4 more comments

Your Answer


