This week's book giveaway is in the Agile forum.
We're giving away four copies of Head First Agile and have Andrew Stellman & Jennifer Greene on-line!
See this thread for details.
Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting custom fonts to work in a runnable JAR  RSS feed

 
Joel Blake
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to incorporate a custom .ttf font into a runnable JAR. I am using the following code to add the font:



I know that to have the font work in a JAR I have to make it a resource, but I'm a little unclear what the difference between a regular file and a resource are however. Either way, the above code is giving me this error:



This makes me think that it's not finding the .ttf file. I have it located in the project folder, alongside the src folder. As you can see I am currently using a relative path, but have also tried it using an absolute path, with the same error being generated. Can anybody give me a hand as to where I'm going wrong? Thanks.
 
Paul Clapham
Sheriff
Posts: 22531
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joel, welcome to the Ranch!

You started out asking about an executable jar, and your question is clearly about that, but then you tell us where the .ttf file is in your IDE. It would be more useful to know where in the executable jar it's located -- perhaps it isn't there at all?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Besides checking the existence/location suggested by Paul, you might also want to try renaming the file to take out white spaces.
 
Paul Clapham
Sheriff
Posts: 22531
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A good point. Since it's supposed to be a URL and not a file path you might also consider URL-escaping it:

 
Joel Blake
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the answers guys. With regard to Paul's JAR vs IDE question, I should have given a better lead in. Originally I had added the font using the following code:



Which worked perfectly as long as I was launching it from Eclipse. As soon as I exported it to a runnable JAR it would stop using the new font. I then checked around online and found a few posts that pointed me towards the getResourceAsStream() method that I initially put into my thread, but that gave the errors I noted in my first post. On another note, I also tried using no spaces, and escape characters, no luck... Sorry for the confusion.
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you are new (I hope tt isn't too late to welcome you here ), I have sorted out the indentation in your code.
There should be no difference between code which “works” on Eclipse and code which “works” on anything else. It may have something to do with the location of that .ttf file. If you run your app from a different location the PATH to that file will be different and you may be unable to find it. Did you put the file in the Workspace folder or similar?
Another thing about .ttf files: are they Windows®‑specific? If so, you may be losing portability by tying yourself to one platform.
 
Paul Clapham
Sheriff
Posts: 22531
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel Blake wrote:Which worked perfectly as long as I was launching it from Eclipse. As soon as I exported it to a runnable JAR it would stop using the new font. I then checked around online and found a few posts that pointed me towards the getResourceAsStream() method that I initially put into my thread, but that gave the errors I noted in my first post. On another note, I also tried using no spaces, and escape characters, no luck... Sorry for the confusion.


A "resource" is something which looks like a file, but could also be a jar file entry and occasionally other things. Your code is looking for it in the classpath, which in an executable jar is normally the jar itself. That's why I asked whether the font was in the jar. I don't know if you've looked yet, but you haven't said so.
 
Joel Blake
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So when I look at the .classpath file it looks like this:



I'm not sure exactly how the .classpath file works, but I don't see the font or the JAR in there. I did read up on this a bit though, and by calling the getResourceAsStream() method, it should be added to the .classpath automatically. Is this wrong?
 
Paul Clapham
Sheriff
Posts: 22531
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know where you got that file from. But it doesn't matter because when you run an executable jar the classpath is the executable jar itself (and also anything referred to in its manifest, which I'm guessing is empty in your case). That's why I asked you (twice now) to look in the jar to see if your font file is there.
 
Joel Blake
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So that file came from the project folder. So in extracting the runnable JAR, it shows me 5 .class files for Main (Which is the name of my class) they are:



I apologize if I'm struggling with this, but this is the first time I've ever gotten into classpaths. In looking at Main.class it looks like it's just my source code with a bunch of markup tags. I could be misinterpreting it though. Where would I see the font file if it was there?
 
Joel Blake
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I also tried putting the .ttf file into the src folder and changing the code to ...getResourceAsStream("src/neropol x rg.ttf")... but it doesn't find it like that either (in Eclipse or the JAR).
 
Paul Clapham
Sheriff
Posts: 22531
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No need to apologize. Using an IDE like Eclipse is great for getting a bunch of code written, but it's woefully bad at preparing people for the real world beyond a development environment.

Anyway you need to put the font file in the right place in your project so that when you ask Eclipse to create the jar (I assume it's Eclipse you're using?) it will include the font file along with the Java class files. But I don't know where that would be; I would have tried putting it under the src folder too (that used to work in Eclipse for me) but apparently that isn't right.

I'll copy this post over to our IDEs forum, maybe somebody there will have an idea.
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try putting the resource one level up from the .src folder.
 
Joel Blake
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like in the actual project folder? That's where I've been doing it from, I only tlried moving it into the src folder to see what would happen.
 
