资源既没有源路径也没有目标路径。无法将其添加到捆绑包中

Resource does not have neither a source nor a target path. Impossible to add it to the bundle

我正在尝试使用 kie api 6 读取包含规则的 .xlsx 文件。 但是我得到这个例外

java.lang.RuntimeException: Resource does not have neither a source nor a target path. Impossible to add it to the bundle. P
lease set either the source or target name of the resource before adding it.null
        at org.drools.compiler.kie.builder.impl.KieFileSystemImpl.write(KieFileSystemImpl.java:83)
        at com.project.watch.validator.RuleValidator.isValidRuleKieNew(RuleValidator.java:71)
        at com.project.watch.validator.RuleValidator.isValidRule(RuleValidator.java:37)
        at com.project.watch.core.WatchDir$ValidateFileRunnable.run(WatchDir.java:177)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
        at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access1(ScheduledThreadPoolExecutor.java:
178)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:745)

代码如下所示

try { 
        KieServices ks = KieServices.Factory.get();
        KieFileSystem kfs = ks.newKieFileSystem();
        kfs.write(ks.getResources().newInputStreamResource(Files.newInputStream(p, StandardOpenOption.READ)));
        KieBuilder kb = ks.newKieBuilder(kfs);
        if (kb.getResults().getMessages(Level.ERROR).size() != 0) {
            logger.lifecycle("Invalid file: {}.", kb.getResults().getMessages());
            return false;
        }
    } catch (Exception e) {
        logger.lifecycle("UNEXPECTED ERROR DURING VALIDATION -------------- ", e);
        return false;
    }
    logger.lifecycle("Valid file: {}.", p);
    return true;
}

顺便说一句,它在 kfs.write 上失败了。

我还通过在控制台中显示来检查文件路径

Attempt to process rule file: D:\PROJECT_FOLDER\project\src\testInteg\resources\rules\rule.xlsx

这是一条正确的道路。 有什么问题?我正在使用 6.3 Final 版本。

我推荐这个咒语,假设你的String p包含如下所示的路径名:

FileInputStream fis = new FileInputStream( p );
kfs.write( "src/main/resources/rule.xslx",
           kieServices.getResources().newInputStreamResource( fis ) );

writeKieFileSystem还有一些其他形式,但我发现这个很容易使用。您可以从 p 派生 rule.xslx,使用 Java 的 API 作为路径名。