db4soundman
db4soundman

Reputation: 33

Using ProcessBuilder to run multiple programs

I'm trying to create an autograder for my Java programming class I'm teaching this semester. The basic idea of the autograder is that it will place a copy of my tester's .class file in same location as the students' turnins (each in their own folder). It will report the results in a text file and give the data back to me.

The issue that I'm stuck on is that I cannot get the ProcessBuilder to execute my tester class, and I've tried various different ways of typing the command out, but I'm not sure what the correct command is. Here is the code:

public...main(String[] args){
///Code not relevant ommitted
    Runtime runtime = Runtime.getRuntime();

    for (String person : uniqueIds) {
        File currentLoc = new File(HW_ID + "/" + person);
        ProcessBuilder g = new ProcessBuilder("bash", "-c",
                "java", " -cp ", currentLoc.getAbsolutePath(),
                " Grader");
        Process process = g.start();
        process.waitFor();
    }
}

"uniqueIds" is a String Array that contains each student's uniqueId, which is the name of the folder that their homework is turned in to. "HW_ID" is a constant that is the name of the folder that the turnin set for all of the students is housed in. So the file structure is: HW_ID/uniqueID/

"Grader" is the name of my program that will grade the turnins. How can I use ProcessBuilder to start my Grader program that is in the specified file location?

EDIT: I'm using a Mac on this, but if the code for Windows is different, please list both. Thanks!

Upvotes: 2

Views: 2088

Answers (2)

Alan
Alan

Reputation: 822

Assuming the program you are trying to run is a Java program and that it exists in the same current directory, here is an example of how I can run a program named SerialNumber.java from another program named Tester.java.

SerialNumber.java:

import java.io.*;

public class SerialNumber
{
  public static void main(String[] args)
  {
    try{
      SerialNumber sn = new SerialNumber();
      System.out.println(sn.executeVolCommand());
    }
    catch(Exception e){e.printStackTrace();} 
  }

  public String executeVolCommand()
  {
    String NEWLINE = System.getProperty("line.separator");
    StringBuffer buffer = new StringBuffer();
    try{

      Process pb = new ProcessBuilder("cmd","/c", "vol").start();  
      InputStream in = pb.getInputStream();  
      BufferedReader br = new BufferedReader(new InputStreamReader(in));  
      String line;  
      while ((line = br.readLine()) != null) {  
        buffer.append(line + NEWLINE);  
      }
    }
    catch(Exception e){e.printStackTrace();}
    return buffer.toString(); 
  }
}

Tester.java:

import java.io.*;

public class Tester
{
  public static void main(String[] args)
  {
    try{
      File currentLoc = new File("SerialNumber");
      System.out.println(currentLoc.getAbsolutePath());
      ProcessBuilder pb = new ProcessBuilder("java.exe",currentLoc.getName());
      Process p = pb.start();

      //Read out dir output
      InputStream is = p.getInputStream();
      InputStreamReader isr = new InputStreamReader(is);
      BufferedReader br = new BufferedReader(isr);
      String line;

      while ((line = br.readLine()) != null) {
        System.out.println(line);
      }
    }
    catch(Exception e){e.printStackTrace();}
  }
}

Hope this helps.

Upvotes: 0

Eric Woodruff
Eric Woodruff

Reputation: 6410

You need to separate the process arguments with a comma rather than using +. Otherwise it concats them to be a single parameter "java-cp"...

... g = new ProcessBuilder("bash", "-c", "java", "-cp", ...

Upvotes: 2

Related Questions