To run a Java program from the command line on Windows, you’ll need to have the Java Development Kit (JDK) installed on your computer. Here are the steps to run a Java program:
- Install JDK: If you haven’t already, download and install the Java Development Kit (JDK) from the official Oracle website.
- Write Your Java Program: Create a Java source code file with a
.java
extension using a text editor or an integrated development environment (IDE) like Eclipse, IntelliJ, or Visual Studio Code. Save it with a meaningful name, such asMyProgram.java
. - Compile the Program: Open the Command Prompt (CMD) or PowerShell. Navigate to the directory where your Java source code file is located using the
cd
command. For
example
cd path\to\your\program\directory
Once you’re in the correct directory, compile your Java program using the javac
a command followed by the filename:
javac MyProgram.java
This will create a compiled bytecode file named MyProgram.class
.
4. Run the Program: After compiling the program, you can run it using the java
a command followed by the class name (without the .class
extension):
java MyProgram
Make sure you don’t include the .class
extension when running the program.
That’s it! Your Java program should now execute, and you’ll see the output in the command prompt.
Please note that the steps above assume you have set up your system’s PATH environment variable to include the JDK’s bin
directory. If you encounter any issues, double-check that your Java installation is properly configured.
Remember that this is a basic example of running Java programs from the command line. In real-world scenarios, you might have more complex projects with multiple classes and dependencies. For those cases, build tools like Maven or Gradle are commonly used to manage and build Java projects.