JAVA6动态编译的问题

在使用JAVA6动态编译时遇到的一个问题,动态编译方法已经写就。通过main方法调用的动态编译时,编译通过,并可以使用编译生成的类。但是,在Tomcat下调用同样的方法进行动态编译时,编译无法通过。其原因是找不到对应的包。因为动态生成的类中import了其他的一些类。我猜是tomcat的某些加载机制影响了动态编译过程,导致动态编译找不引用的类。请问各位,是否有方法解决?
问题补充:
补充代码:
参数 files是一个JavaFile类型的数组,其中包含了Java类的源文件

Java代码 复制代码

  1. public static Class[] complie(JavaFile[] files) throws Exception  
  2.     {  
  3.         JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();  
  4. //ClassLoader clsLoader=ToolProvider.getSystemToolClassLoader();
  5.         DiagnosticCollector diagnostics = new DiagnosticCollector ();  
  6.         MemoryFileManager filemanage = new MemoryFileManager(compiler  
  7.                 .getStandardFileManager(diagnostics, null, null));  
  8. //MemoryFileManager filemanage = new MemoryFileManager(compiler
  9. //.getStandardFileManager(diagnostics, null, null));
  10. //Log.info("Location/t"+filemanage.)
  11.         ArrayList clsFiles=new ArrayList ();  
  12. for(JavaFile file:files)  
  13.         {  
  14.             JavaFileObject clsfile = MemoryFileManager.makeSource(file  
  15.                     .getFileName(), file.toString());  
  16.             clsFiles.add(clsfile);  
  17.             Log.info("compile:"+file.getFileName()+"/t"+clsfile.getName()+"/t"+clsfile.getKind()+"/t"+clsfile.toUri());  
  18.         }   
  19.         Iterable compilationUnits=clsFiles;  
  20.         JavaCompiler.CompilationTask task = compiler.getTask(null, filemanage,  
  21.                 diagnostics, null, options.getClassNames(), compilationUnits);  
  22. boolean success=false;  
  23. try
  24.         {  
  25.             success = task.call();  
  26.         } catch (Exception e)  
  27.         {  
  28.             e.printStackTrace();  
  29.         }  
  30.         Class[] clazz=new Class[files.length];  
  31. if(success)  
  32.         {  
  33. for(int i=0;i
  34.             {  
  35.                 clazz[i] = filemanage.getClassLoader(null).loadClass(files[i].getFileName());  
  36.             }  
  37.         }   
  38. else
  39.         {  
  40. for (Diagnostic diagnostic : diagnostics.getDiagnostics())  
  41.                 System.out.printf(  
  42. "Code: %s%n" +  
  43. "Kind: %s%n" +  
  44. "Position: %s%n" +  
  45. "Start Position: %s%n" +  
  46. "End Position: %s%n" +  
  47. "Source: %s%n" +  
  48. "Message: %s%n",  
  49.                 diagnostic.getCode(), diagnostic.getKind(),  
  50.                 diagnostic.getPosition(), diagnostic.getStartPosition(),  
  51.                 diagnostic.getEndPosition(), diagnostic.getSource(),  
  52.                 diagnostic.getMessage(null));  
  53.         }  
  54.         filemanage.close();  
  55. return clazz;  
  56.     } 
public static Class[] complie(JavaFile[] files) throws Exception
	{
		JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
		 
		//ClassLoader clsLoader=ToolProvider.getSystemToolClassLoader();
		DiagnosticCollector
  
    diagnostics = new DiagnosticCollector
   
    (); MemoryFileManager filemanage = new MemoryFileManager(compiler .getStandardFileManager(diagnostics, null, null)); //MemoryFileManager filemanage = new MemoryFileManager(compiler //.getStandardFileManager(diagnostics, null, null)); //Log.info("Location/t"+filemanage.) ArrayList
    
      clsFiles=new ArrayList
     
      (); for(JavaFile file:files) { JavaFileObject clsfile = MemoryFileManager.makeSource(file .getFileName(), file.toString()); clsFiles.add(clsfile); Log.info("compile:"+file.getFileName()+"/t"+clsfile.getName()+"/t"+clsfile.getKind()+"/t"+clsfile.toUri()); } Iterable
       compilationUnits=clsFiles; JavaCompiler.CompilationTask task = compiler.getTask(null, filemanage, diagnostics, null, options.getClassNames(), compilationUnits); boolean success=false; try { success = task.call(); } catch (Exception e) { e.printStackTrace(); } Class[] clazz=new Class[files.length]; if(success) { for(int i=0;i
       
       
问题补充:
TOMCAT下的编译诊断信息:

Java代码 复制代码

  1. Code: compiler.err.doesnt.exist  
  2. Kind: ERROR  
  3. Position: 0
  4. Start Position: 0
  5. End Position: 0
  6. Source: mfm:///RuntimeEntitySysUser.java from JavaSourceFromString
  7. Message: mfm:///RuntimeEntitySysUser.java:1: 软件包 com.youngor.dboperator 不存在
  8. Code: compiler.err.doesnt.exist  
  9. Kind: ERROR  
  10. Position: 104
  11. Start Position: 78
  12. End Position: 112
  13. Source: mfm:///RuntimeEntitySysUser.java from JavaSourceFromString
  14. Message: mfm:///RuntimeEntitySysUser.java:4: 软件包 com.youngor.webcore.entity 不存在
  15. Code: compiler.err.doesnt.exist  
  16. Kind: ERROR  
  17. Position: 146
  18. Start Position: 124
  19. End Position: 156
  20. Source: mfm:///RuntimeEntitySysUser.java from JavaSourceFromString
  21. Message: mfm:///RuntimeEntitySysUser.java:6: 软件包 com.youngor.dboperator 不存在
  22. Code: compiler.err.doesnt.exist  
  23. Kind: ERROR  
  24. Position: 202
  25. Start Position: 180
  26. End Position: 212
  27. Source: mfm:///RuntimeEntitySysUser.java from JavaSourceFromString
  28. Message: mfm:///RuntimeEntitySysUser.java:9: 软件包 com.youngor.dboperator 不存在
  29. Code: compiler.err.doesnt.exist  
  30. Kind: ERROR  
  31. Position: 239
  32. Start Position: 213
  33. End Position: 251
  34. Source: mfm:///RuntimeEntitySysUser.java from JavaSourceFromString
  35. Message: mfm:///RuntimeEntitySysUser.java:9: 软件包 com.youngor.webcore.entity 不存在
  36. Code: compiler.err.doesnt.exist  
  37. Kind: ERROR  
  38. Position: 0
  39. Start Position: 0
  40. End Position: 0
  41. Source: mfm:///RuntimeEntitySysPosition.java from JavaSourceFromString
  42. Message: mfm:///RuntimeEntitySysPosition.java:1: 软件包 com.youngor.dboperator 不存在
  43. Code: compiler.err.doesnt.exist  
  44. Kind: ERROR  
  45. Position: 108
  46. Start Position: 82
  47. End Position: 120
  48. Source: mfm:///RuntimeEntitySysPosition.java from JavaSourceFromString
  49. Message: mfm:///RuntimeEntitySysPosition.java:4: 软件包 com.youngor.webcore.entity 不存在
  50. Code: compiler.err.cant.resolve.location  
  51. Kind: ERROR  
  52. Position: 328
  53. Start Position: 328
  54. End Position: 337
  55. Source: mfm:///RuntimeEntitySysUser.java from JavaSourceFromString
  56. Message: mfm:///RuntimeEntitySysUser.java:14: 找不到符号
  57. 符号: 类 EntitySet  
  58. 位置: 类 RuntimeEntitySysUser  
  59. Code: compiler.err.cant.resolve.location  
  60. Kind: ERROR  
  61. Position: 383
  62. Start Position: 383
  63. End Position: 392
  64. Source: mfm:///RuntimeEntitySysUser.java from JavaSourceFromString
  65. Message: mfm:///RuntimeEntitySysUser.java:14: 找不到符号
  66. 符号: 变量 DefaultDB  
  67. 位置: 类 RuntimeEntitySysUser  
  68. Code: compiler.err.doesnt.exist  
  69. Kind: ERROR  
  70. Position: 436
  71. Start Position: 410
  72. End Position: 448
  73. Source: mfm:///RuntimeEntitySysUser.java from JavaSourceFromString
  74. Message: mfm:///RuntimeEntitySysUser.java:14: 软件包 com.youngor.webcore.entity 不存在
  75. Code: compiler.err.cant.resolve.location  
  76. Kind: ERROR  
  77. Position: 619
  78. Start Position: 619
  79. End Position: -1
  80. Source: mfm:///RuntimeEntitySysUser.java from JavaSourceFromString
  81. Message: mfm:///RuntimeEntitySysUser.java:16: 找不到符号
  82. 符号: 变量 super
  83. 位置: 类 RuntimeEntitySysUser  
  84. Code: compiler.err.cant.resolve.location  
  85. Kind: ERROR  
  86. Position: 667
  87. Start Position: 667
  88. End Position: -1
  89. Source: mfm:///RuntimeEntitySysUser.java from JavaSourceFromString
  90. Message: mfm:///RuntimeEntitySysUser.java:18: 找不到符号
  91. 符号: 变量 super
  92. 位置: 类 RuntimeEntitySysUser 
Code: compiler.err.doesnt.exist
Kind: ERROR
Position: 0
Start Position: 0
End Position: 0
Source: mfm:///RuntimeEntitySysUser.java from JavaSourceFromString
Message: mfm:///RuntimeEntitySysUser.java:1: 软件包 com.youngor.dboperator 不存在
Code: compiler.err.doesnt.exist
Kind: ERROR
Position: 104
Start Position: 78
End Position: 112
Source: mfm:///RuntimeEntitySysUser.java from JavaSourceFromString
Message: mfm:///RuntimeEntitySysUser.java:4: 软件包 com.youngor.webcore.entity 不存在
Code: compiler.err.doesnt.exist
Kind: ERROR
Position: 146
Start Position: 124
End Position: 156
Source: mfm:///RuntimeEntitySysUser.java from JavaSourceFromString
Message: mfm:///RuntimeEntitySysUser.java:6: 软件包 com.youngor.dboperator 不存在
Code: compiler.err.doesnt.exist
Kind: ERROR
Position: 202
Start Position: 180
End Position: 212
Source: mfm:///RuntimeEntitySysUser.java from JavaSourceFromString
Message: mfm:///RuntimeEntitySysUser.java:9: 软件包 com.youngor.dboperator 不存在
Code: compiler.err.doesnt.exist
Kind: ERROR
Position: 239
Start Position: 213
End Position: 251
Source: mfm:///RuntimeEntitySysUser.java from JavaSourceFromString
Message: mfm:///RuntimeEntitySysUser.java:9: 软件包 com.youngor.webcore.entity 不存在
Code: compiler.err.doesnt.exist
Kind: ERROR
Position: 0
Start Position: 0
End Position: 0
Source: mfm:///RuntimeEntitySysPosition.java from JavaSourceFromString
Message: mfm:///RuntimeEntitySysPosition.java:1: 软件包 com.youngor.dboperator 不存在
Code: compiler.err.doesnt.exist
Kind: ERROR
Position: 108
Start Position: 82
End Position: 120
Source: mfm:///RuntimeEntitySysPosition.java from JavaSourceFromString
Message: mfm:///RuntimeEntitySysPosition.java:4: 软件包 com.youngor.webcore.entity 不存在
Code: compiler.err.cant.resolve.location
Kind: ERROR
Position: 328
Start Position: 328
End Position: 337
Source: mfm:///RuntimeEntitySysUser.java from JavaSourceFromString
Message: mfm:///RuntimeEntitySysUser.java:14: 找不到符号
符号: 类 EntitySet
位置: 类 RuntimeEntitySysUser
Code: compiler.err.cant.resolve.location
Kind: ERROR
Position: 383
Start Position: 383
End Position: 392
Source: mfm:///RuntimeEntitySysUser.java from JavaSourceFromString
Message: mfm:///RuntimeEntitySysUser.java:14: 找不到符号
符号: 变量 DefaultDB
位置: 类 RuntimeEntitySysUser
Code: compiler.err.doesnt.exist
Kind: ERROR
Position: 436
Start Position: 410
End Position: 448
Source: mfm:///RuntimeEntitySysUser.java from JavaSourceFromString
Message: mfm:///RuntimeEntitySysUser.java:14: 软件包 com.youngor.webcore.entity 不存在
Code: compiler.err.cant.resolve.location
Kind: ERROR
Position: 619
Start Position: 619
End Position: -1
Source: mfm:///RuntimeEntitySysUser.java from JavaSourceFromString
Message: mfm:///RuntimeEntitySysUser.java:16: 找不到符号
符号: 变量 super
位置: 类 RuntimeEntitySysUser
Code: compiler.err.cant.resolve.location
Kind: ERROR
Position: 667
Start Position: 667
End Position: -1
Source: mfm:///RuntimeEntitySysUser.java from JavaSourceFromString
Message: mfm:///RuntimeEntitySysUser.java:18: 找不到符号
符号: 变量 super
位置: 类 RuntimeEntitySysUser

Tomcat输出的异常信息:

Java代码 复制代码

  1. java.lang.NullPointerException  
  2.     at com.youngor.dboperator.EntityUtil.getInstance(EntityUtil.java:36)  
  3.     at com.youngor.dboperator.Entity.getInstance(Entity.java:103)  
  4.     at com.youngor.dboperator.SQLServer2000Adaptor.getEntitySetBySQL(SQLServer2000Adaptor.java:283)  
  5.     at com.youngor.dboperator.SQLServer2000Adaptor.getEntitySet(SQLServer2000Adaptor.java:220)  
  6.     at com.youngor.dboperator.InnerDBOperator.getEntitySet(InnerDBOperator.java:451)  
  7.     at com.youngor.dboperator.DBOperator.getEntitySet(DBOperator.java:350)  
  8.     at com.youngor.dboperator.DefaultDB.getEntitySet(DefaultDB.java:147)  
  9.     at com.youngor.dboperator.DefaultDB.getEntitySet(DefaultDB.java:213)  
  10.     at com.youngor.webcore.entity.SysUser.getEntitySet(SysUser.java:501)  
  11.     at com.youngor.webcore.entity.SysUser.doTest(SysUser.java:610)  
  12.     at org.apache.jsp.login_jsp._jspService(login_jsp.java:66)  
  13.     at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)  
  14.     at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)  
  15.     at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)  
  16.     at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)  
  17.     at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)  
  18.     at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)  
  19.     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)  
  20.     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)  
  21.     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)  
  22.     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)  
  23.     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)  
  24.     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)  
  25.     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)  
  26.     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)  
  27.     at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:857)  
  28.     at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:565)  
  29.     at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1509)  
  30.     at java.lang.Thread.run(Thread.java:619)  
  31. java.lang.NullPointerException  
  32.     at com.youngor.dboperator.SQLServer2000Adaptor.getEntitySetBySQL(SQLServer2000Adaptor.java:284)  
  33.     at com.youngor.dboperator.SQLServer2000Adaptor.getEntitySet(SQLServer2000Adaptor.java:220)  
  34.     at com.youngor.dboperator.InnerDBOperator.getEntitySet(InnerDBOperator.java:451)  
  35.     at com.youngor.dboperator.DBOperator.getEntitySet(DBOperator.java:350)  
  36.     at com.youngor.dboperator.DefaultDB.getEntitySet(DefaultDB.java:147)  
  37.     at com.youngor.dboperator.DefaultDB.getEntitySet(DefaultDB.java:213)  
  38.     at com.youngor.webcore.entity.SysUser.getEntitySet(SysUser.java:501)  
  39.     at com.youngor.webcore.entity.SysUser.doTest(SysUser.java:610)  
  40.     at org.apache.jsp.login_jsp._jspService(login_jsp.java:66)  
  41.     at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)  
  42.     at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)  
  43.     at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)  
  44.     at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)  
  45.     at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)  
  46.     at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)  
  47.     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)  
  48.     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)  
  49.     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)  
  50.     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)  
  51.     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)  
  52.     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)  
  53.     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)  
  54.     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)  
  55.     at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:857)  
  56.     at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:565)  
  57.     at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1509)  
  58.     at java.lang.Thread.run(Thread.java:619) 
