There are several different ways of doing it, but here is a snippet to help creating an in-memory kjar with default kbases and ksessions:
public static byte[] createKJar(KieServices ks,
ReleaseId releaseId,
String pom,
String... drls) {
KieFileSystem kfs = ks.newKieFileSystem();
if( pom != null ) {
kfs.write("pom.xml", pom);
} else {
kfs.generateAndWritePomXML(releaseId);
}
KieResources kr = KieServices.getResources();
for (int i = 0; i < drls.length; i++) {
if (drls[i] != null) {
kfs.write( kr.newByteArrayResource( drls[i].getBytes() ).setSourcePath("my/pkg/drl"+i+".drl") );
}
}
KieBuilder kb = ks.newKieBuilder(kfs).buildAll();
if( kb.getResults().hasMessages( org.kie.api.builder.Message.Level.ERROR ) ) {
for( org.kie.api.builder.Message result : kb.getResults().getMessages() ) {
System.out.println(result.getText());
}
return null;
}
InternalKieModule kieModule = (InternalKieModule) ks.getRepository()
.getKieModule(releaseId);
byte[] jar = kieModule.getBytes();
return jar;
}
Please note that you usually don't need the byte[] back, so you can ignore everything after the last 3 lines of the code. Also, the error check in the snippet is just printing to sysout. You should handle this accordingly in your application.
Hope it helps.