banner



How to Use Java in Command Prompt Windows 10

In this Java tutorial, you will learn how to write Java programs in Notepad and run them using the command prompt (cmd) or terminal. Now that you have installed the Java Development Kit on Windows, let us go ahead and write a simple program in Java.

While there are several editors available, in the beginning, you do not need to install any fancy editors for working in Java.

Table Of Contents

  • How To Write Java Programs In Notepad
  • How To Run Java Program In Command Prompt or cmd
    • How To Add Java JDK Bin Folder To Windows Path
    • How To Compile Java In Terminal or Command Prompt
    • How To Run Java Program In Command Prompt (cmd)
  • First Java Program – Explained Line By Line
      • Lines 1 to 4
      • Line number 5
      • Line number 6
      • Line number 7
      • Line number 8
      • Line number 9
      • Line number 10
      • Line number 11
      • Lines 12 and 13
  • Importance of Code Comments in Java
      • Other popular articles:

How To Write Java Programs In Notepad

Open Notepad.exe in Windows or any text editor on your operating system.

Type the below code into Notepad. Save the file with the file name HelloWorld.java

/*

  This is a simple class called

  HelloWorld in a file HelloWorld.java

*/

class HelloWorld

{

//This program starts with a call to "main" method

public static void main ( String args [ ] )

{

//The below line prints a greeting on the console

System . out . println ( "Hello! I'm a Try QA program" ) ;

}

}

  • In Java, the source file is a compilation unit and the name given to the source file is quite important
  • The file name must have a .java extension for the Java compiler to compile the file
  • A single source file can contain multiple classes (which we will see later)
  • Since Java is case sensitive, the capitalization of the class name should match the file name
  • The file name should match the class name

How To Run Java Program In Command Prompt or cmd

The best way to learn programming is by running the program. In this Java tutorial we will first look at how we can run the Java program on Windows and after that we will go through the code to understand it. The steps below explain how to compile and run Java program using the command prompt or cmd.

  1. Before we can run the program we have to compile the Java program.
  2. Compiling the program converts the code from a human readable format which we entered in Notepad, into a "bytecode" which is executed by the Java Virtual Machine (JVM).
  3. Open the command prompt by pressing down the Windows key and R key. This will bring up the Run dialog box.How To Run Java Program In Command Prompt cmd
  4. Type cmd in the dialog box and click the Ok button. This will bring up command prompt.
  5. Another way to bring up the command prompt is to right click your mouse on the Start button on the Taskbar and choose Command Prompt from the menu which is displayed. This will also display the command prompt.How To Run Java Program In Command Prompt cmd
  6. In the command prompt, navigate to the folder where you saved the HelloWorld.java file. In this example, the file is stored in the d:\tryqa\java folder. You can change the drive by typing the command D: and pressing enter. You can then go to the folder using the command cd d:\tryqa\java and pressing enter.How To Run Java Program In cmd
  7. The command to compile the HelloWorld.java program is javac HelloWorld.java If this is your first time compiling a Java program, you will most probably get an error if you run this command, as shown below.javac not recognized as internal or external command
  8. This is because Windows does not know where to look for the Java compiler program, javac. This can be fixed by adding the Java's bin directory to the system path as shown in the section below.

