Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 23 of 23

Thread: Problem loading TrueType Fonts

  1. #1
    Junior Member
    Join Date
    Jun 2017
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Problem loading TrueType Fonts

    Hello everyone,
    I just wanted to change the font-family of a String drawed in the Graphics, and thus included in the contructor of my class:
    InputStream inputStream= this.getClass().getResourceAsStream("/fonts/myFont.ttf");
    myFont= Font.createFont(Font.TRUETYPE_FONT, inputStream);
    inputStream.close();
    where myFont is a Font.

    The font located in '\build\classes\fonts\' exists and works fine in another programm (like MS Word). But even if I have integrated my few codelines, the font remains the same, when I run the project in Netbeans. The font is beeing set with
    g.setFont(new Font("myFont", Font.PLAIN, 12));
    where g is my Graphics
    Since I wrote the code-lines in question, when I build the programm as runtime, absolutely nothing works anymore, the window remains empty (even if in NetBeans it still works, just without the usage of the new font). What could be the problem, what is my mistake? There are no error-messages coming from the compiler!

  2. #2
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Problem loading TrueType Fonts

    What happens if you use the myFont variable in the setFont method call instead of creating a new instance of Font?

  3. #3
    Junior Member
    Join Date
    Jun 2017
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem loading TrueType Fonts

    Of course you are right! Now I changed the code to
    g.setFont(myFont.deriveFont(18.0f));
    Now it works when I run it in NetBeans. But the problem still exists, when I build it and execute it as runtime. Why that? The prompt says "java.io.IOException: Problem reading font data." But actually I copied it into the folder '/src/fonts', where it should be. Furthermore, I controlled the jar-file using 7-Zip. The folder and the therein contained font-file have been packed into it! But why that error?
    Last edited by Mythano; June 15th, 2017 at 10:58 AM.

  4. #4
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Problem loading TrueType Fonts

    What is the path in the jar file to the font file? Make sure the program is using the correct path.

  5. #5
    Junior Member
    Join Date
    Jun 2017
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem loading TrueType Fonts

    Well, it remains the same: "fonts/myFont.ttf" (I've added a screenshot showing it). And in the code it is called by
    InputStream inputStream= this.getClass().getResourceAsStream("/fonts/myFont.ttf");
    location.jpg

  6. #6
    Junior Member
    Join Date
    Jun 2017
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem loading TrueType Fonts

    But indeed what's strange is, that if I call it in NetBeans after having add this
    System.out.println(inputStream);
    to the code, so I will get
    java.io.BufferedInputStream@6bf7606b
    But when I execute the jar-file, so it is
    null
    Why that, what's wrong with my code?

  7. #7
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Problem loading TrueType Fonts

    Why that, what's wrong with my code?
    The path used when the code is executed in the jar is wrong.

  8. #8
    Junior Member
    Join Date
    Jun 2017
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem loading TrueType Fonts

    Yes, of course that, but it is still the same command the programm uses, isn't it? I mean, in NetBeans it works very well, just the jar does not work. And with this.getClass(), shouldn't it search the path in itself (so in the jar-container)? What should I use as path, to solve the problem?

  9. #9
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Problem loading TrueType Fonts

    Do some experimenting to see what folder the program is looking in when it is in the jar file.
    Print out the value returned by calling getResource(<PATHHERE>). Copy the file to different locations until you get a good path.

    What is the path in the jar file?
    What is the path in the getResource method?

  10. #10
    Junior Member
    Join Date
    Jun 2017
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem loading TrueType Fonts

    Okay. My InputStream is obviously null in the jar (only in the jar). When I try to catch the path using what Google told me for that:
    File jarDir = new File(ClassLoader.getSystemClassLoader().getResource(".").getPath());
    System.out.println(jarDir.getAbsolutePath());
    So, the programm executed in NetBeans will print "C:\java\build\classes". But the jar returns a Null-Pointer Exception!
    What matters getRessource(), it returned "file:/C:/java/build/classes/" for the case when executed in NetBeans, but for the jar "null"! I don't understand why there exists that difference, but it is what I observe. Do you have any further idea? Or should I test something else?

  11. #11
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Problem loading TrueType Fonts

    Try copying the file to different folders until getResource finds it.
    Also change the path in getResource until it finds the file.

  12. #12
    Junior Member
    Join Date
    Jun 2017
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem loading TrueType Fonts

    I don't see what the location of the jar itself can change much, even so I did what you proposed. I actually tested the jar-file now from four different locations, including one form my USB-Device; result is always "null. But maybe I also misunderstood you. This is the code I tested it with:
    URL add = this.getClass().getResource("");
            System.out.println(String.valueOf(add));
    When I change the path in getRessource (so "" to "fonts/myFont.ttf", or whatever), of course it just adds that to the path. At least in NetBeans it does, the jar just says "null". It just drives me crazy, I cannot understand the problem.

  13. #13
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Problem loading TrueType Fonts

    The path in the getResouce must point to a file.
    Move the file to the top level and use only the filename in the call to getResource.

    Here's what I got with a simple test:
    This line in main of TestCode23.java:
    System.out.println((new TestCode23()).getClass().getResource("/TestCode23.class"));
    When executed outside of the jar file:
    // file:/D:/JavaDevelopment/Testing/ForumQuestions12/TestCode23.class

    Following from command prompt
    // D:\BatchFiles>java.exe -jar _Testing.jar
    // jar:file:/D:/JavaDevelopment/Testing/ForumQuestions12/_Testing.jar!/TestCode23.class

  14. #14
    Junior Member
    Join Date
    Jun 2017
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem loading TrueType Fonts

    Okay. Either I am a stupid fool, or my Java installation is crap. I reprodused exactly the same what you did; please look at the Screenshot, isn't it the same? testcode.jpg
    But it still returns "null", I don't get it. What's on today, I really begin to cast doubt on my mind... I am using Windows 7 Professional SP1 and Java 8, Update 121. Do you have any idea, what I do wrong all the time?

  15. #15
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Problem loading TrueType Fonts

    What is the path to the TestCode23.class file in the jar file?

    What is printed when you execute the TestCode23 program outside of the jar file?

  16. #16
    Junior Member
    Join Date
    Jun 2017
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem loading TrueType Fonts

    You are right, it isn't "TestCode23.jar\testcode23.class", but it is "TestCode23.jar\testcode23\TestCode23.class". So I changed the code to:
    public class TestCode23 {
        public static void main(String[] args) {
            System.out.println((new TestCode23()).getClass().getResource("/testcode23/TestCode23.class")); 
        }
    }
    But when I run it, it still returns "null". Always "null", I can do what I want, nothing else than "null". How is that possible? Is there a mistake in that line of code?

  17. #17
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Problem loading TrueType Fonts

    Try it without the package. My test was without a package.

  18. #18
    Junior Member
    Join Date
    Jun 2017
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem loading TrueType Fonts

    Yes, thanks, now it works. But I don't understand why? What was now the problem, or differently asked, how can I apply this success to my real project, where the jar still doesn't work. May it change something, if I copy the font-file into the supreme directory, so not at "/fonts/myFont.ttf" but under "myFont.ttf" ?

  19. #19
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Problem loading TrueType Fonts

    now it works
    Ok, now experiment with putting the file to be read in different folders and changing the path in the getResource method until you see what the pattern is.

  20. The Following User Says Thank You to NormR For This Useful Post:

    Mythano (June 15th, 2017)

  21. #20
    Junior Member
    Join Date
    Jun 2017
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem loading TrueType Fonts

    Firsty located under "G:\JavaApplication11\dist" - Java returned: "jar:file:/G:\JavaApplication11/dist/JavaApplication11.jar!/TestCode23.class"
    Then located at "G:\asd\JavaApplication11\dist" - Java returned: "jar:file:/G:\asd\JavaApplication11/dist/JavaApplication11.jar!/TestCode23.class"
    At last I changed the relative input inside of the getRessource form getResource("/TestCode23.class") to getResource("/TestCode23/TestCode23.class") - Java returned "null"

    I cannot find something unusual, since at the location of the last one does not exist a file. But in my real project, THERE IS a file under "/fonts/myFont.ttf", and that's also what is standing in the brackets there...

    I could continue changing the pathes in and of the testfile, but I don't see something interesting that changes. Do you?

  22. #21
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Problem loading TrueType Fonts

    At last I changed the relative input inside of the getRessource
    form getResource("/TestCode23.class")
    to getResource("/TestCode23/TestCode23.class")
    - Java returned "null"
    Was the TestCode23 class in the TestCode23 package
    and the TestCode23.class file in the TestCode23 folder in the jar file?

  23. #22
    Junior Member
    Join Date
    Jun 2017
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem loading TrueType Fonts

    Oh no, I was right, I AM a stupid fool! I just checked every single possible problem, that could cause this unnormal effect in my real project. Because in fact, in the testfiles, most of the time, when the NetBeans-version worked, the JAR-file did too, and vice versa. So I checked it again, and looked out for every single detail. I realised, that the font-file in question isn't named "myFile.ttf" but "myFile.TTF". And since Windows is not case-sensitive, the code executed in NetBeans worked fine. But when Java created it's runtime and executed that one, it seems to make a difference between "ttf" amd "TTF". Oh my god, what I failed today is unbearable.
    Thank you so much for your untiring help and please excuse me my intolerable incompetence. Of course your systematic way of searching a mistake is best. Maybe I would have never looked that detailed without your help. Tank you, thank you, thank you!

  24. #23
    Member
    Join Date
    May 2017
    Location
    Eastern Florida
    Posts
    68
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Problem loading TrueType Fonts

    Glad you figured it out. Computers are pretty stupid and will do exactly what you tell them without looking around to see if maybe the target is close by.

Similar Threads

  1. How to edit Fonts of Java app... !!!!
    By b.shek49794 in forum Java ME (Mobile Edition)
    Replies: 4
    Last Post: September 5th, 2013, 01:54 AM
  2. fonts in eclipse :/
    By MR bruto in forum Java IDEs
    Replies: 1
    Last Post: May 30th, 2013, 03:52 PM
  3. Combine fonts on JLabel
    By gammaman in forum AWT / Java Swing
    Replies: 1
    Last Post: May 9th, 2012, 03:05 PM
  4. Java fonts
    By Ludicrous in forum Java Theory & Questions
    Replies: 2
    Last Post: March 31st, 2012, 05:39 PM
  5. Click to start and drawString fonts
    By Campos in forum Java Applets
    Replies: 3
    Last Post: July 24th, 2009, 02:24 PM

Tags for this Thread