在tomcat下执行class文件的异常

xijunhu 2008-10-06 11:55:04
下面这段代码是用来编译执行一个已经存在的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));
...全文
53 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
huoyin 2008-10-07
  • 打赏
  • 举报
回复
你的机器上有没有安装jdk?

如果有,则在catalina.bat文件头加入set JAVA_HOME=[JDK_DIRECTORY]
这样就可以使用JDK了
xijunhu 2008-10-07
  • 打赏
  • 举报
回复
我不知道,怎么看啊
huoyin 2008-10-07
  • 打赏
  • 举报
回复
你的tomcat启动时用的是jdk还是jre,如果是jre则会出现你的这个错误。
huoyin 2008-10-07
  • 打赏
  • 举报
回复
jre不带编译环境,jdk带了
xijunhu 2008-10-07
  • 打赏
  • 举报
回复
to:huoyin 非常感谢,能告诉我为什么会有这种问题吗?tomcat启动用jdk,还是用jre有什么区别吗?
xijunhu 2008-10-07
  • 打赏
  • 举报
回复
我觉得可能是jdk的版本不对,myeclipse下用的1.5的,tomcat用的1.6的

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