Drools报错:The Eclipse JDT Core jar is not in the classpath

阅读更多

Drools报错:The Eclipse JDT Core jar is not in the classpath

 

这是由于drools-compiler.jar自带两套编译机制  默认使用的Eclipse JDT CORE

但是在某些环境下会报The Eclipse JDT Core jar is not in the classpath 比如weblogic环境下

 

解决方法是

将drools默认编译方式改为JANINO,

附上部分测试代码..

 

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderConfiguration;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import org.springframework.stereotype.Service;

/**
 * 调用规则引擎的内部serivce 接口实现
 * @author wuxiaogang
 *
 */
@Service
public class DroolsUtil{
	private static final transient Logger log = Logger.getLogger(DroolsUtil.class);
	/** 本地缓存-已编译的事件规则 */
	private static Map knowledgeBaseMap=new HashMap();
	
	private static final Properties props = new Properties();
	static{
		props.put("drools.dialect.java.compiler","JANINO");
		props.put("drools.dialect.default","java");
		props.put("drools.compiler.lnglevel","1.7");
	}
	public static void main(String[] args) throws Exception {
		DroolsUtil drl=new DroolsUtil();
		//读取文件
		BufferedReader br=new BufferedReader(new FileReader("d:/d4.drl" ));
		StringBuffer sb=new StringBuffer("");
		String line=null;
		while((line=br.readLine())!=null){
			sb.append( line +"\n");
		}
		Map m=new HashMap();
		
		m.put( "service", drl );
		m.put( "obj", new Object() );//具体对象
		m.put( "serviceHandler", false );
		drl.touchDrlStr(sb.toString(),m);//以代码串形式加载
		
		System.out.println("=============="+m.get( "serviceHandler" ));
	}
	/**
	 * 验证生成的规则文件是否正确
	 * @return
	 * @throws Exception
	 */
	private boolean isDrlYN(String drl_name){
		try {
			// 创建规则构建器
			KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
			// 加载规则文件,并增加到构建器
			kbuilder.add(ResourceFactory.newClassPathResource("config/drools/"+drl_name+".drl"),ResourceType.DRL);
			KnowledgeBuilderErrors errors = kbuilder.getErrors();
			// 编译规则过程中发现规则是否有错误
			if (errors.size() > 0) {
				for (KnowledgeBuilderError error : errors) {
					log.error(drl_name+".drl规则中存在错误,错误消息如下:"+error);
				}
				return false;
			}
		} catch (Exception e) {
			log.error(drl_name+".drl规则文件验证异常:",e);
			return false;
		}
		return true;
	}
	/**
	 * 根据规则文件名称 加载编译规则文件.
	 * @return
	 * @throws Exception
	 */
	private static KnowledgeBase readKnowledgeBase(String drl_name) throws Exception {
		KnowledgeBuilderConfiguration kbConfig = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(props, (ClassLoader[]) null);
		// 创建规则构建器
		KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(kbConfig);
		// 加载规则文件,并增加到构建器
		kbuilder.add(ResourceFactory.newClassPathResource("config/drools/"+drl_name+".drl"),ResourceType.DRL);
		KnowledgeBuilderErrors errors = kbuilder.getErrors();
		// 编译规则过程中发现规则是否有错误
		if (errors.size() > 0) {
			for (KnowledgeBuilderError error : errors) {
				System.out.println("规则中存在错误,错误消息如下:");
				System.err.println(error);
			}
			throw new IllegalArgumentException("Could not parse knowledge.");
		}
		// 创建规则构建库
		KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
		// 构建器加载的资源文件包放入构建库
		kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
		return kbase;
	}
	public static boolean touchDrlFile(String drlFileName,Object ... objs) throws Exception {
		try {
			//--根据事件code加载规则--
			KnowledgeBase kbase = knowledgeBaseMap.get(drlFileName); 
			StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
			KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "info");
			if(objs!=null){
				for(Object obj:objs){
					ksession.insert(obj);
				}
			}
			ksession.fireAllRules();
			logger.close();
		} catch (Exception t) {
			t.printStackTrace();
		}
		return false;
	}
	/**
	 * 根据规则文件名称 加载编译规则文件.
	 * @return
	 * @throws Exception
	 */
	private static KnowledgeBase readKnowledgeBaseByStr(String drlcontext) throws Exception {
		KnowledgeBuilderConfiguration kbConfig = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(props, (ClassLoader[]) null);
		// 创建规则构建器
		KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(kbConfig);
		// 加载规则文件,并增加到构建器
		kbuilder.add(ResourceFactory.newInputStreamResource(new ByteArrayInputStream( drlcontext.getBytes() )),ResourceType.DRL);
		KnowledgeBuilderErrors errors = kbuilder.getErrors();
		// 编译规则过程中发现规则是否有错误
		if (errors.size() > 0) {
			for (KnowledgeBuilderError error : errors) {
				System.out.println("规则中存在错误,错误消息如下:");
				System.err.println(error);
			}
			throw new IllegalArgumentException("Could not parse knowledge.");
		}
		// 创建规则构建库
		KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
		// 构建器加载的资源文件包放入构建库
		kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
		return kbase;
	}
	 
	public static void touchDrlStr(String drlContextStr, Object... objs) throws Exception {
		try {
			log.info("-- start run script---------");
			log.info(drlContextStr);
			if(drlContextStr==null || drlContextStr.trim().length()==0){
				return;
			}
			//--根据事件code加载规则--
			KnowledgeBase kbase =readKnowledgeBaseByStr( drlContextStr ); 
			StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
			KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "info");
			if(objs!=null){
				for(Object obj:objs){
					ksession.insert(obj);
				}
			}
			ksession.fireAllRules();
			logger.close();
			log.info("-- end run script ----");
		} catch (Exception t) {
			log.error(".............run script exception.......",t);
		}
	}
}

 附件中文件是代码文件和支持jar.

Drools报错:The Eclipse JDT Core jar is not in the classpath_第1张图片

 

  • Drools报错:The Eclipse JDT Core jar is not in the classpath_第2张图片
  • 大小: 8.3 KB
  • drools.zip (4.5 MB)
  • 下载次数: 2
  • 查看图片附件

你可能感兴趣的:(drools,janino)