【问题标题】:how to run java program in both platforms?如何在两个平台上运行java程序?
【发布时间】:2013-08-06 14:13:52
【问题描述】:

我在eclipse中开发过软件,平台是ubuntu(linux)。我在程序中使用了古吉拉特语字体并完成了它,它在 ubuntu 中运行良好,但是在带有 JRE 的 Windows 7 中运行时它不能正常工作。文本在 ubuntu 中使用时无法正确显示。我该怎么办?

我也尝试过使用 java -Dfileencoding=utf-8 但它不能正常工作。当我在 Windows 中打开从 Ubuntu 中挑选的 java sorce 文件时,文本可以正常工作并显示。 所以请告诉我如何正确地完成这项工作。

【问题讨论】:

  • 为什么不使用普通字体?这不是标准的 Windows 字体,所以它当然不会正确呈现。
  • @JesusRamos 古吉拉特语是一种语言。你不知道他用的是什么字体。
  • @Sebastian 好吧,该语言必须支持字体,因为它看起来需要特殊字符。
  • 这看起来仍然像 utf-8 被误解了。您是否使用正确的编码(-encoding UTF-8)编译它们?默认情况下,在 ubuntu 上编译的二进制文件会得到正确的,这与在 windows 上编译的不同。
  • 我曾尝试在 ubuntu 中使用 utf-8 编译代码并使用 -Dfileencoding=utf-8 在 Windows 上运行,但它仍然无法正常工作。

标签: java swing


【解决方案1】:

如果您想在应用程序中使用自定义字体(并非在所有操作系统上都可用),您需要在 .jar 文件中包含字体文件(otf、ttf 等),您可以然后通过此处描述的方法在您的应用程序中使用字体:

http://download.oracle.com/javase/6/docs/api/java/awt/Font.html#createFont%28int,%20java.io.File%29

来自 (Here thanks to commenter) 的示例代码;

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("A.ttf")));

如果您不确定如何从 .jar 中提取文件,这是我之前在 SO 上分享过的一种方法;

/**
*  This method is responsible for extracting resource files from within the .jar to the temporary directory.
*  @param filePath The filepath relative to the 'Resources/' directory within the .jar from which to extract the file.
*  @return A file object to the extracted file
**/
public File extract(String filePath)
{
    try
    {
        File f = File.createTempFile(filePath, null);
        FileOutputStream resourceOS = new FileOutputStream(f);
        byte[] byteArray = new byte[1024];
        int i;
        InputStream classIS = getClass().getClassLoader().getResourceAsStream("Resources/"+filePath);
//While the input stream has bytes
        while ((i = classIS.read(byteArray)) > 0) 
        {
//Write the bytes to the output stream
            resourceOS.write(byteArray, 0, i);
        }
//Close streams to prevent errors
        classIS.close();
        resourceOS.close();
        return f;
    }
    catch (Exception e)
    {
        System.out.println("An error has occurred while extracting a resource. This may mean the program is missing functionality, please contact the developer.\nError Description:\n"+e.getMessage());
        return null;
    }
}

【讨论】:

【解决方案2】:

您需要将字体安装到 Windows 中 - 或更改应用程序上的字体。您可以通过将其包含在安装过程中来做到这一点。

【讨论】:

    猜你喜欢
    • 2015-02-24
    • 1970-01-01
    • 1970-01-01
    • 2013-07-15
    • 2019-11-07
    • 2021-04-01
    • 1970-01-01
    • 2021-03-02
    • 1970-01-01
    相关资源
    最近更新 更多