java.lang.NullPointerException
	at com.youngor.dboperator.EntityUtil.getInstance(EntityUtil.java:36)
	at com.youngor.dboperator.Entity.getInstance(Entity.java:103)
	at com.youngor.dboperator.SQLServer2000Adaptor.getEntitySetBySQL(SQLServer2000Adaptor.java:283)
	at com.youngor.dboperator.SQLServer2000Adaptor.getEntitySet(SQLServer2000Adaptor.java:220)
	at com.youngor.dboperator.InnerDBOperator.getEntitySet(InnerDBOperator.java:451)
	at com.youngor.dboperator.DBOperator.getEntitySet(DBOperator.java:350)
	at com.youngor.dboperator.DefaultDB.getEntitySet(DefaultDB.java:147)
	at com.youngor.dboperator.DefaultDB.getEntitySet(DefaultDB.java:213)
	at com.youngor.webcore.entity.SysUser.getEntitySet(SysUser.java:501)
	at com.youngor.webcore.entity.SysUser.doTest(SysUser.java:610)
	at org.apache.jsp.login_jsp._jspService(login_jsp.java:66)
	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	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:233)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
	at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:857)
	at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:565)
	at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1509)
	at java.lang.Thread.run(Thread.java:619)
java.lang.NullPointerException
	at com.youngor.dboperator.SQLServer2000Adaptor.getEntitySetBySQL(SQLServer2000Adaptor.java:284)
	at com.youngor.dboperator.SQLServer2000Adaptor.getEntitySet(SQLServer2000Adaptor.java:220)
	at com.youngor.dboperator.InnerDBOperator.getEntitySet(InnerDBOperator.java:451)
	at com.youngor.dboperator.DBOperator.getEntitySet(DBOperator.java:350)
	at com.youngor.dboperator.DefaultDB.getEntitySet(DefaultDB.java:147)
	at com.youngor.dboperator.DefaultDB.getEntitySet(DefaultDB.java:213)
	at com.youngor.webcore.entity.SysUser.getEntitySet(SysUser.java:501)
	at com.youngor.webcore.entity.SysUser.doTest(SysUser.java:610)
	at org.apache.jsp.login_jsp._jspService(login_jsp.java:66)
	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
	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:233)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
	at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:857)
	at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:565)
	at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1509)
	at java.lang.Thread.run(Thread.java:619)

