当前位置: 动力学知识库 > 问答 > 编程问答 >

eclipse - Run java file using 'java filename' command

问题描述:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: