问题描述:

This question already has an answer here:

  • Error when trying to execute a command in java [duplicate]

    1 answer

网友答案:

This will not work.

For one, cd is a shell command. And then you create another process to run the java command.

You need a ProcessBuilder. You can set up the initial directory, environment etc:

final File wantedCwd = new File(...);
final ProcessBuilder pb = new ProcessBuilder("java", "thefile");

// Change directory
pb.directory(wantedCwd);

You can even change the stdin, stdout, stderr etc of the command.

DO NOT use Runtime.exec(). It won't (exec()) in many situations.

相关阅读:
Top