- Mastering Java Command Line Run: A Step-by-Step Guide for Windows Users
- Navigating to JDK Directory
- Compiling Java Programs
- How to Compile and Run Java Program from Command Prompt
- Executing Java Programs
- Passing Command-Line Arguments
- Running Shell Commands in Java
- Java on Command Line without an IDE
- Other simple code samples for running Java programs through the command line on Windows
- Conclusion
- Java Hello World Program: How to Write & Run?
- Hello World Java – Your First Java Program Video
- Steps to Compile and Run first Java program
Mastering Java Command Line Run: A Step-by-Step Guide for Windows Users
Learn how to run and execute Java programs through the command line on Windows. Follow our step-by-step guide and pass command-line arguments, navigate to JDK directory, run shell commands, and more.
- Navigating to JDK Directory
- Compiling Java Programs
- How to Compile and Run Java Program from Command Prompt
- Executing Java Programs
- Passing Command-Line Arguments
- Running Shell Commands in Java
- Java on Command Line without an IDE
- Other simple code samples for running Java programs through the command line on Windows
- Conclusion
- Can I run Java project command line?
- How to run Java in command line?
- How to run command line arguments in Java?
Running Java programs through the command line on Windows is a useful skill for developers who want to have more control over the compilation and execution process. This guide will cover the key points, important points, and helpful points to successfully run and execute Java programs through the command line on Windows.
Navigating to JDK Directory
To run a Java program through the command line on Windows, the latest version of JDK must be installed, and its directory should be navigated to. Before starting, make sure that the latest version of JDK is installed on your system, and the PATH environment variable is set to the JDK’s “bin” directory.
To navigate to the JDK directory, follow these steps:
- Open the Command Prompt by pressing Windows Key + R and typing cmd in the Run dialog box, and then press Enter .
- In the Command Prompt window, type cd\ and press Enter . This will take you to the root directory of your system.
- Type cd and press Enter . For example, if the JDK is installed in C:\Program Files\Java\jdk-16.0.1 , type cd C:\Program Files\Java\jdk-16.0.1\ and press Enter .
Compiling Java Programs
Before running a Java program, it must be compiled using the “javac” command. The “javac” command reads the source code file and generates the bytecode file. To compile a Java program through the command line on Windows, follow these steps:
- Navigate to the directory where the source code file is located. For example, if the source code file is located in the C:\Users\yourname\Documents directory, type cd C:\Users\yourname\Documents and press Enter .
- Type javac .java and press Enter . Replace with the name of your source code file. For example, if the source code file is named “HelloWorld.java”, type javac HelloWorld.java and press Enter . This will generate a bytecode file named “HelloWorld.class”.
How to Compile and Run Java Program from Command Prompt
We can compile and run java programs in command prompt ( CMD ) and this video tutorials Duration: 6:27
Executing Java Programs
To execute the compiled Java program, use the “java” command followed by the class or jar file name. The “java” command reads the bytecode file and executes the program. To execute a Java program through the command line on Windows, follow these steps:
- Navigate to the directory where the bytecode file is located. For example, if the bytecode file is located in the C:\Users\yourname\Documents directory, type cd C:\Users\yourname\Documents and press Enter .
- Type java and press Enter . Replace with the name of your class or jar file. For example, if the bytecode file is named “HelloWorld.class”, type java HelloWorld and press Enter . This will execute the program.
Passing Command-Line Arguments
Command-line arguments can be passed to a Java program through the command line. Command-line arguments are passed as strings separated by spaces. To pass command-line arguments to a Java program through the command line on Windows, follow these steps:
- Navigate to the directory where the bytecode file is located. For example, if the bytecode file is located in the C:\Users\yourname\Documents directory, type cd C:\Users\yourname\Documents and press Enter .
- Type java . and press Enter . Replace with the name of your class or jar file, and , , and so on with the command-line arguments. For example, if the bytecode file is named “HelloWorld.class”, and you want to pass two arguments “arg1” and “arg2”, type java HelloWorld arg1 arg2 and press Enter . This will execute the program with the passed arguments.
Running Shell Commands in Java
Java programs can also run shell commands through the “Runtime.getRuntime().exec()” method. The “exec()” method takes a string as an argument, which represents the shell command to be executed. To run shell commands in java through the command line on Windows, follow these steps:
- Navigate to the directory where the Java program is located. For example, if the Java program is located in the C:\Users\yourname\Documents directory, type cd C:\Users\yourname\Documents and press Enter .
- Open your preferred text editor and create a new Java file.
- Add the following code to the Java file:
import java.io.*;public class ShellCommand < public static void main(String[] args) throws IOException < Process process = Runtime.getRuntime().exec(""); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) < System.out.println(line); >> >
Replace with the shell command you want to execute. For example, if you want to execute the “dir” command, replace with “dir”. Save the file as ShellCommand.java .
- Compile the Java file by typing javac ShellCommand.java and press Enter . This will generate a bytecode file named “ShellCommand.class”.
- Execute the Java program by typing java ShellCommand and press Enter . This will execute the program and display the output of the executed shell command.
Java on Command Line without an IDE
Java can be run directly from the command line on Windows without the use of an IDE. To run Java programs through the command line without an IDE on Windows, follow these steps:
- Navigate to the directory where the source code file is located. For example, if the source code file is located in the C:\Users\yourname\Documents directory, type cd C:\Users\yourname\Documents and press Enter .
- Open your preferred text editor and create a new Java file.
- Write the Java code in the text editor and save the file with a “.java” extension. For example, if you want to create a “HelloWorld” program, write the following code in the text editor:
Save the file as HelloWorld.java .
- Compile the Java file by typing javac HelloWorld.java and press Enter . This will generate a bytecode file named “HelloWorld.class”.
- Execute the Java program by typing java HelloWorld and press Enter . This will execute the program and display the output “Hello, World!”.
Other simple code samples for running Java programs through the command line on Windows
In Java case in point, java run cmd code sample
public void excCommand(String new_dir)< Runtime rt = Runtime.getRuntime(); try < rt.exec(new String[]); > catch (IOException e) < // TODO Auto-generated catch block e.printStackTrace(); >>
In Java , run java from terminal
//Run this line to compile javac programName.java//Run this line to run java programName
In Java case in point, java run cmd code example
import java.io.*;public class CmdTest < public static void main(String[] args) throws Exception < ProcessBuilder builder = new ProcessBuilder( "cmd.exe", "/c", "cd \"C:\\Program Files\\Microsoft SQL Server\" && dir"); builder.redirectErrorStream(true); Process p = builder.start(); BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream())); String line; while (true) < line = r.readLine(); if (line == null) < break; >System.out.println(line); > > >
In Kotlin , for example, java run cmd command
fun excCommand(cmd: String) < val rt = Runtime.getRuntime() try < rt.exec(arrayOf("cmd.exe", "/c", cmd)) >catch (e: IOException) < // TODO Auto-generated catch block e.printStackTrace() >>
In Java case in point, run java in cmd code sample
C:\Users\Your Name>javac MyClass.java
In Java , for instance, run java application in commande line code sample
java -jar target/spring-cloud-gateway-keycloak-oauth2-0.0.1-SNAPSHOT.jarjava -jar target/product-service-0.0.1-SNAPSHOT.jar
Conclusion
running Java programs through the command line on Windows requires navigating to the JDK directory, compiling the program, executing it, passing command-line arguments, and running shell commands. Although it has a steeper learning curve than using an IDE, it provides a better understanding of the compilation and execution process. Best practices include properly structuring code and using version control, while common issues may arise due to incorrect syntax or missing dependencies. With this step-by-step guide, you should be able to master Java command line run on Windows and take your development skills to the next level.
Java Hello World Program: How to Write & Run?
In this Java Hello World example, we’ll use Notepad. It is a simple editor included with the Windows Operating System. You can use a different text editor like NotePad++ or use online java compiler.
Hello World Java – Your First Java Program Video
This video will help you learn how to start a Java program:
Click here if the video is not accessible
Steps to Compile and Run first Java program
Here is a step by step process on how to run Java program:
Step 1) Open Notepad from Start menu by selecting Programs > Accessories > Notepad.
Step 2) Create a Source Code for your Hello World program in Java
- Declare a class with name A.
- Declare the main method public static void main(String args[])
- Now Type the System.out.println(“Hello World”); which will print Hello World in Java.
Step 3) Save the file for Java Hello World program as FirstProgram.java make sure to select file type as all files while saving the file in our working folder C:\workspace
Step 4) Open the command prompt. Go to Directory C:\workspace. Compile the code of your Hello world Java program using command,
Step 5) If you look in your working folder, you can see that a file named A.class has been created.
Step 6) To execute the code, enter the command java followed by the class name, as expected output Hello World is displayed now.
Note: Java is case sensitive Programming language. All code, commands, and file names should is used in consistent casing. FirstProgram is not same as firstprogram.
Step 7) If you copy and paste the same code in IDE like Eclipse the compiling and execution is done with the click of a button Using IDE is convenient and improves your efficiency but since you are learning Java, we recommend you stick to notepad for simple Java program execution.