Friday, July 12, 2024

Different ways to run Java Program

 

Different ways to run Java Program:

1. From Eclipse:

2. Through Command Prompt Window:

1. From Eclipse:

  • Write your Java program in Eclipse.
  • Right-click on your Java file or class containing the main method.
  • Select Run As > Java Application.

(or)

In Menu Items (under 'navigate' or 'Project' menu) >click  'play button (or) Run button' >  it runs java program

 

1.      Through Command Prompt Window:

Creating a Java File in Notepad:

Steps to create a Java file in Notepad:

  1. Open Notepad.
  2. Type your Java program.
  3. Go to the File menu.
  4. Select Save As.
  5. Give the file name the same as the class name with the extension .java (e.g., SampleJava.java).
  6. Click Save.

This creates a SampleJava.java file.

 

 

 

cmds :

1. compile java program:

syntax :

  javac javafilename.java

  javac SampleJava.java

 

 

C:\brahma\Practise\SelniumPractiseNew\JavaPrograms>javac SampleJava.java

SampleJava.java:7: error: unclosed string literal

 System.out.println("Hi Java')

                    ^

1 error

 

 System.out.println("Hi Java")

 

C:\brahma\Practise\SelniumPractiseNew\JavaPrograms>javac SampleJava.java

SampleJava.java:7: error: ';' expected

 System.out.println("Hi Java")

                              ^

1 error

 

 

Compilation Process:

The compiler checks all errors in the program.

If there are any errors, it displays all error details.

 If there are no errors:

  1. Sample.java is compiled to Sample.class (contains bytecode instructions).
  2. The Sample.class file is then passed to the JVM (Java Virtual Machine) to run/execute the Java program.

 

 

 Sample.java   to  >   Sample.class  ( contains byte code instructions) --> JVM ( Run / execute java programs)

>   to low level lang (or) Machine level lang ( 01010) -->  Processor (or) system >    executes the instruction

> display the o/p on console window

 

Once we compile the Java program, it creates a class file with the same name as the class name, containing bytecode instructions. The JVM translates this bytecode into machine language, which the system or processor understands. The system then executes the program and displays the output.

 

FAQ: What is JVM?

  • JVM (Java Virtual Machine): Converts bytecode instructions to machine language and passes them to the processor. The processor executes the instructions and displays the output on the console window.
  • The JVM is used to run Java programs.

 

2. run java Program :

-----------------------------

Syntax: cmd :  java javafilename

 

C:\brahma\Practise\SelniumPractiseNew\JavaPrograms>    javac SampleJava.java

 

C:\brahma\Practise\SelniumPractiseNew\JavaPrograms>java SampleJava

Hi Java

 

C:\brahma\Practise\SelniumPractiseNew\JavaPrograms>java SampleJava

Hi Java

Change the code : HiRam

C:\brahma\Practise\SelniumPractiseNew\JavaPrograms>javac SampleJava.java

 

C:\brahma\Practise\SelniumPractiseNew\JavaPrograms>java SampleJava

Hi Ram

 

C:\brahma\Practise\SelniumPractiseNew\JavaPrograms>

 

Note : 

For every Java program, we must first compile and then run the program.

 If you modify some code or add 2 or 3 lines of code, you must first compile and then run the Java program.

Compilation and Running Commands:

  1. Compile Command:

javac filename.java

2.      Run Command:

java filename

Common Errors:

  • Using java Filename.java results in an error. Do not include the .java extension.
  • Using java Filename.class results in an error. Do not include the .class extension.

Example Errors:

C:\brahma\Practise\SelniumPractiseNew\JavaPrograms>java SimpleJava.class

Error: Could not find or load main class SimpleJava.class

Caused by: java.lang.ClassNotFoundException: SimpleJava.class

 

C:\brahma\Practise\SelniumPractiseNew\JavaPrograms>java SimpleJava.java

error: class found on application class path: SimpleJava

 

HW Create 2 Programs in notepad and compile and Run it?

   Sample2.java

   Sample3.java

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...