英文:
Lucee not finding XSSFWorkbook after cfspreadsheet extension installed
问题
我在尝试创建XSSFWorkbook
时遇到了错误。
代码
currentCharterTemplate = 'UnusedTicketsWorkbook.xlsx';
currentFilePath = getDirectoryFromPath(getCurrentTemplatePath());
javaFile = createObject('java', 'java.io.File').init(currentFilePath & currentCharterTemplate);
excelFile = createObject('java', 'java.io.FileInputStream').init(javaFile);
xssfWorkbook = createObject('java', 'org.apache.poi.xssf.usermodel.XSSFWorkbook');
xssfWorkbook = createObject('java', 'org.apache.poi.xssf.usermodel.XSSFWorkbook').init(excelFile);
我还尝试了只使用下面的一行代码,但是收到了相同的错误。
xssfWorkbook = createObject('java', 'org.apache.poi.xssf.usermodel.XSSFWorkbook');
错误信息
无法通过类的字符串名称加载类,因为找不到指定名称 [org.apache.poi.xssf.usermodel.XSSFWorkbook] 的类定义
(由于 java.lang.ClassNotFoundException:org.apache.poi.xssf.usermodel.XSSFWorkbook;
java.lang.ClassNotFoundException:org.apache.poi.xssf.usermodel.XSSFWorkbook 未在 lucee.core [46] 中找到;)
我已经安装了适用于 Lucee 的 cfspreadsheet 扩展,并重新启动了 Lucee 和我的计算机,我已经确认 `cfspreadsheet-3.0.1.jar` 与 Lucee 一起提供在 `C:\lucee\tomcat\lucee-server\bundles` 中,以及 Lucee 提供的所有其他 jar 文件。
如果我使用 7-zip 打开这个 jar 文件,我可以看到其中包括了 `poi-ooxml-3.15.jar`。在该 jar 文件中,我可以浏览到 `C:\lucee\tomcat\lucee-server\bundles\cfspreadsheet-3.0.1.jar\poi-ooxml-3.15.jar\org\apache\poi\xssf\usermodel\XSSFWorkbook.class`。
错误消息中的 `lucee.core` 让我认为它无法识别扩展中的 jar 文件,但是我找不到启用此功能的设置。
<details>
<summary>英文:</summary>
I'm receiving an error when trying to create an `XSSFWorkbook`
Code
currentCharterTemplate = 'UnusedTicketsWorkbook.xlsx';
currentFilePath = getDirectoryFromPath(getCurrentTemplatePath());
javaFile = createObject('java', 'java.io.File').init(currentFilePath & currentCharterTemplate);
excelFile = createObject('java', 'java.io.FileInputStream').init(javaFile);
xssfWorkbook = createObject('java', 'org.apache.poi.xssf.usermodel.XSSFWorkbook');
xssfWorkbook = createObject('java', 'org.apache.poi.xssf.usermodel.XSSFWorkbook').init(excelFile);
I also tried just the line below and I receive the same error.
xssfWorkbook = createObject('java', 'org.apache.poi.xssf.usermodel.XSSFWorkbook');
Error
cannot load class through its string name, because no definition for the class with the specified name
[org.apache.poi.xssf.usermodel.XSSFWorkbook] could be found caused by (java.lang.ClassNotFoundException:org.apache.poi.xssf.usermodel.XSSFWorkbook;
java.lang.ClassNotFoundException:org.apache.poi.xssf.usermodel.XSSFWorkbook not found by lucee.core [46];)
I've installed the cfspreadsheet extension for Lucee, and restarted Lucee and my computer, I've verified `cfspreadsheet-3.0.1.jar` is available in `C:\lucee\tomcat\lucee-server\bundles` along with all the other jar files included with lucee.
If I open the jar with 7-zip, I can see `poi-ooxml-3.15.jar` is included. Within that jar I can browse to `C:\lucee\tomcat\lucee-server\bundles\cfspreadsheet-3.0.1.jar\poi-ooxml-3.15.jar\org\apache\poi\xssf\usermodel\XSSFWorkbook.class`
The `lucee.core` in the error message makes me think it's not recognizing jars from my extensions, but I can't find a setting to enable this.
</details>
# 答案1
**得分**: 2
```js
// 强制 Lucee 激活 bundle
spreadsheet action="read" src="#expandPath( 'UnusedTicketsWorkbook.xlsx' )#" name="test";
xssfWorkbook = createObject('java', 'org.apache.poi.xssf.usermodel.XSSFWorkbook');
我还发现,如果你将 'cfspreadsheet' 作为 createObject 的第三个参数(bundle 名称)传递,这会强制 Lucee 加载它。
// 明确引用 bundle 名称
xssfWorkbook = createObject('java', 'org.apache.poi.xssf.usermodel.XSSFWorkbook','cfspreadsheet');
值得注意的是,该扩展将 POI jars 重新打包到自己设计的新 bundle 中,这就是为什么 bundle 名称是 'cfspreadsheet'。
<details>
<summary>英文:</summary>
I just tested your code and it seems that the OSGI bundle from the extension isn't actually loaded until you actually hit the cfspreadsheet tag at least once. Once I run the cfspreadsheet tag, the cfspreadsheet bundle in the server admin shows as **active** and then `createObject()` can find the class.
```js
// Force Lucee to activate the bundle
spreadsheet action="read" src="#expandPath( 'UnusedTicketsWorkbook.xlsx' )#" name="test";
xssfWorkbook = createObject('java', 'org.apache.poi.xssf.usermodel.XSSFWorkbook');
I also just figured out, if you pass 'cfspreadsheet' as the third param to create object (bundle name) that forces Lucee to load it.
// Explicitly reference the bundle name
xssfWorkbook = createObject('java', 'org.apache.poi.xssf.usermodel.XSSFWorkbook','cfspreadsheet');
It's worth noting the extension re-packages the POI jars inside a new bundle of it's own design, which is why the bundle name is cfspreadsheet
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论