Java Graphics drawString部署Solaris服务器时字体异常求助
Hey there! Your guess is totally right—this issue is almost certainly caused by missing fonts on your Solaris 5.10 server. Let's break down why this happens and how to fix it:
Why it works on Windows but fails on Solaris
Windows comes pre-installed with the Tahoma font, and it has solid support for Unicode characters like the Vietnamese diacritics in your strings (Đà Nẵng). Solaris 5.10, however, has a much more limited default font library—it likely doesn't include Tahoma, and may lack fonts that properly render those special Unicode characters. When Java can't find the specified font, it falls back to a default font that might not support the characters you're trying to draw, leading to rendering failures (like missing characters or garbled text).
Steps to fix the problem
1. Verify available fonts on Solaris
First, confirm which fonts are actually available on your server. You can do this two ways:
- From the command line: If
fontconfigis installed, runfc-listto list all system fonts. Look for "Tahoma" or fonts that support Vietnamese (like "Arial Unicode MS" or open-source options like "Noto Sans"). - From Java code: Add a quick snippet to print available font families when your app runs:
Run this on Solaris to see if your target font exists.import java.awt.GraphicsEnvironment; public class FontChecker { public static void main(String[] args) { String[] fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); for (String font : fonts) { System.out.println(font); } } }
2. Install the required font
If Tahoma is missing, you have two options:
- Use Tahoma (note: copyright considerations): Copy the Tahoma font files (
tahoma.ttf,tahomabd.ttf) from a Windows system to a font directory on Solaris (e.g.,/usr/share/fonts/TrueType/). Then update the font cache withfc-cache -fv(if using fontconfig) and restart your Java application so it picks up the new font. - Use an open-source alternative (recommended): Avoid copyright issues by using a free, multi-language font like Noto Sans (supports Vietnamese and most other scripts). Download the font files, place them in the Solaris font directory, and update the cache as above.
3. Make your code font-resilient
To prevent future issues, modify your code to fall back to a valid font if Tahoma isn't available:
import java.awt.Font; import java.awt.GraphicsEnvironment; import java.util.Arrays; // ... String preferredFont = "Tahoma"; GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); String[] availableFonts = ge.getAvailableFontFamilyNames(); // Check if preferred font exists, else pick a fallback if (!Arrays.asList(availableFonts).contains(preferredFont)) { // Use a fallback that supports Unicode/Vietnamese preferredFont = "Noto Sans"; // Or try "Arial Unicode MS" if it's available } g.setFont(new Font(preferredFont, Font.PLAIN, 24));
4. Ensure proper Java encoding
Add the JVM argument -Dfile.encoding=UTF-8 when starting your application to make sure Java handles Unicode characters correctly throughout the process.
Bonus check: Java version compatibility
Solaris 5.10 is an older OS—make sure your Java version is recent enough (Java 6 or later) to support modern Unicode rendering. Older Java versions might have issues with certain diacritic characters.
内容的提问来源于stack exchange,提问作者LamLe

