Java:为Windows安装新字体 [英] Java: Installing new fonts for Windows

查看:246
本文介绍了Java:为Windows安装新字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里遇到问题,我们有一个应用程序,要求在Windows中安装特定的字体,在某些情况下,客户会丢失它们. 因此,决定将它们与应用程序捆绑在一起,并在缺少它们时进行安装. 到目前为止,我正在测试可运行的jar,其中我将所有必需的字体(* .ttf)包括在内作为资源,并尝试从代码中安装它们.

I got a problem here, we have an application which require specific fonts to be installed in Windows and in some cases customers are missing them.

So it was decided to bundle them together with application and install if they are missing. So far im testing runnable jar where I include all required fonts (*.ttf) as resources and trying to install them from code

例如,在这里我正在检查所需的字体是否丢失:

for example here i'm checking if required font is missing:

GraphicsEnvironment gr = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] allSystemFonts = gr.getAvailableFontFamilyNames();
if (!Arrays.asList(allSystemFonts).contains("YourMissingFontName"))
{
    System.out.println("Fonts are missing, installing...");
    installMissingFonts(getLocalFonts());
}

在函数"getLocalFonts"中,我从嵌入式jar资源中获取字体作为文件数组

in function "getLocalFonts" im getting fonts from embedded jar resources as array of files

File localFontFolder = new File(
     this.getClass().getClassLoader().getResource("Fonts/")
    .getFile());
return localFontFolder.listFiles();

并使用"installMissingFonts"功能中的"registerFont"在系统中注册它们

And registering them in the System with "registerFont" in "installMissingFonts" function

for (File file : allFonts) {
    try{
        Font fontFile = Font.createFont(Font.TRUETYPE_FONT, file);
        gr.registerFont(fontFile);
        System.out.println("Font [" + fontFile.getFontName() +"] was registred successfully");
    }
    catch(Exception e)
    {
        System.out.println("Failed register font from ["+file+"]: "+e);
    }
}

在运行时间内可以很好地运行,并且如果我在注册完成后再次致电检查,它不会对我说字体丢失了我真正需要的内容.

但是,它没有将它们安装到C:\ Windows \ Fonts文件夹中,并且午餐时间过后那些注册的字体将被丢弃.因此,每次我运行jar时,它将安装这些字体,但仅在应用程序在其自己的虚拟空间中存在的一段时间内安装这些字体. 在Win7上以管理员身份运行它没有帮助,它仍然只能在应用程序处于活动状态的时间内起作用

That works perfectly within a run time, and if i'll call check over again after registration was complete, it will not say to me that fonts are missing what is exactly what I need

However it is not installing them into C:\Windows\Fonts folder and those registered fonts are discarded after lunch time. So each time I would run the jar it will install those fonts but only for a duration of application live within its own virtual space. Running it as Admin on Win7 doesn't help, it still working only for a duration of application live

问题: 如果我需要将* .ttf文件作为资源嵌入到资源中,该如何从Java代码向系统(C:\ Windows \ Fonts)安装缺少的字体?

Question: How do I install missing fonts to the System (C:\Windows\Fonts) from Java code if I have required fonts as *.ttf files embedded as resource?

提前谢谢!

推荐答案

最好将Windows Installer(MSI)与您的应用捆绑在一起,而不是依靠Java代码来做到这一点.

Rather than rely on Java code to do this, it's better if you just bundle a Windows Installer (MSI) with your app.

您可以使用 WIX工具集来执行此操作.使用WIX创建最小的无UI安装程序(MSI文件),该安装程序只需使用 RegisterFonts 指令.或也可以安装该应用程序的完整安装程序.

You can use the WIX tool set to do this. Use WIX to create an minimal UI-less installer (MSI file) that just installs the fonts with the RegisterFonts directive. Or a full installer that installs the app as well.

如果Java应用程序具有任何类型的安装程序脚本,则可以让其调用MSI文件(Runtime.exec).否则,将MSI捆绑为资源,就像捆绑字体一样.在您的应用第一次运行或检测到缺少字体时,请它执行MSI文件.

If your Java application has any sort of installer script, you can have it invoke your MSI file (Runtime.exec). Otherwise, bundle the MSI as a resource like you are bundling the fonts. On the the first time your app runs or when it detects the fonts are missing, have it exec the MSI file.

理想情况下,您需要WIX构建一个完整的安装程序,将字体和Java代码一起安装,然后分发MSI(或setup.exe)供它们启动.

Ideally, you have WIX build a full installer that installs both the Fonts and Java code together and you distribute the MSI (or setup.exe) for them to launch.

这篇关于Java:为Windows安装新字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