Search code examples
javashellencodingcompilationjavac

javac marks my comments as errors


I'm trying to compile 3 java files located in: /var/www/PhpProject2/Solutions/5/9/1/ on my Computer (Linux Ubuntu).

I used the following command in my shell: javac /var/www/PhpProject2/Solutions/5/9/1/*.java

and it outputs 100 errors that looks like this:

error: unmappable character for encoding UTF8 mergeSort(arr); // ���� �� ����� ������ ��� ������ ���

with an arrow pointing at one of the � signs.

This is new to me because for all I know Compilation gets rid of all comments in my code, not treating them as errors.

What I have tried so far:

change the command to: javac -encoding UTF-8 /var/www/PhpProject2/Solutions/5/9/1/*.java

look for the file encoding with: file -bi /var/www/PhpProject2/Solutions/5/9/1/UnionIntervals.java

and then I tried: javac -encoding text/x-c++; charset=iso-8859-1 /var/www/PhpProject2/Solutions/5/9/1/*.java

all ended up with the same error.


Solution

  • Given the following minimalistic Java file:

    > cat test.java 
    // ä
    

    The following command gives exactly the error you describe:

    > javac -encoding utf8 test.java 
    test.java:1: warning: unmappable character for encoding utf8
    // ?
    

    When using the correct encoding, the file is processed without errors:

    > javac -encoding iso-8859-1 test.java 
    

    This is because the Java compiler needs to parse the whole file (including the comments). The comments are only stripped off after the parse.