我通常通过运行配置在Eclipse中输入命令行参数。但是我不知道如何在IntelliJ IDEA中实现同样的任务。


当前回答

Maytham -ɯɐɥah λɐɯ,你可以用这段代码来模拟输入文件:

System.setIn(new FileInputStream("FILE_NAME"));

或者将文件名作为参数发送,然后将其放入FileInputStream:

System.setIn(new FileInputStream(args[0]));

其他回答

奇怪的是,没有人写过简单地将“Run”命令输出复制到终端并添加参数。

例如,当我在Intellij IDEA中运行“run”命令时,输出如下

/usr/lib/jvm/java-1.11.0-openjdk-amd64/bin/java -javaagent:/opt/idea-IC-211.7142.45/lib/idea_rt.jar=32887:/opt/idea-IC-211.7142.45/bin -Dfile.encoding=UTF-8 -classpath /home/aliaksei/Projects/Training/Java/firstTry/out/production/firstTry MyClass

Process finished with exit code 0

然后我简单地复制第一行,输入我的参数,像这样:

<THIS_HUGE_LINE> foo bar

注意:记得在终端运行命令前先构建工程。先点击“构建”或“运行”。例如,你可以在底部找到这些选项: 链接到截图

自IntelliJ IDEA 2021年以来,我对这个问题添加了更新的答案。

Select Edit Configuration from the toolbar (or under the Run menu): Picture of new location of edit configuration box You now have to click on the Modify Options menu (Alt + M) to the left of where it says Build and Run, and above the drop down menu that lets you select the version of Java (or other language) that your using. After you click Modify Options the first box you will see--titled Operating System--will have a line that says "Redirect input". Click on that to add the box for input redirection. Close Modify Options. Under Build and Run, the third box down is for command line input.

Intelij IDE的图片与编辑配置框打开箭头指向cmd行输入和输入重定向

这就是它的全部。

点击“编辑配置…”从工具栏顶部 在弹出窗口中,确保“环境”部分展开(通过单击下拉箭头) 应用程序的命令行参数应该进入“程序参数”字段

如果你正在使用intellij去运行>编辑配置菜单设置。将出现一个对话框。现在您可以向Program arguments输入字段添加参数。

Windows、Linux和部分mac电脑:

ALT+SHIFT+F10,右,E, Enter, Tab,输入你的命令行参数,Enter。: -)

Mac的“OS X 10.5”键模式:

CTRL+ALT+R,右,E, Enter, Tab,输入你的命令行参数,Enter。