- Using your package manager install the "ttf-mscorefonts-installer" package.
- Open this file in a text editor, "/etc/fonts/conf.avail/60-latin.conf". Add the "Times New Roman" font family to the top of the list for the "serif" fonts. Add the "Arial" family to the top of the list for the "sans-serif" fonts. Add "Courier New" to the top of the list for the "monospace" fonts. Or use my version of the file: 60-latin.conf. Save and close the file.
- Run this command to update fonts: "sudo dpkg-reconfigure fontconfig"
- The new fonts will not be used if an operating system specific Java fontconfig property file exists that matches the operating system being used. If such a file exists it must be removed. Here is an example of removing such a file: "sudo rm /usr/lib/jvm/java-7-oracle/jre/lib/fontconfig.Ubuntu.properties".
- That is it, the next time you launch a Java application the Windows fonts will be used.
Java does a pretty good job at making applications look the same on Windows and Linux. But the default fonts that Java uses on Linux are not the same fonts used for Windows. This causes a problem for implementing GUI projects on Linux that will be used on Windows. The default font used on Linux can have different dimensions than the fonts used on Windows causing differences in the flow of text and messing up components in windows. Here is an example:
The solution is to install and use Microsoft's free "TrueType core fonts for the Web" on Linux and configure Java to use those fonts.
You might wonder if it is legal to install these Microsoft fonts on a Linux computer. The answer is yes it is legal. And you don't have to own any copies of Windows. The license for these fonts is short and easy to read. You can read it here: https://www.microsoft.com/typography/fontpack/eula.htm The key parts of the license are, "The SOFTWARE PRODUCT is licensed, not sold." and "You may install and use an unlimited number of copies of the SOFTWARE PRODUCT."
Here are instructions on how to install and configure Java to use these fonts on Ubuntu Linux. These instructions work for Oracle Java 7, Oracle Java 8 and OpenJDK 7.
Thanks to Kyle Chase and pphillips for providing information about how to do this on the IA Forum topic Java Fonts and Linux.
I would love to hear your experiences about installing Windows fonts in Java on Linux. Leave a comment about your experience or about this article.
Did you find this blog post useful? Sign up for my mailing list to get notified of future blog posts.