简体   繁体   中英

how to run java program in both platforms?

I had developed software in eclipse and platform was 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. Text won't show properly as it used in ubuntu. What shoud I do?

在此处输入图片说明

I have tried using java -Dfileencoding=utf-8 also but it wont worked properly. While I open java sorce file picked from Ubuntu in Windows the text works and shows properly. 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:

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;

/**
*  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. You can do this by including it in your install process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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