Definitely solve problems with strange characters in Java

Asked

Viewed 505 times

4

I had a problem and I solved it this way:

javac -encoding UTF-8 nomedoarquivo.java

As I can already leave configured this command for all files?

1 answer

4

Ideally you use a tool to compile your programs.

It can be an IDE like Eclipse or Netbeans, where you then configure the project as UTF-8 and can still set additional compiler parameters, but that depends on which IDE you use and which version.

Preferably a tool of build as Maven or Gradle helps define the IDE’s standalone project. In Maven, you can simply define the encoding in the project configuration in the file pom.xml. Example:

<project>
  ...
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  ...
</project>

If you really need to use command line, you can try to set the environment variable JAVA_TOOL_OPTIONS containing the parameters you want to be used in the command javac. The problem with this approach is that you affect the whole environment.

A simpler solution for a small personal project is to create a script (batch for Windows, bash for Linux or whatever you have available) that automatically runs the build process in your project.

And it is always good to remember that it is not only when compiling classes that you may have problems. Preferably, run the command java also passing the encoding parameter -Dfile.encoding=UTF-8 also through your IDE or command line, as you prefer.

Also remember that it is recommended to pass the encoding whenever converting bytes to Strings and vice versa, whenever read or write files, transmit data over the network, return data in requests and so on.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.