问题补充:
public static Class[] complie(JavaFile[] files) throws Exception  这个方法是用main(String[] args)方法测试成功。放到JSP页面中调用输错。错误信息已经在上面给出。
问题补充:
把com.youngor.*之类的类文件,放到tomcat下的common或shared下对应的classes或lib下,应该就不会报这个异常 或许可以解决问题。但是这不太符合Web应用部署的常规做法,不知“lovewhzlq”是否还有其他可行方法,不吝赐教。
问题补充:
问题基本解决,需要为编译过程指定编译的类路径;

Java代码 复制代码

  1. C:/myproject>javac -help  
  2. 用法:javac <选项> <源文件>  
  3. 其中,可能的选项包括:  
  4. -g 生成所有调试信息  
  5. -g:none 不生成任何调试信息  
  6. -g:{lines,vars,source} 只生成某些调试信息  
  7. -nowarn 不生成任何警告  
  8. -verbose 输出有关编译器正在执行的操作的消息  
  9. -deprecation 输出使用已过时的 API 的源位置  
  10. -classpath <路径> 指定查找用户类文件的位置  
  11. -cp <路径> 指定查找用户类文件的位置  
  12. -sourcepath <路径> 指定查找输入源文件的位置  
  13. -bootclasspath <路径> 覆盖引导类文件的位置  
  14. -extdirs <目录> 覆盖安装的扩展目录的位置  
  15. -endorseddirs <目录> 覆盖签名的标准路径的位置  
  16. -d <目录> 指定存放生成的类文件的位置  
  17. -encoding <编码> 指定源文件使用的字符编码  
  18. -source <版本> 提供与指定版本的源兼容性  
  19. -target <版本> 生成特定 VM 版本的类文件  
  20. -version 版本信息  
  21. -help 输出标准选项的提要  
  22. -X 输出非标准选项的提要  
  23. -J<标志> 直接将 <标志> 传递给运行时系统 
