Java In CMD: Running A Java Program In CMD Using Notepad

Photo of author

Any programming language, whether C, C++, Java, or Python, has different methods of writing and running code.

If they came into being via tools like VS Code, they might be eligible to be run and debug there on the same platform. With alternative techniques, the same thing is also possible. The complete code block can be put into notepad and run using cmd as one method. (command prompt). The required extension files for that specific programming language must be downloaded or installed in order for this technique to function.

java program in notepad

Let’s run a java program in cmd using notepad. Prior to beginning the complete process of executing a Java program, you must check that the correct recent JDK file exists in your system.

Change the environment variable in addition to setting the proper path.

Steps to Run Java program in cmd using notepad

Your Java program will only function if you notice these minor details.

Step 1: 

On your computer, launch ‘Notepad.’ You can do this directly by either typing “Notepad” into the search window or by using the keyboard shortcut ‘Windows Key + R,’ typing notepad and then hitting the ENTER or OK button.

notepad

Any program’s code that needs to be run should be written down and put into an application like notepad. Ensure that your code is free of bugs or errors for smooth compilation and faster execution.

Step 2:

Save the java program and provide it with a real relatable file name. Remember that the saved file name should be the same as the class name used in the program, followed by the extension.java.

Hypothetically stating,

Name of the file: helloworld.java

Location of the saved file: C:\Users\Desktop\Java\CS

Step 3:

You’ve now successfully written Java code in the Java program that will run and remain in a specific area on your computer after completing the previous steps. The following steps will involve the compilation and debugging of code. That will lead to a successful run of code.

java code on notepad

Open the Command prompt window(cmd) in your computer by either searching it directly on the search bar or by pressing Windows Key + R, typing cmd, and pressing the ENTER key or OK button.

Step 4: 

command prompt

As soon as the cmd window opens up on your desktop, you have to make sure that there are no already running commands on the same. As this would increase the possibility of a significant error. For this, type “cls” and then press enter. This would ensure everything is clear, and you are good to go.

Step 5: 

Now, in the Command Prompt(cmd) window, write the following commands.

  1. cd\
  2. cd Users\Desktop\Java\CS

Writing this will ensure you put the folder where your respective java code is save.

Step 6:

As you have now entered the folder, type the following code to compile your code written in java program:


javac helloworld.java

The code you wrote shall have successfully gone through compilation if there are no mistakes shown in it. Later, a.class file with the same name as the.java file got at the precise position by the system is to be.

Step 7: 

Now that the helloworld.class file was able to save and download, it can run and read. To run the Java program, type the following command:


java helloworld

You have thus effectively created java code and saved it to notepad. Following that, debugging, execution, and output display were all done using the cmd function.

See also: How To Download JRE For Windows?

FAQs

How do I run a Java program in Notepad?

Running a Java program in Notepad requires saving the code with a .java extension. Open CMD, navigate to the file's directory, compile using 'javac filename.java,' then run using 'java filename.'

How to run a class file in CMD Java?

To run a Java class file in CMD, open CMD, navigate to the file's location, and use the 'java class name' command without the .class extension.

How to run Eclipse Java file in CMD?

Export the Java project as a JAR file in Eclipse. Then, open CMD, navigate to the JAR's location, and run it using 'java -jar filename.jar.'

How to run a JDBC program in CMD?

Compile the JDBC program with necessary libraries using 'javac,' then run it by navigating to the class file's location and executing 'java class name.' Ensure the JDBC driver is in the classpath.

Conclusion

The above summary of the entire article outlines the procedures for writing code in any programming language (java program in this case), debugging it, saving it, and finally running it on the command prompt window. It’s advisable that you download and retain the most recent JDK version on your computer.

See Also: How To Check If File Exists Python

Leave a Comment