How To Add Java JDK Bin Folder To Windows Path

  1. Open Windows Explorer by pressing Windows + E key or double click on My Computer
  2. Right click on the computer icon in Explorer and click on Properties in the pop up menu, as shown belowHow To Add Java JDK Bin Folder To Windows Path
  3. This will display your system properties. Click on the Advanced system settings link on the left hand side menu.How To Add Java JDK Bin To Windows Path
  4. This will open the System Properties dialog. Click on the Environment Variables button near the bottom.How To Add Java JDK Bin Folder To Windows Path
  5. This will open the Environment Variables dialog. The system variables are listed in the lower group in the dialog box. If the Path variable is not visible, use the scroll bar to scroll down the list of System variables, to bring the Path variable into view, as shown in the screenshot below.How To Add Java JDK To Windows Path in Envrionment Variable
  6. Click on the row containing the Path variable, to select it and then click the Edit button under System variables. If for some reason, your system does not have a Path variable, click on the New button under System variables.How To Add Java JDK To Windows Path in Envrionment Variable
  7. Assuming the Path variable exists on your system, the Edit System Variable dialog is displayed when you click the Edit button. Here the Variable value field contains the current system path. We need to add the folder path of the Java compiler executable files to the existing path. Values in the path variable are separated by semicolon ";"
  8. The Java compiler executable files are usually in a folder called bin under the JDK directory where you installed the Java Development Kit (JDK). In this example the Java JDK bin directory is present at C:\Program Files\Java\jdk1.8.0_201\bin In case you installed a 32 bit version of Java, the Java folder may be present under C:\Program Files (x86)\ instead of C:\Program Files\. The JDK folder name under the Java directory will vary depending on the version of Java Development Kit (JDK) installed on your machine.
  9. We need to add the above path to the end of the variable value of Path. First we will add the separator semicolon ";" followed by the path of the JDK binary folder as shown below. Some people prefer to copy the original content of the Path variable into a text file and add the Java JDK path there, before pasting the final string back into the Edit System Variable dialog.Add Java JDK Bin Folder To Windows Path Env
  10. Click the Ok button on the Edit System Variable dialog, the Environment Variables dialog and System Properties dialog. An example of updated value of Path variable is given below. This may vary from machine to machine depending on the software installed on the machine.

% SystemRoot % \ system32 ; % SystemRoot % ; % SYSTEMROOT % \ System32 \ WindowsPowerShell \ v1 . 0 \ ; C : \ Program Files ( x86 ) \ ATI Technologies \ ; C : \ Program Files \ Java \ jdk1 . 8.0_201 \ bin ;

How To Compile Java In Terminal or Command Prompt

  1. Close the previously opened command prompt and open a new command prompt
  2. Switch to the folder where the HelloWorld.java file is located, in the command prompt
  3. Java source code is compiled using the command javac <filename.java>
  4. Type javac HelloWorld.java and press enter. The program is compiled and the user is returned to the command prompt.How To Compile Java Program In Command Prompt cmd
  5. Compiling the program generates a .class file with the name of the class, for every class in the source file. In this case, the file HelloWorld.class is generated by the compiler.How To Compile Java Program In Command Prompt cmd
  6. In the next step we will run the compiled program

How To Run Java Program In Command Prompt (cmd)

  1. Java programs are run using the command java <class name>
  2. In this example, the class name is HelloWorld, type java HelloWorld and press Enter
  3. The Java Virtual Machine (JVM) looks for a file matching the class name with the extension .class
  4. The JVM executes the program and displays the output "Hello! I'm a Try QA program" as shown below.How To Run Java Program
  5. Once the execution of the program is complete, the user is returned to the command prompt.

First Java Program – Explained Line By Line

Let us go through above example line by line to understand the example better.

Lines 1 to 4

/*

  This is a simple class called

  HelloWorld in a file HelloWorld.java

*/

The program begins with a multi line comment from lines 1 to 4, which describes the program. Multi line comments are enclosed between "/*" and "*/" as shown here. The Java compiler ignores the contents within a comment. Comments are used to describe the code to the reader.

Line number 5

On line number 5, we are declaring the class. A class is declared using the keyword class followed by the class name as shown below. The class contains the code to be executed. In this example, HelloWorld is an identifier which is the class name.

Line number 6

The scope or boundary of the class is defined using opening and closing curly brackets (also called curly braces or flower brackets in some countries) "{" and "}". Line number 6 contains the opening curly brackets for the class. Curly brackets added after the class name indicate where the scope or boundary of the class begins.

Curly brackets are also used to define scope / boundary for methods and other types of blocks, which we will see later.

Line number 7

//This program starts with a call to "main" method

