简体   繁体   English

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

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

I had developed software in eclipse and platform was ubuntu (linux). 我已经在eclipse中开发了软件,平台是ubuntu(linux)。 I used gujarati fonts in program and completed it and it runs wery well in ubuntu but while running in windows 7 with JRE it doesnt work properly. 我在程序中使用了古吉拉特语字体并完成了它,它在ubuntu中运行良好,但是在Windows 7中使用JRE运行时却无法正常工作。 Text won't show properly as it used in ubuntu. 文本无法正确显示,如在ubuntu中使用的那样。 What shoud I do? 我该怎么办?

在此处输入图片说明

I have tried using java -Dfileencoding=utf-8 also but it wont worked properly. 我也尝试使用java -Dfileencoding = utf-8,但无法正常工作。 While I open java sorce file picked from Ubuntu in Windows the text works and shows properly. 当我在Windows中打开从Ubuntu中选取的java sorce文件时,文本可以正常显示。 So please tell me the way to do work this properly. 因此,请告诉我正确执行此操作的方法。

If you want to use a custom font in your application (that isn't available on all operating systems), you will need to include the font file (otf, ttf, etc.) in your .jar file, you can then use the font in your application via the method described here: 如果要在应用程序中使用自定义字体(并非在所有操作系统上都可用),则需要在.jar文件中包含字体文件(otf,ttf等),然后可以使用通过此处描述的方法在应用程序中添加字体:

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

Sample code from ( Here thanks to commenter ); 的示例代码( 感谢评论者 );

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

If your unsure on how to extract your file from a .jar, here's a method I've shared on SO before; 如果您不确定如何从.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;
    }
}

You'll need to install the font into Windows - or change the font on your application. 您需要将字体安装到Windows中-或在应用程序中更改字体。 You can do this by including it in your install process. 您可以通过将其包含在安装过程中来实现。

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

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