I usually input command line arguments in Eclipse via run configuration. But I don't know how do achieve the same task in IntelliJ IDEA.
14 Answers
There's an "edit configurations" item on the Run menu, and on the pull-down to the left of the two green "run" and "debug" arrows on the toolbar. In that panel, you create a configuration with the "+" button in the top left, and then you can choose the Class containing main(), add VM parameters and command-line args, specify the working directory and any environment variables.
There are other options there as well: code coverage, logging, build, JRE, etc.
We cannot go into Terminal and give in the good old java Classname arg1 arg2 arg3
We'll have to edit the run configuration.
Step 1 : Take the Run menu
Step 2 : Select Edit Configurations
Step 3 : Fill the Program arguments field
After that, the arguments will be inserted to the end of the command that IntelliJ creates whenever you run the program :)
maytham-ɯɐɥıλɐɯ, you can use this code to simulate input of file:
System.setIn(new FileInputStream("FILE_NAME"));
Or send file name as parameter and then put it into FileInputStream:
System.setIn(new FileInputStream(args[0]));
In IntelliJ, if you want to pass args parameters to the main method.
go to-> edit configurations
program arguments: 5 10 25
you need to pass the arguments through space separated and click apply and save.
now run the program if you print
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println(args[2]);
Out put is 5 10 25
As @EastOcean said, We can add it by choosing Run/Debug configurations option. In my case, I have to set configuration for junit. So on clicking Edit configurations option, a pop up window is displayed. Then followed the below steps:
- Click on + icon
- Choose junit from the list
- Then we can see Configuration tab in the right hand side
- Select test kind, in my case Its a Class
- Next step browse through the location of the class which needs to be executed/run
- Next to that, choose VM Option, click on expand arrow icons
- Set required arguments for an example (-Durl="http://test.com/home" -Dsourcename="API" -Dbrowsername="chrome")
- Set jre path.
Save and run.
Thank you.
It's strange that no one wrote about simply copying "Run" command output into your terminal and adding the arguments.
For example, when I run "Run" command in my Intellij IDEA the output is something like
/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
And then I simply copy the first line and enter my arguments like:
<THIS_HUGE_LINE> foo bar
Note: remember to build the project before running the command in terminal. Either click "Build" or "Run" first. You can find these options, for example, at the bottom: link to the screenshot