How to Use the CMD to Launch a Java Application? Check Here for All Updates!
When it comes to computer programming, Java is now in high demand. It finds applications in many other areas, including software engineering, data science, etc. The code for this language is written in an integrated development environment (IDE), much like code for any other language (Integrated Development Environment).
However, not all users are comfortable with the idea of downloading an external program in order to write code; others may find it more convenient to use a program already installed on their computers, such as Microsoft Word or Notepad.
While these may facilitate coding, they are not suitable for executing the code. If you’re having trouble here, you might want to try the Command Prompt. This article will show you how to use the Command Prompt to execute a Java program.
Although we’ll be using Command Prompt as our example, this feature is also available in Terminal on macOS. Mac and Windows users follow identical steps. You must first install a specialized Java compiler on your machine. The Java Development Kit (JDK), the primary toolset for creating Java programs, is available for free download from Oracle’s site. A prerequisite to getting started is having the JDK already installed on your machine.
How to Run a Java Program Using Command Prompt
The first step is to create the executable Java file containing the code you wish to launch. You can use either Microsoft Word or Notepad for this. In this tutorial, for instance, you will learn how to write some basic code that prints “Hello World!” as its result. To summarise the code, it would look like this:
Read More: How To Convert To An SVG File and What Can You Do With An SVG File?
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
Users must check that the file is saved as a ‘.java’ file and not as a ‘.txt’ file, and ensure that the indentation is correct, as neither Notepad nor Word can assist with this. Name your file “SampleFile.java” to get started.
(Given that the saved file ends in.java and the ‘All Files’ option is selected under File type, the file’s name can be anything you choose. Save this document to a new folder in your preferred location.
To do this, launch the Command Prompt and right-click to run it in administrative mode. With the ‘cd’ command, we may tell the Command Prompt to run your Java program’s installation directory. Try the following command line:
cd Folder-destination[Java-program-folder]
The Dir command can also be used to verify the presence of your Java program file in this location. Get the JDK’s location on your machine and enter it into the following command line. For instance, unless you modify the default installation path during setup, the 64-bit JDK file will be placed in “C: Program FilesJava” if you’re using Windows and you’ve decided to install it.
set path=%path%;C:\Program Files\Javajdk
You must now build your code by issuing the javac command (Java compile).
javac SampleFile.Java
After deciding on a filename for your application, you must then add the javac keyword. Nothing will happen just yet because the program has merely been compiled and not run. The Java command is then used to execute the command.
java SampleFile
You don’t need to define a permanent path for your files if you’re only creating small applications and keeping them all in the same directory. When you use the Command Prompt to alter an environment variable, that change will take effect for the duration of your session but will be lost when you exit the prompt.
As such, the following procedures can be used to establish a permanent course for all subsequent sessions:
The System and Security menus can be accessed by opening the Control Panel. Checking the category view in the Control Panel should help you find what you need. A second route to this page exists through the Windows control panel. Click the Environment Variables button at the bottom of the System Properties box after selecting Advanced System Settings from the left-hand menu.
Read More: What Are the Best Parts of Mac OS Monterey, and What Are the Drawbacks?
The Path variable can be found in System Variables. Choose it and then hit the Edit button that appears.
To add a new directory to your path, go to the Edit Environment Variables box and click on New.
Copy the path you used to compile the Javascript earlier and put it here, then click Ok to save your changes.
To compile and launch a Java program in the command prompt, you must follow these procedures. To clarify, most Java programmers who are reading this page will already have the Java Runtime Environment installed and operating on their PCs, but users will need the Java Development Kit for the method.
Oracle.com provides a download link for the JDK. If you found any value in this article, that’s great!