在tomcat下执行class文件的异常
下面这段代码是用来编译执行一个已经存在的java文件的,这个工程用myeclipse可以正常编译执行,没有异常,但是把工程发布到tomcat下,运行tomcat后,在StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);这一行报空指针异常,请各位帮忙看一下,什么原因
再说明一下:我的工程发布到tomcat后,在myeclipse下启动tomcat服务器,能够正确执行工程文件;但是,如果直接启动tomcat时,在执行到上面的那段代码时就会报空指针异常。
在myeclipse下启动tomcat与直接启动tomcat,对发布的工程的执行有什么区别吗?
Java代码
// 编译生成的java文件
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
// ��bDiagnosticCollector����
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager fileManager = compiler
.getStandardFileManager(diagnostics, null, null);
// String realPath1 = System.getProperty("user.dir");
/*
* System.setProperty("user.dir",
* "D:/Administrator/workspace/msmart"); String realPath =
* System.getProperty("user.dir");
*
* Iterable<String> options = Arrays.asList("-encoding",
* "UTF-8", "-d", realPath + "/WebRoot/WEB-INF/classes", "-cp",
* realPath + "/WebRoot/WEB-INF/lib/msmart.jar");
*
* String[] sd = new String[1]; sd[0] = realPath +
* "/src/com/msmart/servlet/Model" + id + ".java";
*/
// System.setProperty("user.dir",
// "D:/Administrator/workspace/msmart");
String path1 = request.getSession().getServletContext()
.getRealPath("/");
String realPath = System.getProperty("user.dir");
System.out.println("CCC" + path1);
System.out.println("BBB" + realPath);
Iterable<String> options = Arrays.asList("-encoding", "UTF-8",
"-d", path1 + "/WEB-INF/classes", "-cp", path1
+ "/WEB-INF/classes");
String[] sd = new String[1];
sd[0] = path1 + "/WEB-INF/classes/com/msmart/servlet/Model"
+ id + ".java";
Iterable<? extends JavaFileObject> compilationUnits = fileManager
.getJavaFileObjectsFromStrings(Arrays.asList(sd));
JavaCompiler.CompilationTask task = compiler.getTask(null,
fileManager, diagnostics, options, null,
compilationUnits);
// ����Դ����
boolean success = task.call();
fileManager.close();
System.out.println((success) ? "����ɹ�" : "����ʧ��");
for (Diagnostic<?> diagnostic : diagnostics.getDiagnostics())
System.out
.printf("Code: %s%n" + "Kind: %s%n"
+ "Position: %s%n" + "Start Position: %s%n"
+ "End Position: %s%n" + "Source: %s%n"
+ "Message: %s%n", diagnostic.getCode(),
diagnostic.getKind(), diagnostic
.getPosition(), diagnostic
.getStartPosition(), diagnostic
.getEndPosition(), diagnostic
.getSource(), diagnostic
.getMessage(null));