C:/myproject>javac -help
用法:javac <选项> <源文件>
其中,可能的选项包括:
-g 生成所有调试信息
-g:none 不生成任何调试信息
-g:{lines,vars,source} 只生成某些调试信息
-nowarn 不生成任何警告
-verbose 输出有关编译器正在执行的操作的消息
-deprecation 输出使用已过时的 API 的源位置
-classpath <路径> 指定查找用户类文件的位置
-cp <路径> 指定查找用户类文件的位置
-sourcepath <路径> 指定查找输入源文件的位置
-bootclasspath <路径> 覆盖引导类文件的位置
-extdirs <目录> 覆盖安装的扩展目录的位置
-endorseddirs <目录> 覆盖签名的标准路径的位置
-d <目录> 指定存放生成的类文件的位置
-encoding <编码> 指定源文件使用的字符编码
-source <版本> 提供与指定版本的源兼容性
-target <版本> 生成特定 VM 版本的类文件
-version 版本信息
-help 输出标准选项的提要
-X 输出非标准选项的提要
-J<标志> 直接将 <标志> 传递给运行时系统

修改部分代码如下:

Java代码 复制代码

  1. String cp=PathTool.get(AdvancedCompilerAPIDemo.class);  
  2.             Log.info(cp);  
  3. int i=cp.indexOf("/WEB-INF/classes/");  
  4.             cp=cp.substring(0, i+17);  
  5.             Log.info(cp);  
  6.             Iterable options = Arrays.asList("-d", cp,"-cp",cp); // 编译选项,将编译产生的类文件放在当前目录下
  7.             CompilationTask task = compiler.getTask(null, fileManager,  
  8.                     diagnostics, options, null, sourcefiles); 
String cp=PathTool.get(AdvancedCompilerAPIDemo.class);
			Log.info(cp);
			int i=cp.indexOf("/WEB-INF/classes/");
			cp=cp.substring(0, i+17);
			Log.info(cp);
			Iterable
        
          options = Arrays.asList("-d", cp,"-cp",cp); // 编译选项,将编译产生的类文件放在当前目录下
			CompilationTask task = compiler.getTask(null, fileManager,
					diagnostics, options, null, sourcefiles);
        

这里找到了WEb应用的classes目录并设置为编译输出目录和类路径。编译通过并可以使用了。

那就得自己定义类加载器去加载com.youngor.*里面的类文件,而不是用tomcat默认的加载器去加载类,因为父加载器是不能加载子加载器下面的类文件的,

你可能感兴趣的:(java,exception,null,sqlserver,compiler,Diagnostics)