包不存在,并且在运行时编译时找不到符号

时间:2014-02-19 12:26:50

标签: java runtime-compilation

即使程序包连接用于运行时编译,程序包也不存在错误。 这是代码。

JavaSourceCompiler javaSourceCompiler = new JavaSourceCompilerImpl();
                JavaSourceCompiler.CompilationUnit compilationUnit = javaSourceCompiler.createCompilationUnit();
                compilationUnit.addClassPathEntry("G:/project/eclispe/www.spade.com/src/com/ibm/database/DbConnection.java");
                compilationUnit.addJavaSource(myClass, sourcecode);
                compilationUnit.addClassPathEntry("/src/com/ibm/database/DbConnection.java");
                System.out.println(compilationUnit.getClassPathsEntries());
                ClassLoader classLoader = javaSourceCompiler.compile(compilationUnit);

                Class<?> FMClass = classLoader.loadClass(myClass);

                Object FMObj= FMClass.newInstance();

                Method forecast = FMClass.getMethod("Forecast",new Class[] {String.class,String.class,String.class,int[].class});
                System.out.println("Forecast method return type is:"+forecast.getReturnType());
                f=(Integer)forecast.invoke(FMObj, new Object[] {partcode,sercid,fm,a});

运行时运行的代码需要导入包com.ibm.database。 找到的错误是:

java.lang.IllegalStateException: string:///CumulativeBean.java:5: package com.ibm.database does not exist


import com.ibm.database.DbConnection;

publistring:///CumulativeBean.java:17: cannot find symbol
symbol  : variable DbConnection
location: class CumulativeBean
ction con=DbConnection.getMyConnstring:///CumulativeBean.java:26: cannot find symbol
symbol  : variable DbConnection
location: class CumulativeBean
tion con1=DbConnection.getMyConnstring:///CumulativeBean.java:35: cannot find symbol
symbol  : variable DbConnection
location: class CumulativeBean
tion con2=DbConnection.getMyConnstring:///CumulativeBean.java:44: cannot find symbol
symbol  : variable DbConnection
location: class CumulativeBean
tion con3=DbConnection.getMyConnstring:///CumulativeBean.java:55: cannot find symbol
symbol  : variable DbConnection
location: class CumulativeBean
tion con4=DbConnection.getMyConn
at     org.abstractmeta.toolbox.compilation.compiler.impl.JavaSourceCompilerImpl.compile(JavaSourceCompilerImpl.java:102)
at org.abstractmeta.toolbox.compilation.compiler.impl.JavaSourceCompilerImpl.compile(JavaSourceCompilerImpl.java:78)
at org.abstractmeta.toolbox.compilation.compiler.impl.JavaSourceCompilerImpl.compile(JavaSourceCompilerImpl.java:72)
at com.ibm.bean.ForecastingBean.Forecast(ForecastingBean.java:124)
at com.ibm.controller.ControllerServlet.doPost(ControllerServlet.java:73)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)

提前致谢

2 个答案:

答案 0 :(得分:0)

它很长,但我在compilationUnit.addClassPathEntry中看到了可能的拼写错误。你有eclispe,它可能应该是eclipse。

但正如我所说这是远景,也许还有另外一个问题。但值得一试:)

答案 1 :(得分:0)

您添加类路径条目的方式似乎有问题:

取代:

 compilationUnit.addClassPathEntry("G:/project/eclispe/www.spade.com/src/com/ibm/database/DbConnection.java");

compilationUnit.addClassPathEntry("G:/project/eclipse/www.spade.com/src/");

当你试图添加同一个类时,删除其他条目。希望这有帮助。