繁体   English   中英

如何在两个平台上运行Java程序?

[英]how to run java program in both platforms?

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

在此处输入图片说明

我也尝试使用java -Dfileencoding = utf-8,但无法正常工作。 当我在Windows中打开从Ubuntu中选取的java sorce文件时,文本可以正常显示。 因此,请告诉我正确执行此操作的方法。

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

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

的示例代码( 感谢评论者 );

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;
    }
}

您需要将字体安装到Windows中-或在应用程序中更改字体。 您可以通过将其包含在安装过程中来实现。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM