簡體   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