Run Dart programs in the CLI using Terminal or CMD
To run dart programs in Terminal or CMD, first ensure Dart SDK in installed on your system.
Windows
Open CMD by pressing Win + R, typing “cmd,” and hitting Enter. Navigate to the directory containing your Dart program using the “cd” command.
If the Dart SDK isn’t recognized, you might need to add the SDK’s “bin” directory to your system’s PATH variable. This allows CMD to recognize the “dart” command globally.
To add Dart SDK to PATH, find the directory path where Dart SDK is installed. Copy this path and open the System Properties by right-clicking on “This PC” or “My Computer,” selecting “Properties,” and clicking “Advanced system settings.”
In the System Properties window, click on “Environment Variables,” find the PATH variable in the “System variables” section, and click “Edit.”
Add a new entry and paste the path to the Dart SDK’s “bin” directory. Click “OK” to save the changes. After adding Dart SDK to PATH, close and reopen CMD for the changes to take effect.
Now, you can run Dart programs in CMD from any directory without specifying the full path to the Dart executable.
Once your program runs successfully, CMD will display the output in the console. You can interact with the program as intended, whether it’s a simple print statement or a more complex application.
Mac : Open the Terminal
Check the dart version:
dart --version
How to compile and run a dart program?
Compiling and running a Dart program in the command line is super easy. The “dart compile” command is essential for compiling a Dart program. Let’s compile a sample program that retrieves input from the keyboard and displays it on the console.
Sample Program:
import 'dart:io';
void main(){
stdout.write('Enter your name');
String? userInput = stdin.readLineSync();
print('Your name is $userInput');
}
Let’s run it!
CLI Command:
dart compile exe hello.dart
Run dart program using ./
./hello.exe
Output:
Mastering the execution of Dart programs via the command line interface opens a gateway to seamless development across platforms. Whether navigating through CMD in Windows or Terminal on Mac, the process remains straightforward. This tutorial illuminates the steps to effortlessly compile and execute Dart code, facilitating efficient program testing and debugging.