You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何加载iText 7试用许可证文件?路径配置及报错解决

解决iText 7许可证文件加载问题

许可证文件放置位置

可以放在类路径下,也可以放在任意磁盘路径:

  • Maven/Gradle项目:直接放到src/main/resources目录,编译后会自动进入类路径
  • 普通Java项目:放到项目的src目录,或者在IDE里把目标文件夹设置为资源根目录

获取路径并加载的正确方法

方式一:从类路径加载(推荐)

不用硬写路径,通过类加载器获取资源,适配不同运行环境:

// 用输入流加载(更可靠)
InputStream licenseStream = getClass().getClassLoader().getResourceAsStream("itextkey.json");
LicenseKey.loadLicenseFile(licenseStream);

如果一定要用File对象:

URL licenseUrl = getClass().getClassLoader().getResource("itextkey.json");
if (licenseUrl != null) {
    File licenseFile = new File(licenseUrl.getPath());
    LicenseKey.loadLicenseFile(licenseFile);
}

方式二:从绝对路径加载

直接写文件的完整路径,比如:

  • Windows:new File("C:/your-path/itextkey.json")
  • Linux/macOS:new File("/home/your-path/itextkey.json")

解决License file can not be null报错

这个错误说明代码没找到目标文件,检查以下几点:

  • 文件名是否正确,注意Linux/macOS系统区分大小写
  • 用类路径加载时,确认编译后文件出现在target/classes(Maven)或IDE的输出目录里
  • 用相对路径时,路径是相对于项目运行的工作目录,不是源码目录,别搞混了

内容的提问来源于stack exchange,提问作者zyngot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 03:55:11