Using Native2Ascii class in Java Code

The Java compiler and other Java tools can only process files which contain Latin-1 and/or Unicode-encoded (\udddd notation) characters.

If you want to encode in escaped \u Unicode a Japanese string for instance:

問題です

Save this string into source.txt with UTF-8 encoding.

We can simple encode it using native2ascii java tool.

C:\>native2ascii -encoding utf8 c:\source.txt c:\output.txt

But if you need to do it from your Java app, you can still use

sun.tools.native2ascii

that is embedded in the JDK tools.jar.

Java Conversion

Lets include that Jar in to your gradle dependency list:

import javax.tools.ToolProvider
dependencies {
  ...
    compile files(((URLClassLoader) ToolProvider.getSystemToolClassLoader()).getURLs())
}

(Check this Stackoverflow topic)

After that lets use Native2Ascii to Encode the file:

File destFile = new File("C:\output.txt");
File source = new File("C:\source.txt");

sun.tools.native2ascii.Main native2ascii = new sun.tools.native2ascii.Main();
native2ascii.convert(new String[] {"-encoding","UTF-8", source.getAbsolutePath(), destFile.getAbsolutePath() });

Your file will contain:

\u554f\u984c\u3067\u3059

This study was interesting for a Automated Localization Process. Especially important for the conversion from Japanese language to an ISO8859-1 character encoding as required by Properties class.


Warning: Native2Ascii in Java 1.8.0_60 is affected by bug.

Input stream file is not closed. On windows that file remains blocked for all the time of Thread execution.

Leave a Reply