After this, we have a single line comment in the program on line number 7, which is shown here. Single line comments are used to enter a single line of comment. In Java these are denoted using "//" as shown below. While the use of comments in this example may seem unnecessary, comments are extremely useful to document and communicate implementation details etc in real time projects.

Line number 8

public static void main ( String args [ ] )

The main method begins on this line. When we run a Java program, it begins by calling the main() method. We will be using the main method in all our programs, so it is good to know the syntax of the main method.

However, at this point we will not go very deep into the details of each part of the syntax since its requires a good understanding of Java, we will briefly explain the parts here.

  • public – The word public is an access modifier keyword used to denote the visibility. Adding public to a class member tell the JVM that it can be accessed from outside the class, in which it is declared. The main method is called by code outside its class so it is declared as public.
  • static – The static keyword is used indicate that the member can be accessed without creating an instance of the class. The JVM calls the main method before any objects are created so we need the main method to be static.
  • void – void is used to indicate that the main method does not return any value. We will see other return types in future when methods return a value.
  • main – main is the method which is called when Java application is executed. Java is case sensitive so Main is different from main. The main method is required to run the program. Java compiler will also compile classes without the main method, however they cannot be executed. So if you make a mistake and type Main instead of main, in your program, the program will compile but will give an error when you try to run it since it would be unable to find the main method.
  • String args[] – There are set a brackets "(" and ")" following the name of the method called main. Information that has to be passed to the method are received by variable within these parenthesis / brackets which are present after the method name. There are called parameters. Empty parenthesis are still required even if there are no parameters in a method. The main method has only one parameter where String args[] declares a parameter called args which is an array of objects of type String. String objects store a string of characters and here, arrays are a collection of similar objects. In case of the main method, args is used to receive command line arguments when the program is executed. In this example the args parameter is not being used.

Note: Complex programs may have multiple classes within one file and only one of them may need a main method. Sometimes, you may not have any main methods at all, for example in web applications, the application server will use a different mechanism to execute the programs.

Line number 9

The curly brackets "{" on line number 9 which are found after the method name, indicate the beginning of the method body. All code which belongs to a method will be present between the method's opening and closing braces.

Line number 10

//The below line prints a greeting on the console

This is a single line comment used to describes the statement below it.

Line number 11

System . out . println ( "Hello! I'm a Try QA program" ) ;

This statement is used to display the text – Hello! I'm a Try QA program – on the console, followed by a new line. This is accomplished using the method println() which displays any string or other information, passed to it.

This statement is a little complex to explain in the first tutorial but in short – in the above statement, System is an in-built class which provides access to the System and out is a member variable which provides console access through an output stream. The method println() is called on the out variable.

The above statement ends with a semicolon ";" The semicolon is used to end all Java statements.

The other lines in the program are not statements, so they do not need to end with a semicolon.

Lines 12 and 13

The curly brackets on line 12 is used to close the main() method. The curly brackets on line 13 are used to close the class definition for the class HelloWorld.

Comments are used to describe classes, methods, statements or any piece of code. It is extremely important to comment your source code in real life projects.

Comments may be used to describe the purpose of a class or classes, what a method is expected to do or how the logic is being implemented in a piece of code etc. This is important because it helps maintaining the code over a period of time.

In live projects, there is usually more than one person working on the project at any time.

People may be asked to work on code written by you or you may be asked to work on a program written by someone else. Among other things, code comments are useful in explaining how the logic is being implemented in a method or piece of code.

Even if you have written the logic yourself, there will be times when you will not be able to recall why you implemented the functionality in a certain way, after  6 to 8 months. Code comments can be used to explain to the reader how / why the logic is being implemented in a certain way.

In the next topic we will look at Data Types, Variables & Identifiers in Java

Other popular articles:

Reader Interactions

How to Use Java in Command Prompt Windows 10

Source: http://tryqa.com/how-to-write-run-first-java-program-cmd-notepad/

0 Response to "How to Use Java in Command Prompt Windows 10"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel