Wednesday, May 31, 2017

How to download file/folder from Linux Server to Mac's Desktop or any location ?






Download a directory from the Linux Server

scp -r <user_name>@<host_ip_address>:<PATH_OF_DIRECTORY_TO_DOWNLOAD>  <PATH_ON_LOCAL_DESKTOP>

Enter Linux Server Password:




Download a file from the Linux Server

scp <user_name>@<host_ip_address>:<PATH_OF_FILE_TO_DOWNLOAD>  <PATH_ON_LOCAL_DESKTOP>

Enter Linux Server Password:

Thursday, April 27, 2017

How to make Apache Tomcat as service in Windows 7 ?



How to make Apache Tomcat as service in Windows 7 ?

Go to the tomcat location c:\apache-tomcat-7.0.77\bin and write command service.bat install as
show in the below picture.





2. You will see the 'Tomcat X' has been started.

Note: X is the version of your Tomcat.






3. Monitor your installed Tomcat service.

c:\apache-tomcat-7.0.77\bin>tomcat7w.exe //MS//Tomcat7




You will see below dialog,






How to set+modify Java option ? or How to set+modify PATH system variable ?

How to set/modify the PATH system variable?


This post applies following OS's :

Windows 7, Windows 8, Windows Vista, Windows XP

Macintosh OS X, 

Oracle Linux, 

Red Hat Linux, 

SUSE Linux, 

Solaris SPARC,




Information for All

  • The PATH is a system variable that your operating system uses to locate needed executables from the command line or Terminal window.

  • The PATH system variable can be set using System Utility in control panel on Windows, or in your shell's startup file on Linux and Solaris.

  • Making changes to the system PATH variable is typically not necessary for computers running Windows or Mac OS X.

Windows

Windows 10 and Windows 8

  1. In Search, find for and then select: System (Control Panel)
  2. Click the Advanced system settings link.
  3. Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New.
  4. In the Edit System Variable (or New System Variable) window, specify the value of the PATHenvironment variable. Click OK. Close all remaining windows by clicking OK.
  5. Reopen Command prompt window, and run your java code.
Windows 7

  1. From the desktop, right click the Computer icon.
  2. Choose Properties from the context menu.
  3. Click the Advanced system settings link.
  4. Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New.
  5. In the Edit System Variable (or New System Variable) window, specify the value of the PATHenvironment variable. Click OK. Close all remaining windows by clicking OK.
  6. Reopen Command prompt window, and run your java code.
Windows Vista

  1. From the desktop, right click the My Computer icon.
  2. Choose Properties from the context menu.
  3. Click the Advanced tab (Advanced system settings link in Vista).
  4. Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New.
  5. In the Edit System Variable (or New System Variable) window, specify the value of the PATHenvironment variable. Click OK. Close all remaining windows by clicking OK.
  6. Reopen Command prompt window, and run your java code.
Windows XP

  1. Select Start, select Control Panel. double click System, and select the Advanced tab.
  2. Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New.
  3. In the Edit System Variable (or New System Variable) window, specify the value of the PATHenvironment variable. Click OK. Close all remaining windows by clicking OK.
  4. Reopen Command prompt window, and run your java code.

Mac OS X

To run a different version of Java, either specify the full path, or use the java_home tool:
% /usr/libexec/java_home -v 1.8.0_73 --exec javac -version

Solaris and Linux

  1. To find out if the path is properly set:
    In a terminal windows, enter:
    % java -version
    This will print the version of the java tool, if it can find it. If the version is old or you get the error java: Command not found, then the path is not properly set.
  2. Determine which java executable is the first one found in your PATH
    In a terminal window, enter:
    % which java
Set the PATH permanently

To set the path permanently, set the path in your startup file.
Note: Instructions for two most popular Shells on Linux and Solaris are listed. If you are using other shells, see the Path Setting Tutorial.
Bash Shell
Edit the startup file (~/.bashrc)
  1. Modify PATH variable
    PATH=/usr/local/jdk1.8.0/bin:$PATH
    export PATH
  2. Save and close the file
  3. Load the startup file
    % . /.profile
  4. Verify that the path is set by repeating the java command
    % java -version
C Shell (csh)
Edit the startup file (~/.cshrc)

  1. Set Path
    set path=(/usr/local/jdk1.8.0/bin $path)
  2. Save and close the file
  3. Load the startup file
    % source ~/.cshrc
  4. Verify that the path is set by repeating the java command
    % java -version

Monday, April 17, 2017

How to run a task in java periodically ?



How to schedule a task in java ?

Few java applications are required to update something after a period of time - and that too automatically or without manual intervention.

To do so, in Java, we can perform below steps to complete this task,


------------------------------ScheduleTask.java---------------------------



public class ScheduledTask extends TimerTask {
public void run() {

System.out.println(" scheduler enabled on " + new Date().getTime());

//you can perform any operation required as per your need here

}
}



--------------------RunScheduledTask.java------------------------------

public class RunScheduledTask{

    public static void main(String args[]){

                Timer time = new Timer(); // Instantiate Timer Object
ScheduleTask st = new ScheduleTask();
time.schedule(st, 0, 86400000);// scheduled for one day

    }
}



Saturday, March 25, 2017

How the HashMap works in Java or working of the HashMap ?

How HashMap works in Java ?


I will explain put and get operation done in HashMap in pictorial representation so that you will get clear idea what exactly happens inside.

What is HashMap ?

HashMap is in the java.util package. This package provides lot of inbuilt data structure so the we need not to implement it by our own.

So maps are associated arrays that let programmer stores the key-value pairs. We need key and value to store in the hash map and later on we need that key to look for the stored value in the hash map.

HashMap is the implementation of the Map interface. It is based on a function called Hashing.