Campbell Ritchie
Marshal
Posts: 55799
164
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is what I had thought, but I was obviously mistaken.
 
Joel Blake
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So going back to the classpath angle, where would I see the JAR within the class path file? And which of the 5 I mentioned above would I look in?
 
Paul Clapham
Sheriff
Posts: 22531
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel Blake wrote:So going back to the classpath angle, where would I see the JAR within the class path file? And which of the 5 I mentioned above would I look in?


I don't understand that. Are you abandoning the idea of using an executable jar, then? And what's the "class path file" you asked about?

As for "the 5 you mentioned" I can only guess that you're referring to the five classes which were put into the jar file. Is that right? If so, you wouldn't "look in" any of them. What you need to see is the five classes AND the font file.

It occurred to me earlier today that you might not be generating the executable jar correctly. I don't remember what it looks like to generate a jar in Eclipse, and maybe there's more than one way to do it, but if you're telling it to only include "*.class" files then that would guarantee the font file wouldn't be put into the jar.
 
Joel Blake
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah ok, I was misusing the word classpath, but I think I'm following now. No the font file was not inside the JAR file. And yes, when I referred to "the 5 I mentioned before" I was talking about:



As far as generating the JAR, Eclipse gives three options:

Extract required libraries in generated JAR
Package required libraries in generated JAR
Copy required files into a sub-folder alongside the generated JAR

But I'm not using any external libraries so I don't think that applies, unless I'm wrong. I'm almost positive the error is in the code and not in JAR generation, as I'm getting the error I mentioned in my opening post...
 
Paul Clapham
Sheriff
Posts: 22531
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like I said... if the font file isn't in the jar then you're going to get that error. And the font file isn't in the jar, so you get that error. It's not your code (yet), it's the fact that you haven't built the jar correctly.
 
Joel Blake
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I'm getting the error when I run it from Eclipse, before I even try to export the JAR.
 
Paul Clapham
Sheriff
Posts: 22531
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just opened up Eclipse and made a runnable jar from an old project (dating from 2010). It has a /src folder with a couple of classes in a package and a file called Elves.txt. Then I went through the File-Export process to a runnable jar the same as you did. There were no external libraries needed in my code either, so that selection didn't matter. The resulting jar file had a folder for the package containing the classes, a META-INF folder for the jar's manifest, and the Elves.txt file. So that was what I expected.

I looked at the /bin folder with the Windows tool and noticed that Elves.txt was there, so I deleted it from that folder to see what would happen. Then I redid the File-Export process and generated another runnable jar, and that produced exactly the same result as the first try. It contained the Elves.txt file even though there wasn't a copy of that file in the /bin folder any more. So I guess the 2010 version of Eclipse duplicated project resources from /src into /bin to help out in the jar-making process but the 2016 version of Eclipse doesn't do it that way.

So yeah: project resources should go into the /src folder and it Just Works from there. At least Eclipse Mars does, for me.

 
Paul Clapham
Sheriff
Posts: 22531
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A line of code from my working application:


 
Joel Blake
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That did it, but it's weird. So by removing the getClassLaoder() method, it worked. But it's strange because I have the .ttf file in the src folder, but my path is only "/neuropol x rg.ttf". If I change the path to "/src/neuropol x rg.ttf", as you would think you should be, it's doesn't work. I'm super glad it's working, and thank you all for that, but I'm a little curious how the path is working...
 
Paul Clapham
Sheriff
Posts: 22531
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never understood the documentation for how the class loader was supposed to find resources. The Class.getResource method has a little better documentation but it's still incomprehensible until somebody explains it to you.

And the /src folder is a thing which Eclipse uses to keep source code separate from compiled code. It's not part of any external environment so you shouldn't expect to find references to it anywhere outside Eclipse.
 
Joel Blake
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But src is still a valid folder within the Windows file system, so shouldn't the relative path have to reflect that?
 
Paul Clapham
Sheriff
Posts: 22531
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why stop at the src folder then? It's buried six levels deep in other folders, so why wouldn't you have to include all of them?

And anyway the src folder is Eclipse's place to store your source code. The compiled classes, which are the ones which you use so they are actually the ones found in the classpath, are in a folder called bin which Eclipse hides from you. So it should really be /bin/neuropol and so on, right?

No, none of that stuff is right. Eclipse's classpath root for your project is actually the bin folder, so your URL to get the font file should be evaluated starting there. Not starting at the project folder. And in the runnable jar, you'll notice, there's no /src or /bin folders at its root either.
 
Joel Blake
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. Well thanks for all the help, I really appreciate it!
 
Randall Twede
Ranch Hand
Posts: 4690
8
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I only read the first half of this, but my suggestion is do like me. jar it from the command line. don't use an ide at all. use notepad++
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!