gpt4 book ai didi

java - 使用从命令行运行的命令在 Java 中使用 exec 进行编译和执行失败

转载 作者:行者123 更新时间:2023-12-01 11:48:31 26 4
gpt4 key购买 nike

所以想法是下面代码中的这一行

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");
}
}
}

最佳答案

cd 不是一个程序,它是一个 shell 命令。

您可以使用 ProcessBuilder 来代替,这将允许您定义应该执行命令的工作目录上下文

类似于 this for example

前面示例中的缩写代码,已更新以提供指定工作目录的能力

public int compile(String file, File workingDirectory) throws IOException, InterruptedException {        
ProcessBuilder pb = new ProcessBuilder("javac", file);
pb.redirectError();
pb.directory(new File(workingDirectory));
Process p = pb.start();
InputStreamConsumer consumer = new InputStreamConsumer(p.getInputStream());
consumer.start();

int result = p.waitFor();

consumer.join();

System.out.println(consumer.getOutput());

return result;
}

public class InputStreamConsumer extends Thread {

private InputStream is;
private IOException exp;
private StringBuilder output;

public InputStreamConsumer(InputStream is) {
this.is = is;
}

@Override
public void run() {
int in = -1;
output = new StringBuilder(64);
try {
while ((in = is.read()) != -1) {
output.append((char) in);
}
} catch (IOException ex) {
ex.printStackTrace();
exp = ex;
}
}

public StringBuilder getOutput() {
return output;
}

public IOException getException() {
return exp;
}
}

你可以使用类似的东西来调用......

compile("PackA/classA.java", new File("/Users/fnord/Documents/workspace/LearningJava/src"));

现在,如果你真的有勇气,你可以看看 How do you dynamically compile and load external java classes? ,它使用 javax.tools.JavaCompiler` 类来编译 Java 文件...

关于java - 使用从命令行运行的命令在 Java 中使用 exec 进行编译和执行失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28955020/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com