Hashing is a technique by which you can short a big length String or Object into a fixed length number or representation. Eg, a long string, hashing can convert this string in a number.That helps in indexing and faster lookup.

As we know, each object in java has hash code method available.So this method is supposed to return hash code of the object.

Also there is a equal and hash code contract in java. If two objects are equal, there hash code should be same. So it is very important that there should be a robust implementation of hash code in class.

Why this hashcode is so important?

It is important because  hash code is used in storing values into the HashMap. If we lookup a value in the corresponding given key and if the hash code is not consistent you wont be able to lookup the corresponding value.

Map Hierarchy


Node of the HashMap and its fields


Lower level details:

HashMap has a table as an array of Node as show in the diagram.

Node has integer hash, K as key of the HashMap, V as the value of the corresponding key of the HashMap and Node<K, V> next as the next pointer to the next node of the linked list.Basically the node itself is a linked list.

So as in blue array diagram, each index of it is a Node of linked list and we call it bucket.Each index is a bucket and each bucket is a node or can be a linked list of nodes.

Implementation of the put method of the HashMap:
As I said a HashMap comprises of a table and a table is bydefault of size 16.
HashMap Tabular Structure (defauld size is 16)

So for  example we need to put below student entries in this table, lets see how does it put in this table:

Eg.

HashMap student = new HashMap();

student.put(“AJAY”,12);
student.put(“VIJAY”,32);
student.put(“SHARMA”,22);

Case 1:
Lets put AJAY as key and 12 as value of this key in hashmap

student.put(“AJAY”,12);

put(K k, V v){
hash(k);  => hash(“AJAY”) = 23462031
index = hash & (n-1); => index = 4
}

Here suppose hash code generated by hash(k) method of AJAY is 2346031.So we can not have array of this size because if each and every hash is going to have this much big number array with in it, soon we will run out of memory.

So we need to compute index to find out where we can put the value in given 2 raise to power n array which is 0 to 15 indexed.

That's why index is computed using modular operator. So basically we divided the hash to the maximum value of the range of array and we will get the reminder which will fit in the array range.
Suppose the index calculated here is 4. So the entry will go under index 4 as a node as shown below:

Put Node values at index 4

Case 2:

student.put(“VIJAY”,32);
put(K k, V v){
hash(k);  => hash(“VIJAY”) = 25462032
index = hash & (n-1); => index = 4
}

Case 3:

student.put(“SHARMA”,22);
put(K k, V v){
hash(k);  => hash(“SHARMA”) = 6416803
index = hash & (n-1); => index = 9
}

after inserting all 3 cases, we will form below hashmap structure. At array index 4 there would be two nodes added as linked list.

After inserting all key-value pairs

So in case of AJAY and VIJAY keys, we have collision because we have same index. So the next will point to the new node added in the linked list of existing node.

This is how the hashmap will look like at the end of the put operation

So here we have seen if there is same index of the key, then those node will be stored in the bucket as linked list nodes.


Java HashMap allows null key, which always goes to index 0 as hash of null is 0.

How get method works in HashMap ?

Suppose we need to get key: SHARMA from hash map.

V get(Objcet key){
hash(key) => 23462031
index = hash & (n-1) => 9
}


checking hash code and key value of map
In the get operation, we do the same set of operations to calculate the hash and find index of the key where it could have store the value in the array.

So it will compare the hash code which is 23462031 and if it matches then we will compare the key using equal method which is SHARMA in this case.If both matches, it will get the value.

Now we need to get key VIJAY


V get(Objcet key){
hash(key) => 25462032
index = hash & (n-1) => 4
}
get operation on hash map

Same thing happens in case of VIJAY but at index 4, we have two nodes in the bucket.


It will first compare hash code with first node which is not same, so it skip this node.Then It will check next nodes hash code which matched and then compare the key of with the same node using equal method which matched and returns the value 32 of this node.



Monday, February 13, 2017

Java8: Basics of Lambda Expression !!!

Lambda Expression:

Lambda expression is an anonymous method or function without declaration. It is useful to write short and less code because of this it avoid unnecessary lengthy code.

Lambda expression is much readable, productive and reliable code.

Lambda Expression make is easier to use functional programming.
It is the first step towards the functional programming.We can pass lambda expression as it is an object and execute when required.

Lambda feature is not new but new in java. It was there in Scala, Haskell..the languages which are
used for cloud.

Advantages of Lambda:

  1. It enables functional programming:We are used to write object oriented programming in java, but with lambda we can write functional programming.

  1. Readable and concise code:In some cases while development, lambda will remove the boilerplate code that we use to write in certain scenario.

  1. Easier to use libraries and APIs.

  1. Lambda enables support for parallel processing: We can use lambda where code runs over multiprocessor environments.


These are few of the advantages...


Lambda Expression has 3 parts:


i. Parameter List
  Lambda Expression can have 0 or 1  or more parameters. Parameters are optional.


ii. Lambda Arrow Operator


Lambda arrow operator is “->” and it detach parameters list and body


iii. Lambda Expression Body


Below highlighted in lambda expression’s body

Eg. java4sks ( x -> x*2 ), it means java4sks receives integer as and then returns the multiplication of  that integer.


In java or programming language we deals with data types. Like “java4sks” is of java.lang.String type.In the same way ,

What is the type of below mentioned lambda expression?


() -> System.out.println(“hi lambda….”);

Above statement has no parameters and return some result. So its type would be “java.lang.Runnable” … Functional Interface.

What is Functional Interface ?
Functional Interface is just like SAM interface, which means it has only one abstract method.
(Single Abstract Method). Java8 API has defined many Functional Interfaces.



In the next post, you will see some examples of lambda expression and discuss it in detail...











Fixing yum command on linux

You may find yourself having to fix more packages. So you can just remove everything you had installed via  pip and reinstall everythin...