I would like to install a set of fonts to the windows system from my java class. I am using these font for my Birt Report.
3 Answers
You can write a batch/powershell script and include it, along with the font files in your application. Then you can execute the script with
Runtime.getRuntime().exec(...)
You'll most likely have to raise the privileges for your application once you run it.
As for passing the password. It's possible to run cmd.exe
so it pops up and propts the user for it. You can also try assigning the return value of exec to a Process
class object, which has InputStream
and OutputStream
properties. I'm not sure how to do it properly. I did it once in a project, a couple of years ago but I no longer have the code.
If you only have to install the fonts once, consider creating an installer for your java application that will take care of it. There's a neat installer generator called IzPack, which allows you to create complex installers using XML. It also allows you to raise privileges for executables run during the installation. This is the way I do such stuff.

- 17,895
- 9
- 86
- 131
-
But that batch file has to be executed as run as administrator. How can I do this using Runtime.getRuntime.exec(...) – Muzy May 29 '12 at 10:38
-
@user1423344 this will be a good place to start: http://stackoverflow.com/questions/1385866/java-run-as-administrator First thing I'd try would be to execute the script using the runas command http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/runas.mspx?mfr=true – toniedzwiedz May 29 '12 at 10:40
-
If we are using runas then on the next line its asking for password for the administrator. how could we pass this form java – Muzy May 29 '12 at 11:01
-
@user1423344 would it be acceptable for the application to open a console and prompt the user for password? Or does it have to be fully automatic? Do you want to install the fonts once, while setting up the program or each time you execute it? – toniedzwiedz May 29 '12 at 11:19
-
It has to be automatic. On every execution I have to check whether the font is installed or not. If it is not Installed then it has to be installed automatically. – Muzy May 29 '12 at 13:29
-
@user1423344 then try passing the password to a stream returned by the process object created on execution of your command, like I wrote in the edited answer. If it doesn't work, I'm out of ideas. – toniedzwiedz May 29 '12 at 13:46
You can install those fonts user System -> Fonts if you are in window to test it out. If you are trying to arrange it with your program, you must start by including them in your java resource file in order to refer to it later on.
Hope it helps~

- 2,069
- 9
- 38
- 60
Without local admin rights it is possible to add custom fonts to the font cache. Then your custom fonts will be available to all your applications until you log out. The Windows API that does that is AddFontResource. Via a JNI helper DLL you can call it directly, or just execute the RegisterFonts utility.

- 80,671
- 25
- 200
- 267