Thursday, 22 September 2016

What is V8 JavaScript Engine?

It is JavaScript Engine developed for web browser Google Chrome .
             JavaScript Engine is Program or Library that helps to compile and execute JavaScript code.
It mainly used in Web Browsers.

Examples of JavaScript Engines-
V8- Chrome
Rhino -Firefox
Chakra - Microsoft IE 

V8 complies JavaScript code to machine code before executing it.

Traditional Approach is compiling whole program to machine code before executing it.
Instead V8, optimizes complied code at runtime.


Applications -
NodeJs, MongoDB, and Couchbase use V8 engine

Uses ->
Client Side Applications - Chrome (All Browsers)
Server Side Applications - NodeJs

**Advantages -
1.It is much faster than its peers because it compiles code directy to machine code without generating byte code(intermediate code).
2.It uses 2 compilers to compile code one after another.Second run compiler mainly optimizes main functions.

What is ECMAScript (ES) and how it is related to JavaScript (JS) ?

ECMA stands for European Computer Manufacturer's Association.
ECMAScript or ES is a standard for a scripting language given by ECMA.
It specifies the core features that a scripting language should provide and how those features should be implemented.

ES has various versions-
ES4- Abandoned
ES5- launched in 2011
ES6- launched in 2015
ES7- Finalized in 2016.

Whereas,
JavaScipt or JS is the most popular implementations of the ECMAScript Standard.

Note**- Every browser has a JavaScript interpreter.

Imp Implementations of ES ->
1. JavaScript V8 Engine.  - Engine used by Google Chrome to compile and interpret ES.
2. Jscript - used by Microsoft Internet Explorer (compiles and interprets ES )
3. JavaScript

Tuesday, 13 September 2016

Using Sockets

Sockets are used to communicate 2 different processes  on same or different machines.

Application ->
For Eg.
Suppose a JavaScript Application want to send Message to Java Application(JAR file.)
Hence we can communicate JavaScript and Java Application using sockets.

Also, If processes are running on different machines then socket is used as a common ground to communicate both processes.

How to use ->
Socket has 2 parts ->
1.Client -> App which sents/request data to another app
2.Server -> App which processes/responds to sent request.

In Order to make Socket Object we need IP and Port.

Here I am using an example in which client sends 2 no a and b to server. Server then adds them and returns the result.

I am using nodeJs and Javascript  

Client Part (Client.js)
Client selects IP and PORT to establish Socket.
    
var JsonSocket = require('json-socket');//Client Details
var HOST = "172.16.207.151";
var PORT = "6969";
//same port where server is listening...
// Make a sokcet with JSONSocket
var socket = new JsonSocket(new net.Socket());
//Create instance of server and wait for connection
socket.connect(PORT, HOST);
console.log('Server listening on ' + HOST + ':' + PORT);

socket.on('connect', function () {
//Don't send until we're connected
//On connection sending no a and b to server to multiply.
    socket.sendMessage({ a: 5, b: 7 });
   
//on getting result from server print it.
    socket.on('message', function(message) {
        console.log('The result is: '+message.result);
    });
});








Server Part (Server.js)
Sever only needs to listen to port where client is sending data

var net = require('net');
JsonSocket = require('json-socket');
var PORT = "6969";
var server = net.createServer();
server.listen(PORT);
server.on('connection', function(socket) {
//This is a standard net.Socket
    socket = new JsonSocket(socket);
//Now we've decorated the net.Socket to be a JsonSocket
    socket.on('message', function (message) {
        console.log("Message Received on server");
        var result = message.a + message.b;
        socket.sendEndMessage({result: result});
    });
});
How to test?
First run server.js and then client.js
output: 
on Client Side->
Server listening on 172.16.207.151:6969
The result is: 12

on Server side->
Message Received on server
 

I Hope this helps.


Difference between noClassDefFoundError and ClassNotFoundException

Before discussing the difference lets talk about their similarities.
1.Both occur because of  the unavailability of class at runtime at classpath.

Differences -
1.noClassDefnFoundError is error and cant be handled while ClassNotFoundExceptionis exception and can be handled.

2.The exception occurs when we try to load class at runtime using
Class.forName()  or
ClassLoader.loadClass()

In short,
The difference is the root cause of the problem->

ClassNotFoundExcpetion occurs when you try to load a class at runtime by using Class.forName() or loadClass() and requested class is not present in classpath  
Eg.
Your are trying to load jar file and that jar file is not found in classPath.

 noClassDefFoundError occurs when requested class was present at compile time but not available at runtime.
 This happens because of exception during class Initialization.

How to test?
Step1- Create below files -
1. Test.java -->
 public class Test {}
2.Test1,java-->
public class Test1{
Test test = new Test();
}

Step 2.Now compile Test1.java
javac Test1.java
 2 class files will be generated Test.class and Test1.class

Step3. Now delete Test.class file and run Test1.java

noClassDefnFoundError occurs.
 

Monday, 12 September 2016

Javac is not recognised as internal or external command...

Whenever we try to compile .java file to .class file we often get this message.
It means that java compiler (javac.exe) is not found by OS in the current path.

So we need to tell OS where we have stored javac.exe.

Hence, Execute below command
set PATH ="C:\Program Files (x86)\Java\jdk1.8.0_31\bin"
where bin folder contains javac.exe

Now OS knows where javac is present and it will compile the java files successfully.

Difference between program files and program files(*86) on windows OS?

A 64 bit windows OS has 2 different but similar folders
1.Program Files - which contains 64 bit applications
2.Program Files(*86)- which contains 32 bit applications.


This is because, windows run both 32 bit and 64 bit architectural programs on 64 bit OS.
I do not know why it does not use all 64 bit programs on 64 bit OS ?


In short, 32 bit architectural programs are stored under Program Files(*86) and
64 bit architectural programs are stored under Program Files.

How to create jar file from Java Project and Run using Command Prompt?

I am using eclipse.

Create Java Project and add few classes to it.
Now a jar file can be exported as JAR or Runnable JAR.

The Difference between JAR and Runnable JAR is that the later has Entry point( Main Class) defined in it so that if we run the jar from command Prompt  it gets executed.

Steps ->
1.Right click on project and select Export 
2.Select Runnable JAR option and Click Next
3.Now select Entry Point in Launch Application Option. (Eg. Main Method)
4.Save .

Now your JAR file is created.
Each JAR file has  META-INF/MANIFEST.MF file which has default settings.
But when we create jar File as Runnable JAR entry point gets created in MANIFEST.MF file
as Main-Class:PackageName.ClassName  
Eg. Main-Class:com.tush.pat.TestClass

Whenever entry point is not mentioned in class add above line to it and then run .

Note**-
Whenever we create jar file simply as JAR we often miss to mention entry point in jar .
Hence jar file first has to be made runnable and then run.

To avoid it we can directly create runnable jar.

How to run ?
1.Open cmd and cd to jar file location.
2. java -jar abc.jar    
 where abc.jar is our jar file

It will execute the jar file.