小弟是一名大三的学生,最近要做一个acm程序练习的网站,练习者在浏览器中将源代码粘贴在文本域中,提交给服务器后,我根据他编码的语言保存为相应的源程序文件,问题如下:
(1)如何来编译这个文件?
(2)运行时,我有一个测试文件,保存的是测试程序的数据,现我想用它作为程序的输入文件,该如何来做?
希望给为前辈能给出一些思路,最好给出一些关键代码,小弟感激不尽(Windows和linux下的方案都可以,关键代码最好为java代码)

解决方案 »

  1.   

    http://topic.csdn.net/u/20090703/00/bad2ed0a-a3bc-4736-8060-ead440feb146.html这篇文章是一个java程序运行另一个java程序.里面要讨论到跟你这个差不多的问题. 你可以调用 commond编译你获得的源文件.还可以获得编译运行后得到的结果. 
      

  2.   

    其实1楼那位同志说的没错,他给你那个链接可以给你一些帮助。我也看了些资料,下面我给你全部代码,其实,你最主要的是第一个问题,先解决第一个问题:第一个类是主类test.java(先就这样叫吧),它将执行另一个文件demo.java.
    先说思想:第一个问题的解决其实是通过程序去让java虚拟机,就是那个java.exe去执行我们想要执行的另一个java文件。所以,首先应当找到java.exe的位置,然后是被执行的java文件(demo.java)生成的class文件(demo.class)的位置。
    当调用成功时,也就是说另一个文件demo.class被javaVM执行后,现在就存在两个进程,test.class所在的进程我们把它叫作父进程,执行另外一文件demo.class时也生成了一个进程,我们把它叫作子进程。
    在子进程里面,我们的demo.class的main()中可能会输出一些数据,这些数据可以输出到dos的窗口,但是,我们为了能在父进程中得到这些数据,我们就要在父进程中用某种方法(后面程序中有)把这些数据当作父进程的输入流以便父进程能够读取。
    为什么我要说父进程来得到数据呢?因为,你一定需要在测试程序中输出另一个执行的文件的运行结果。以下是父类的代码:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.List;import com.mydemo.demo;public class test extends Thread{

    InputStream is;
    String type;
    List output=new ArrayList();
    boolean debug=true;

    public test(InputStream is,String type){
    this(is, type, true);

    }
    public test(InputStream is,String type,boolean debug){
    this.is=is;
    this.type=type;
    this.debug=debug;
    } public void run(){
    try{
     
    InputStreamReader isr=new InputStreamReader(is);
    BufferedReader br=new BufferedReader(isr);
    String line=null;
    while((line=br.readLine())!=null){
    output.add(line);
    if(debug){
    System.out.println(type+">"+line);
    }
    }
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    public List getOutput(){
    return output;
    }
    public static void main(String args[]) throws IOException{
    try{
    List<String> list=new ArrayList<String>();
    ProcessBuilder pb=null;
    Process p=null;
    // System.out.println(System.getProperty("java.home"));
    String java=System.getProperty("java.home")+File.separator+"bin"+File.separator
    +"java";
    String classpath=System.getProperty("java.class.path");
    // System.out.println(classpath);
    list.add(java);
    list.add("-classpath");
    list.add(classpath);
    list.add(demo.class.getName());
    pb=new ProcessBuilder(list);
    p=pb.start();
    System.out.println("Command: "+pb.command());
    test error=new test(p.getErrorStream(),"ERROR");
    test output=new test(p.getInputStream(),"OUTPUT");
    error.start();
    output.start();
    int exitValue=p.waitFor();
    System.out.println("ERROR: "+error.getOutput());//error.getOutput()是一个list
    System.out.println("OUTPUT: "+output.getOutput());
    System.out.println("exit value: "+exitValue);
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    }另一个被执行的文件demo.java 如下:
    package com.demo;
    import java.io.IOException;
    public class demo {
    public static void main(String args[]) throws IOException{
    try{
    System.out.println("In demosfdsaf");
    System.out.println("In demooooooooffffffffffffff");
    System.out.println("In demoooooooosssssssssssssssssss");
    System.out.println("In demoooooooggggggggggggggg");
    System.out.println("In demoooooooooosfsaoo");
    System.out.println("In demooofdsafdsafaoooooooooo");
    }catch(Exception e){
    e.printStackTrace();
    }
    }
    }