Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java compilaton issue with ß character

I have come across a strange issue. In the below piece of code, I am searching for the presence of ß.

public static void main(String[] args) {
    char [] chArray = {'ß'};
    String str = "Testß";
    for(int i=0; i<chArray.length; i++){
        if(str.indexOf(chArray[i])>-1){
            System.out.println("ß is present");
            break;
        }
    }

}

I have a web application running on JBOSS in linux, Java 6. The above code doesn't detect the presence of ß when include the code in the above specified application. Surprisingly, if I compile the same file in my eclipse workspace and then apply the patch in the application it runs as expected!

Points to note:

  1. The application build environment is a black-box to me, hence no idea if there is any -encoding option is present for the javac command or something like that
  2. My eclipse's JRE is java8, but the compiler version set for the project is Java6
  3. I changed the value from ß to unicode equivalent to \u00DF in the array declaration, but still the behavior is same.

    char [] chArray = {'\u00DF'};

  4. When I decompiled the class file generated the character array declared value was shown as 65533, which is \uFFFD, nothing but replacement character which is used for unidentified symbol. I used JD-GUI as decompiler, which I dont see trustworthy!

Need your help folks! I am sure it is not same as: case sensitive issue of beta Java's equalsIgnoreCase fails with ß ("Sharp S" used in German alphabet)

Thanks in advance

like image 296
Betta Avatar asked Jul 28 '14 18:07

Betta


1 Answers

I think your problem is the encoding of ß. You have two options to solve your error:

  1. First convert your java source code into ascii chars, and then compile it:

    native2ascii "your_class_file.java"
    javac "your_class_file.java"
    
  2. Compile your java file with your encoding, utf-8 on linux and iso-8859-15 on windows:

    javac -encoding "encoding" "your_class_file.java"
    

As far as I can judge it, it should have worked with replacing "ß" with "\u00df". If the solutions above don't work, print every char and its unicode value to System.out and look which char is 'ß'.

Another error might be that you read the text in an encoding that doesn't support ß; try reading your String by reading the bytes and call:

String input = new String(input_bytes, StandartCharsets.UTF_8); // on linux
String input = new String(input_bytes, StandartCharsets.ISO_8859_1); // on windows

For more information on charsets, see StandartCharsets class reference.

like image 125
msrd0 Avatar answered Oct 16 '22 18:10

msrd0