当前位置: 技术问答>java相关
如何使用JSP远程编译一个Java类?
来源: 互联网 发布时间:2015-04-01
本文导语: 如何使用JSP远程编译一个Java类? 请附上详细说明,或者网页地址。谢谢! | import java.io.*; import java.util.*; import java.lang.reflect.*; public class MakeTodayClass { Date today = new Date(); String tod...
如何使用JSP远程编译一个Java类?
请附上详细说明,或者网页地址。谢谢!
请附上详细说明,或者网页地址。谢谢!
|
import java.io.*;
import java.util.*;
import java.lang.reflect.*;
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 + ":nn");
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);
}
public void runIt() {
try {
Class params[] = {};
Object paramsObj[] = {};
Class thisClass = Class.forName(todayClass);
Object iClass = thisClass.newInstance();
Method thisMethod = thisClass.getDeclaredMethod("doit", params);
thisMethod.invoke(iClass, paramsObj);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
import java.util.*;
import java.lang.reflect.*;
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 + ":nn");
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);
}
public void runIt() {
try {
Class params[] = {};
Object paramsObj[] = {};
Class thisClass = Class.forName(todayClass);
Object iClass = thisClass.newInstance();
Method thisMethod = thisClass.getDeclaredMethod("doit", params);
thisMethod.invoke(iClass, paramsObj);
}
catch (Exception e) {
e.printStackTrace();
}
}
}