Writing and executing first java program through CMD in windows, Setting up ECLIPSE in java, Directory of .class file



Contents of page :
  • 1) Writing and executing java program through CMD in windows >
  • 2) Setting up ECLIPSE, writing and executing first program in eclipse.
    • Where the .class file is formed, when execution is done using eclipse?


Before going through this tutorial you must Download and install JDK, Setting environment variables JAVA_HOME & JDK PATH temporarily and permanently in windows, What is a path? need to set PATH variable?


1) Writing and executing java program through CMD in windows >
Let’s write first class >
public class FirstProgram {
   public static void main(String[] args) {
          System.out.println("This is my first Java Program");
   }
}


Let’s say above code in file =  E:\workspace\FirstProgram.java


Go to CMD and type below commands >
C:\Users\ankitmittal01>e:

E:\>cd E:\workspace

E:\workspace>javac FirstProgram.java

E:\workspace>java FirstProgram
This is my first Java Program

E:\workspace>


As soon as javac FirstProgram.java is called .class file is formed. (.class file contains bytecode)


Directory of .class file = E:\workspace\FirstProgram.class
when java FirstProgram is called it executes the .class file and output of program “This is my first Java Program” is printed in CMD.


CMD snapshot >

In short about program execution >
At compile time > Compiler converts .java file into .class file (bytecode), then,
At runtime > .class file is executed by JVM.

      • For compiling one file  (As done above)
      • For compiling more than one files >
Enter name of all java files in a file (separated by blank or line breaks(next line) ).  Then enter javac @ fileName
Example -
Let’s say we have three java files  
E:\MyClass1.java  
E:\MyClass2.java
E:\MyClass3.java


Enter name of  name of all java files in a MyFile separated by blank.


E:\MyFile will look like this >
E:\MyClass1.java  E:\MyClass2.java  E:\MyClass3.java


C:\Users\ankitmittal01>e:

E:\>javac @MyFile

E:\>



2) Setting up ECLIPSE, writing and executing first program in eclipse.

Download and install eclipse (I’ll recommend you to download Eclipse IDE for Java Developers)



Just unzip the downloaded files and open eclipse.exe


Eclipse will ask you to set up workspace directory.
Select Directory, let’s say you selected directory = E:\workspaceTest\MyFirstProject

Creating new project >
Click File, New, Java Project



Give name to your first project as - MyFirstProject (as per your choice) and click Finish


Creating new package in MyFirstProject>
Right click on MyFirstProject, Click New , Package.


Creating new class in MyFirstPackage>
Right click on MyFirstPackage, Click New , Class.


New class will be created and write your first java program >
package MyFirstPackage;
public class FirstProgram {
   public static void main(String[] args) {
          System.out.println("This is my first Java Program");
   }
}


Now, let’s execute our first java program.
Click on circled Run button. And you will be able to see output in Console.


Alternatively, you might execute program by right clicking on java file, Run As, Java Application.


Where the .class file is formed, when execution is done using eclipse?


.class file is formed in directory = E:\workspaceTest\MyFirstProject\bin\MyFirstPackage\FirstProgram.class
(Directory location may differ based on >
selected workspace location
project, package and class names
)


Additionally you must know, FirstProgram.java was formed in directory =
E:\workspaceTest\MyFirstProject\src\MyFirstPackage\FirstProgram.java


/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */


RELATED LINKS>

Java tutorial - Advantage, Where is java used in real world, platform independent language



Interface in java - Multiple inheritance, Marker interfaces, When to use interface practically, 12 features



Abstract class in java - When to use abstract class or interface practically, 10 features



Constructor in java - Constructor chaining, access modifiers with constructors, constructor overloading, exception thrown, constructors are not inherited


No comments:

Post a Comment