Компиляция и выполнение с использованием exec в Java не удается с помощью команды, которая работает из командной строки

Таким образом, идея заключается в том, что эта строка в коде ниже

Runtime.getRuntime().exec("cd /Users/fnord/Documents/workspace/LearningJava/src/PackA/; javac classA.java; cd ..; java PackA.classA");

следует сделать то же самое, что и эта строка

cd /Users/fnord/Documents/workspace/LearningJava/src/PackA/; javac classA.java; cd ..; java PackA.classA

когда эта вторая строка запускается из терминала. То есть для компиляции и запуска кода Java. Я неправильно понимаю, как работает exec ()? Если это так, что было бы лучшим способом добиться того, чего я хочу достичь?

package PackA;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;


public class classA {
    public static void main(String[] args) throws Exception{
        ClassLoader loader = classA.class.getClassLoader();

        //Sets the file path to the path of the current .java file
        File file = new File(loader.getResource(classA.class.getPackage().getName()+"/"+classA.class.getSimpleName()+".class").toString().replaceAll("file:", "").replaceAll("bin", "src").replaceAll("sA.class", "sA.java"));

        BufferedReader in = new BufferedReader(new FileReader(file)); //establishes the reader that will be used to read this .java file    
        StringBuffer string = new StringBuffer(); //the stringBuffer that will be used to hold the contents of this .java file
        String stringRead = in.readLine(); //sets a string to the first line of this .java file

        while((stringRead) != null){ //as long as we haven't reached the end of the file
            string.append(stringRead); //append the line
            string.append(System.getProperty("line.separator")); //go to the next line
            stringRead = in.readLine(); //read the next line
        }

        Integer intToFind = new Integer(0); //the integer being looked for

        if (intToFind<=10) { //as long as the intToFind is less than or equal to 10
            //increment the intToFind in the stringBuffer 
            StringBuffer newProgram = new StringBuffer(string.toString().replaceFirst("[(]"+intToFind.toString(), "("+String.valueOf(++intToFind)));
            //establishes the writer that will be used to write to the file
            BufferedWriter out = new BufferedWriter(new FileWriter(file));

            out.write(newProgram.toString()); //write the newProgram to this .java file with the incremented intToFind

            in.close(); //close both the reader and writer
            out.close();

            //Go to the directory of the java file, compile the code, move down one directory, execute the .class file
            Runtime.getRuntime().exec("cd /Users/fnord/Documents/workspace/LearningJava/src/PackA/; javac classA.java; cd ..; java PackA.classA");
        }
    }
}

Ответы на вопрос(1)

Ваш ответ на вопрос