public class MakeTodayClass {
Date today = new Date();
String todayMillis = Long.toString(today.getTime());
String todayClass = "z_" + todayMillis;
String todaySource = todayClass + ".java";
public static void main (String args[]){
MakeTodayClass mtc = new MakeTodayClass();
mtc.createIt();
if (mtc.compileIt()) {
System.out.println("Running " + mtc.todayClass + ":\n\n");
mtc.runIt();
}
else
System.out.println(mtc.todaySource + " is bad.");
}
public void createIt() {
try {
FileWriter aWriter = new FileWriter(todaySource, true);
//aWriter.write("package com;");
aWriter.write("public class "+ todayClass + "{");
aWriter.write(" public void doit() {");
aWriter.write(" System.out.println(\""+todayMillis+"\");");
aWriter.write(" }}\n");
aWriter.flush();
aWriter.close();
}
catch(Exception e){
e.printStackTrace();
}
}
public boolean compileIt() {
String [] source = {"-d","d:/",new String(todaySource) };
ByteArrayOutputStream baos= new ByteArrayOutputStream();
//new sun.tools.javac.Main(baos,source[0]).compile(source);
// if using JDK >= 1.3 then use
System.out.println(com.sun.tools.javac.Main.compile(source));
System.out.println("================"+new String(baos.toByteArray()));
return (baos.toString().indexOf("error")==-1);
}
另外,我也想到应该与jvm有关的,但只有产生异常时能得到堆栈信息,调用如下:
public void Invoker(){
.....
Exception e = new Exception();
doReturn(e);
.....
}
如上是可以完全实现此功能的,但这样有点投机取巧,我想实在不行就这样做,但最好能够直接得到堆栈信息。
所以我仍在思考。