BEST SITE FOR WEB DEVELOPERS
Java programming language. Lessons for beginners from hyperskill

Ua

IntelliJ IDEA run configurations


Theory

IntelliJ IDEA uses run/debug configurations to run, debug, and test applications. A run/debug configuration represents a set of startup properties, such as VM or JRE options, that are used to run the program. It also contains information about command-line arguments you pass to the program on startup.


§1. Run/Debug configurations dialog

The application run/debug configuration enables you to run or debug applications via the main method. To set up a configuration, open the Edit Configurations dialog.

Edit Configurations dialog

The command-line arguments to pass at the startup of the program are configured at the Build and run section in the Program arguments field.

Program arguments field

The arguments are passed as a sequence of strings separated by whitespaces. While running a program, the strings would be the command-line arguments passed to its entry points. The delimiters between command-line elements are whitespace characters, and the end-of-line delimiter is the newline character.


§2. Passing arguments in configuration

To pass arguments to a program, you should type a list of them in the format you would use on the command line. The whitespaces will be parsed as delimiters, separating one argument from another. These arguments will be passed to the entry point of the program as an array.

It is important to remember that all the command-line arguments are passed as strings, so if they are supposed to be treated as, for example, integers, you should convert them explicitly inside the program.

To pass the arguments correctly, you should use a list of rules in the following paragraph.


§3. Rules for passing arguments

When specifying arguments, follow these rules:

  • Use spaces to separate individual arguments, for example, --data text.txt.
  • If an argument includes spaces, enclose the spaces or the argument that contains spaces in double quotes, for example, some" "arg or "some arg".
  • If an argument includes double quotes (as part of the argument), escape the double quotes using backslashes, for example, \"quoted_value\".

For example, if you want to pass two arguments, --data and text.txt, you should insert them as in the following example using the rules above:

Using the rules

§4. Conclusion

Let's remind ourselves what we have learned in this topic:

  • To pass command-line arguments for your application in IntelliJ IDEA, go to Run/Debug configurations dialog and set the Program arguments field.
  • For a correct specification of command-line arguments, follow the rules from the third paragraph.

You can also view Lesson on hyperskill.org.


Practical tasks and answers

Tasks and answer options are given. The correct option is highlighted in blue color.

№1. Pay attention to the rules

Question: Match the passing line of arguments and what would be actual arguments. We have indicated them in square brackets.

Match the items from left and right columns:

  • --data "Commander Android" Soong - [--data] [Commander Android] [Soong]
  • --data" "Commander Android Soong - [--data Commander] [Android] [Soong]
  • --data Commander Android \"Soong\" - [--data] [Commander] [Android] ["Soong"]
  • --data "Commander" Android Soong - [--data] [Commander] [Android] [Soon]

Explanation. The correct matching for the passing line of arguments and actual arguments is:

Passing line of arguments | Actual arguments

  • --data "Commander Android" Soong | --data, Commander, Android, Soong
  • --data" "Commander Android Soong | --data, Commander, Android, Soong
  • --data Commander Android \"Soong\" | --data, Commander Android, Soong
  • --data "Commander" Android Soong | --data, Commander, Android, "Soong"

The --data option takes a string as its argument. The string can contain any characters, including spaces, commas, and quotation marks. If the string contains spaces or commas, they must be escaped using double quotation marks.

For example, the following two lines of arguments are equivalent:

  • --data "Commander Android Soong"
  • --data Commander Android \"Soong\"

In the first line, the space between "Commander" and "Android" is escaped using double quotation marks. In the second line, the space is not escaped, but the quotation marks are.

The following two lines of arguments are not equivalent:

  • --data Commander Android Soong
  • --data Commander, Android, Soong

The first line only has three arguments, while the second line has four arguments. The fourth argument in the second line is the comma between "Android" and "Soong".


№2. Which tab?

Question: Where can you find the Program command-line arguments in the Run/Debug configuration dialog?

Select one option from the list:

  • Logs tab
  • Before launch window
  • Code Coverage tab
  • Configuration tab ✔

Explanation. The correct option is Configuration tab. In the Run/Debug configuration dialog, the Program command-line arguments are located in the Configuration tab, under the Program arguments field. The Logs tab displays the output of the program, including any errors or warnings. The Before launch window allows you to run custom commands before the program launches. The Code Coverage tab shows you how much of your code is being executed when the program runs.


