'AddCharset UTF-8 doesn't seem to have any effect for .java

I've got the following in the .htaccess located in the root directory of website https://klipspringer.avadeaux.net/:

AddDefaultCharset UTF-8
AddCharset UTF-8 .js
AddCharset UTF-8 .html
AddCharset UTF-8 .java
AddCharset UTF-8 .c
AddCharset UTF-8 .css

This works fine for most code files that can be viewed in the browser. For instance, when viewing sendcmd.js in Chrome, the “–” character in “2014–2021” is correctly displayed: sendcmd.js correct display

But the UTF-8 directive doesn't seem to work for .java files. For instance, in TrackChain.java, Chrome displays the UTF-8 bytes of the “–” character as three characters: enter image description here

Any ideas why, or what can be done about it?

Safari has the same problem as Chrome (while Firefox doesn't want to show Java files at all, but prompts me for a downloading destination).

(Bonus question: Is there any way to set the charset for files that don't contain a ‘.’, like Makefile?)

UPDATE: The following worked for viewing Java and and Makefile in UTF-8:

<FilesMatch "\.java$">
     Header set Content-Type "text/x-java-source; charset=UTF-8"
</FilesMatch>
<FilesMatch "^[Mm]akefile$">
     Header set Content-Type "text/plain; charset=UTF-8"
</FilesMatch>

Why the AddCharset UTF-8 .java line has no effect is however still a mystery to me.



Solution 1:[1]

As per the documentation AddDefaultCharset affects all files of MIME type text/plain or text/html (which is also pointed out in a comment to htaccess UTF-8 encoding for .html, .css, .js - Whats the best way?). So your definition to .html is redundant (unless they're in fact XHTML and served as such).

AddCharset is part of mod_mime, so you have to make sure this module is available. As per documentation you can define multiple extensions with one line instead of using one line for each.

  • Requesting your JAVA example however only brings back this header, without any hint of the text encoding - unsurprisingly then the client either guesses it correctly or not:

    content-type: text/x-java-source

    So you have only assumed what is served, effectively skipping the possibility that you could still be part of the problem, and instead accused the clients for their behaviour.

  • Requesting your JS example brings back a header as expected:

    content-type: application/javascript; charset=UTF-8

So you have to find out why JAVA files are not served with any charset definition. See all answers in How to change the default encoding to UTF-8 for Apache to try a few workarounds and use your browser to check if the response headers actually contain what you want.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1