1 of 2

  1. // imports...
  2. /**
  3. Takes a command, represented as an array of strings runs it, and returns its combined stdout and stderr as a
  4. string.
  5. */
  6. static String exec(String[] cmd) throws IOException {
  7. Process p = new ProcessBuilder()
  8. .command(Arrays.asList(cmd))
  9. .redirectErrorStream(true)
  10. .start();
  11. InputStream outputOfBash = p.getInputStream();
  12. return new String(outputOfBash.readAllBytes());
  13. }
  14. }
  15. class ExecExamples {
  16. public static void main(String[] args) throws IOException {
  17. String[] cmd1 = {"ls", "lib"};
  18. System.out.println(ExecHelpers.exec(cmd1));
  19. String[] cmd2 = {"pwd"};
  20. System.out.println(ExecHelpers.exec(cmd2));
  21. String[] cmd3 = {"echo hello world”};
  22. System.out.println(ExecHelpers.exec(cmd3));
  23. String[] cmd4 = {"touch", "a-new-file.txt"};
  24. System.out.println(ExecHelpers.exec(cmd4));
  25. }
  26. }

Name:

PID: (page 1)

public ProcessBuilder redirectErrorStream(boolean redirectErrorStream)

Sets this process builder's redirectErrorStream property. If this property is true, then any error output generated by subprocesses subsequently started by this object's start() method will be merged with the standard output, so that both can be read using the Process.getInputStream() method.

public ProcessBuilder command(List<String> command)

Sets this process builder's operating system program and arguments.

public Process start() throws IOException

Starts a new process using the attributes of this process builder.

public abstract InputStream getInputStream()

Returns the input stream connected to the normal output of the subprocess. The stream obtains data piped from the standard output of the process represented by this Process object.

Which line creates and starts a Process?

In ProcessBuilder class

In ProcessBuilder class

In ProcessBuilder class

In Process class

$ javac GradeServer.java Server.java

$ java ExecExamples

hamcrest-core-1.3.jar

junit-4.13.2.jar

/Users/joe/src/list-examples-grader

hello world

list-examples-grader/

lib/

hamcrest-core-1.3.jar

junit-4.13.2.jar

GradeServer.java

Server.java

TestListExamples.java

grade.sh

directory structure

GradeServer.java

What other interesting effects would happen in addition to this output?

2 of 2

  • // imports...
  • class ExecHelpers {
  • static String exec(String[] cmd) throws IOException {
  • Process p = new ProcessBuilder()
  • .command(Arrays.asList(cmd))
  • .redirectErrorStream(true)
  • .start();
  • InputStream outputOfBash = p.getInputStream();
  • return String.format("%s\n", streamToString(outputOfBash));
  • }
  • static String streamToString(InputStream out) { /* ... */ }
  • }
  • class Handler implements URLHandler { /* hidden */ }
  • class GradeServer { /* hidden */ }
  • class ExecExamples {
  • public static void main(String[] args) throws IOException {
  • String[] cmd1 = {"ls", "lib"};
  • System.out.println(ExecHelpers.exec(cmd1));
  • // hid the rest of these examples
  • }
  • }
  • // What if we wanted to use Java, rather than grade.sh, to do the grading?
  • // Use the grade.sh at the bottom of this page for reference
  • // For example, ExecHelpers.exec({"rm", "-rf", "student-submission"}); could be used to
  • // run the first `rm` command in that script
  • class Grade {
  • static String grade(String repo) throws IOException {
  • String CPATH = ".:lib/hamcrest-core-1.3.jar:lib/junit-4.13.2.jar";
  • String result = "";
  • result += ExecHelpers.exec({"rm", "-rf", "student-submission"});
  • }
  • public static void main(String[] args) throws IOException {
  • System.out.println(grade(args[0]));
  • }

Name:

PID: (page 1)

list-examples-grader/

lib/

hamcrest-core-1.3.jar

junit-4.13.2.jar

GradeServer.java

Server.java

TestListExamples.java

grade.sh

directory structure

GradeServer.java

CPATH='.:lib/hamcrest-core-1.3.jar:lib/junit-4.13.2.jar'

rm -rf student-submission

git clone $1 student-submission

echo 'Finished cloning'

cp student-submission/ListExamples.java ./

javac -cp $CPATH *.java

java -cp $CPATH org.junit.runner.JUnitCore TestListExamples

One goal is to be able to run:

$ java Grade https://github.com/...

Cloning into 'student-submission'...

Finished cloning