1

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?

enter image description here

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.

Natan Streppel
  • 5,759
  • 6
  • 35
  • 43
night bird
  • 67
  • 5
  • Why not use a normal font? That's not a standard Windows font so of course it won't render correctly. – Jesus Ramos Aug 05 '13 at 18:51
  • @JesusRamos Gujarati is a language. You don't know which font he uses. – Sebastian Aug 05 '13 at 18:54
  • @Sebastian Well there has to be font support for the language since from what it looks like requires special characters. – Jesus Ramos Aug 05 '13 at 18:57
  • That still looks like utf-8 misinterpreted. Did you **compile** them using the right encoding (-encoding UTF-8)? A binary compiled on ubuntu would get the correct one by default, unlike one compiled on windows. – kiheru Aug 05 '13 at 19:12
  • i had tried using compiling code with utf-8 in ubuntu and run on windows using -Dfileencoding=utf-8 but it still wont work. – night bird Aug 06 '13 at 18:11

2 Answers2

6

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;
    }
}
Community
  • 1
  • 1
Robadob
  • 5,319
  • 2
  • 23
  • 32
  • 4
    +1, simllar question/answers here, code sample: http://stackoverflow.com/questions/5652344/how-can-i-use-a-custom-font-in-java – Frederik.L Aug 05 '13 at 18:57
0

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.

Vaughan Hilts
  • 2,839
  • 1
  • 20
  • 39