如何使用JSP远程编译一个Java类? 如果不用JSP,用Servlet行不行?用compile.php行吗?代码如下:<?system ("javac SomeClass.java")?>我发现还是没有成功,到底为什么? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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 + ":\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); } 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.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 + ":\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); } 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(); } }} 对象不支持此属性或方法 jpg图片转tif格式报异常 有关spring Ioc方面 jsp和javascript的区别是什麽? 求关于URL中的特殊字符的含义? tomact5.5.7 配置数据源问题 在servlet里如何想客户端发出提示对话框? java连接SQLserver! 看一下 启动Tomcat 后的异常 如何自定义http://localhost:8080 所指的页面?在先等待 JSP怎么运行的?原理? 在servlet中如何处理多次上传文件的方法.
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 + ":\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);
}
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 + ":\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);
}
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();
}
}
}