Hi friends, I want to create an auto-evaluator for java.. If I submit java program, it has to run and show the output or exceptions(if so).. I tried in some simple ways, it doesnt works out. please give some suggestions how to start and progress... Thanks in advance...

How is this different from executing the javac and java programs with the classes to be tested?

Do you mean something like JUnit, where you define test cases and it runs them and checks that the results are correct?

Sorry for late reply Norm and James ... @James ... I should give my piece of code to that program where it should compile and execute for the test case we give to it.. For ex: http://ideone.com/ .. In this website, we can provide any language code and evaluate it.. Like this only am trying to develop.. Give some idea to start and progress with this project ... Thanks in advance...

What "evaluations" are done to the code?

Hi NormR1, My spec is to compile and run java program with sets of test cases using another java program... If am not clear, am sorry, You just visit this Link http://ideone.com/ ...

compile and run java program

The javac program will compile a program. The java program will execute a program.
There is a Compiler class and JavaCompiler interface that could be used in a program to compile a java source.
The class's main method could be called to execute the program.

with sets of test cases

What are the "test cases"? Are they files that the program is to read?

Ya i tried with "JavaCompiler" as well "Process" class in java... Both running another java class but we cant provide input variables to it.. Only run time args accepted .. Test cases in the sense --> for the below code snippet we can provide any string as input for testing .. Am unable to provide Input to this program while running using JavaCompiler or process classes in Java without using Console... I need to provide Input to this code through that code itself not via Console...

public class Main{
    public static void main(String[] ar){
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        System.out.println(str);
    }
}

You can change System.in by using a System class method.

Yes Norm .. Good Idea, But User wont give their code snippet as like that know.. What the think is I wondered about http://www.codechef.com(online programming contest) .. If I provide a code into their evaluation page, it compile and runs my program with some predefined test cases(Input to the specific program)... I provided it with System.in only ... I just want to know how they are processing the code, so that I decided to create as like that but I got stuck by providing the test cases or inputs to that program ...

I got stuck by providing the test cases or inputs to that program ...

If the user submitting the code does not provide input for test cases, I have no idea how a program could generate it.

If I ask you to create a simple Integer sorting program, You will program to get Input and do sorting and print output.. So the Inputs are predefined and outputs are expected ones..
If you give me that program, I will test whether its working fine or not.. I need to automate it friend .. I know what will be the Input, so I can have it in a file and fetch that and give that as Input to the program ... So my code doesnt generate input to that, we know what will be the input and output so we can check the program runs proper or not... got????

In general, a program can not generate input for another program without knowing a lot about what input the program expects and the order it should be in.

Have you understand Norm, What my Spec is.. ok .. Is any idea to give any Input to program watever it is.. jus providing Input to a program using another program..

providing Input to a program using another program..

Where does the program being tested get the the input?
From a disk file,
from System.in,
from somewhere else???

I assume that the contents of the input has been provided by the person that submits the program for testing.

Hi Norm, This is my code to Run a java program in another ..

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class jus {

  private static void printLines(String name, InputStream ins) throws Exception {
    String line = null;
    BufferedReader in = new BufferedReader(
        new InputStreamReader(ins));
    while ((line = in.readLine()) != null) {
        System.out.println(name + " " + line);
    }
  }

  private static void exeCode(String command) throws Exception {
    Process pro = Runtime.getRuntime().exec(command);
    printLines(command + " stdout:", pro.getInputStream());
    printLines(command + " stderr:", pro.getErrorStream());
    pro.waitFor();
    System.out.println(command + " exitValue() " + pro.exitValue());
  }

  public static void main(String[] args) {
    try {
      exeCode("javac Main1.java");
      exeCode("java Main1");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}




public class Main1 {
    public static void main(String[] ar){
    // This block will Run
    System.out.println("This print will run");
    // This block waits for Input
    java.util.Scanner sc = new java.util.Scanner(System.in);
        System.out.println(sc.next());
    }
}

Run jus.java and trying to provide Input to Main1.java's scanner object(sc) via jus.java itself...
Thanks in advance...

Please explain what the problem is you are having executing this code so I'll know what to look for when (or if) I test it.

jus.java will Compile Run Main1.java ... Now Main1.java contains Scanner object which has to be initailized .. I want to do it via jus.java itself Norm ..

trying to provide Input to Main1.java's scanner object(sc)

The code runs in a separate JVM: exeCode("java Main1"). I don't know how to change System.in for that.
The Process class has methods to connect to the input stream.

ok Norm .. This is what am trying to do .. K if you got any suggestion please post.. Thank you ...

I don't know how to send data to a class being executed by the java program.
Have you tried using the Process class's method to connect to the input stream?
What happened? I would think that would connect to the java program, not the class being executed, but you never know until you try it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.