№3. Pass some arguments

Question: Imagine you have a program reading the content of text files and writing it into the console. It accepts the file names via command-line arguments. What would be the arguments passed to the program in case you need to output the content of files example.txt and implementation.txt lying in the program root folder, and the example.txt file content should go first?

Enter a short text: example.txt implementation.txt

Explanation. The arguments that you would need to pass to the program to output the content of files example.txt and implementation.txt lying in the program root folder, with example.txt file content going first, would be:

example.txt implementation.txt

You would need to separate the file names by spaces. The program would then read the contents of the files in the order that you specified, and write them to the console. The content of example.txt would go first, followed by the content of implementation.txt.

Here is an example of how you would run the program on a Unix-based system:

$ ./program example.txt implementation.txt

This would run the program called program, and pass the file names example.txt and implementation.txt as command-line arguments. The program would then read the contents of the files and write them to the console.


№4. Determine the arguments

Question: You pass the following line to the program as an argument list: "sys\"\" \"path" What would be the arguments?

Select one option from the list:

  • "sys path
  • " sys path
  • sys "path
  • sys"" "path ✔

Explanation. The correct answer is sys"" "path. The string "sys"" "path" is enclosed in double quotes. This means that the spaces between the words "sys", """, "path" are interpreted as literal spaces, not as separators between arguments. Therefore, the arguments that are passed to the program are "sys", """, and "path". The other options are incorrect because they do not take into account the spaces between the words in the string. For example, the option "sys path" would split the string into two arguments: "sys" and "path". The option "sys "path" would also split the string into two arguments, but the first argument would be "sys ", which is not a valid argument. The option "sys" "path" would split the string into three arguments: "sys", """, and "path". However, the second argument would be """, which is not a valid argument.


№5. Count arguments

Question:How many arguments are passed to the program via the line below?

--text-files task.txt output_for"Task"_lines.txt --folder-name "My topics"

Enter a number: 5 ✔

Explanation. There are five arguments passed to the program via the line below:

--text-files task.txt output_for"Task"_lines.txt --folder-name "My topics"

The arguments are:

  1. --text-files: This is the option that specifies the text files that the program should process.
  2. task.txt: This is the name of the first text file.
  3. output_for"Task"_lines.txt: This is the name of the output file for the first text file.
  4. --folder-name: This is the option that specifies the folder that contains the text files.
  5. "My topics": This is the name of the folder that contains the text files.

The first argument, --text-files, is followed by a list of text files. The list of text files is enclosed in double quotes. This means that the spaces between the file names are interpreted as literal spaces, not as separators between arguments. Therefore, the first argument is actually 4 arguments in one.

The correct conclusion is that there are 5 arguments passed to the program.


№6. The output

Question: You are passing arguments 1 2 to the following program:

Java:

public static void main(String[] args) {
    System.out.println(args[0] + args[1]);
}

Kotlin:

fun main(args: Array<String>) {
    print(args[0] + args[1])
}

What would be the output?

Select one option from the list:

  • 12 ✔
  • An IOException will occur
  • 1+2
  • 3

Explanation. Concatenation takes place, not addition!


№7. Charle's mistakes

Question: Your colleague Charles is trying to pass these arguments: --data Emma Rodolphe "Leon" to a program like this:

--data" "Emma, Rodolphe, /"Leon/"

But he's not succeeding, and he's getting sad. Help him find and correct his mistakes, and make Charles happy.

Select one or more options from the list:

  • unnecessary double quotes between --data and Emma ✔
  • forward slashes instead of backslashes around Leon ✔
  • superfluous comma after Emma and Rodolphe ✔
  • unnecessary backslashes around Leon
  • no comma after --data

Explanation. The following are the mistakes Charles made in his command:

  • He used double quotes instead of single quotes around "Leon". Single quotes are used to pass arguments that contain spaces or special characters, such as quotation marks.
  • He used forward slashes instead of backslashes around "Leon". Backslashes are used to escape special characters in Windows command prompts.
  • He put a comma after "Rodolphe". Commas are not allowed in command arguments.

To correct his mistakes, Charles should use the following command:

--data "Emma, Rodolphe, \"Leon\""

This command will correctly pass the arguments "Emma", "Rodolphe", and "Leon" to the program.

Here is a breakdown of the command:

  • The --data option tells the program that the following arguments are data.
  • The double quotes around "Emma, Rodolphe, and "Leon"" are used to pass the arguments as a single string.
  • The backslashes around "Leon" are used to escape the quotation mark in the string.
  • The comma after "Rodolphe" is not allowed, so it should be removed.

№8. Configuration-in-itself

Question: What represents run/debug configuration?

Select one option from the list:

  • only command-line arguments you pass to the program on startup
  • Java Virtual Machine options for better performance and debugging
  • set of startup properties: JVM or JRE options, command-line arguments ✔
  • commands for "run" and "debug"

Explanation. A run/debug configuration is a set of startup properties that includes JVM or JRE options, command-line arguments, and other configuration settings that define how a program should be run or debugged. It is used to specify the environment in which an application should be launched, and can include options such as the main class to be executed, program arguments, and environment variables. This configuration can also include Java Virtual Machine options for better performance and debugging. Therefore, the correct answer is:

set of startup properties: JVM or JRE options, command-line arguments


№9. War and peace

Question: Consider you need to pass to a program the following three arguments: L. Tolstoy, "War and peace", 1867. What would be the correct way to write them in the argument field?

Select one option from the list:

  • L. Tolstoy \"War and peace\" 1867
  • L. Tolstoy "\"War" and "peace"\" 1867
  • "L. Tolstoy" "\"War and peace\"" 1867 ✔
  • L. Tolstoy "War and peace" 1867

Explanation. The correct way to write the arguments in the argument field is:

L. Tolstoy "\"War and peace\"" 1867

The string literal for the book title needs to be enclosed in double quotes. Single quotes would not work because the space between the words "War" and "and" would be interpreted as a delimiter.

The code you provided outputs the following:

L. Tolstoy "War and peace" 1867

This is incorrect because the book title is not enclosed in double quotes.

Here is an example of how you can use the arguments in a Java program:

Example

public class PrintArguments {

  public static void main(String[] args) {
    printArguments("L. Tolstoy", "\"War and peace\"", 1867);
  }

  public static void printArguments(String author, String book, int year) {
    System.out.println(author);
    System.out.println(book);
    System.out.println(year);
  }
}

This code defines a function called print_arguments(), which takes three arguments: the author name, the book title, and the year of publication. The function then prints these arguments to the console.

When this code is executed, it will print the following output to the console:

L. Tolstoy War and peace 1867


№10. Tough question

Question: Pass argument "Bolek_and_Lolek" to the program.

Hint: Everything is part of parameter including double quotes.

Enter a short text: \"Bolek_and_Lolek\" ✔


What are IntelliJ IDEA run configurations?

IntelliJ IDEA run configurations are a set of settings that define how to run a project. The settings can include the following:

  • The program to be run
  • The command-line arguments
  • The environment variables
  • Other settings, such as the debugger

Run configurations can be used to run projects on a local computer, a remote server, or in a Docker container. They can also be used to run projects in different environments, such as development, testing, and production.

To create a new run configuration, open the Run/Debug Configurations dialog (Ctrl+Shift+F10) and click the "+" button. In the "New Run Configuration" dialog, select the type of program you want to run.

Once you have created a run configuration, you can edit it by double-clicking on it in the Run/Debug Configurations dialog. You can also run a project from a run configuration by clicking on the green arrow next to it.

Here are some examples of how you can use run configurations in IntelliJ IDEA:

  • You can create a run configuration to run a Java program with specific command-line arguments.
  • You can create a run configuration to run a web application on a local web server.
  • You can create a run configuration to run a Python program in a Docker container.

Run configurations are a powerful tool that can be used to run projects on different platforms and in different environments.

Here are some additional things to know about run configurations in IntelliJ IDEA:

  • You can create multiple run configurations for the same project. This can be useful if you want to run the project in different ways, such as with different command-line arguments or environment variables.
  • You can save run configurations as project files. This allows you to share them with other developers or import them into other projects.
  • You can run a run configuration from a keyboard shortcut. This can be useful if you run the same run configuration frequently.

Here are some of the different types of run configurations available in IntelliJ IDEA:

  • Application: This type of run configuration is used to run a standalone application.
  • JUnit: This type of run configuration is used to run JUnit tests.
  • TestNG: This type of run configuration is used to run TestNG tests.
  • Spring Boot: This type of run configuration is used to run a Spring Boot application.
  • Docker: This type of run configuration is used to run a Docker container.