1-1-9-1-4 哪位大侠帮忙看看~
发信人: rehte (Ava), 信区: Java
标题: Re: 哪位大侠帮忙看看~
发信站: BBS 水木清华站 (Mon Nov 18 14:48:26 2002), 转信
我已经试过了,问题出在ComplingClassLoader的compile函数中的
Runtime.getRuntime().exec("javac "+javaFile);一句。
由于这样运行javac命令是不带任何环境参数的,包括path和classpath
,因此你在系统环境中设置的path,classpath全部不起作用了。因此
着个代码运行的结果是
'javac' 不是内部或外部命令,也不是可运行的程序或批处理文件。
你的程序运行时没有报这个错误是因为exec不给被执行程序输出控制
台(output console),因此实际上exec执行失败,class文件并没有生成,
它的返回值ret=1,因此也就会ClassNotFoundException
解决办法
1.使用exec(String []cmdarray,String[]envp)将环境参数path,classpath
通过envp传入。
或者
2.使用sun.tools.javac.Main类编译java源文件,sun.tools.javac.Main属于
核心包的实现库(基础库),可以,import进后同核心类一样使用。
下面是一个例子(摘自tomcat源代码)
public boolean compile(String source) {
Main compiler = new Main(out, "jsp->javac");
String[] args = new String[]
{
"-encoding", encoding,
"-classpath", classpath,
"-d", outdir,
source
};
return compiler.compile(args);
}
【 在 marlborowang (Marlboro) 的大作中提到: 】
: import java.io.*;
: public class CompilingClassLoader extends ClassLoader
: {
: private byte[] getBytes( String filename ) throws IOException
: {
: System.out.println( "begin to load the getBytes method~~~~" );
: File file = new File( filename );
: long len = file.length();
: System.out.println("file = "+file);
: System.out.println("len = "+len);
: byte raw[] = new byte[(int)len];
: ...................
页:
[1]