We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

automated code evaluator

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...

3
Contributors
20
Replies
3 Days
Discussion Span
8 Months Ago
Last Updated
21
Views
javalover
Light Poster
30 posts since Jan 2012
Reputation Points: 6
Solved Threads: 3
Skill Endorsements: 0

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

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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

JamesCherrill
... trying to help
Moderator
8,533 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,456
Skill Endorsements: 30

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...

javalover
Light Poster
30 posts since Jan 2012
Reputation Points: 6
Solved Threads: 3
Skill Endorsements: 0

What "evaluations" are done to the code?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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/ ...

javalover
Light Poster
30 posts since Jan 2012
Reputation Points: 6
Solved Threads: 3
Skill Endorsements: 0

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?

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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);
    }
}
javalover
Light Poster
30 posts since Jan 2012
Reputation Points: 6
Solved Threads: 3
Skill Endorsements: 0

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

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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 ...

javalover
Light Poster
30 posts since Jan 2012
Reputation Points: 6
Solved Threads: 3
Skill Endorsements: 0

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.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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????

javalover
Light Poster
30 posts since Jan 2012
Reputation Points: 6
Solved Threads: 3
Skill Endorsements: 0

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.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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..

javalover
Light Poster
30 posts since Jan 2012
Reputation Points: 6
Solved Threads: 3
Skill Endorsements: 0

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.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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...

javalover
Light Poster
30 posts since Jan 2012
Reputation Points: 6
Solved Threads: 3
Skill Endorsements: 0

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.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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 ..

javalover
Light Poster
30 posts since Jan 2012
Reputation Points: 6
Solved Threads: 3
Skill Endorsements: 0

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.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

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

javalover
Light Poster
30 posts since Jan 2012
Reputation Points: 6
Solved Threads: 3
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.1185 seconds using 2.78MB