Friday, 1 July 2016

How to search file in linux find command

How to search file in Linux with find command

Syntax:
find <directory> -name <file-name>

Description: In place of '<directory>' place search directory and in place of '<file-name>' place search file.


Example 1:

find . -name  test.txt

Description 1:
In above command finds test.txt file in current directory( '.' represents current directory)


Example 2:

find . -name  test*

Description 1:
In above command finds files whose name starts with 'test' in current directory( '.' represents current directory)

How to use SSH command in linux?

Syntax:
ssh <user>@<IP>

Example 1:
 ssh root@192.168.1.1 

Example 2:
 ssh root@192.168.1.1 "cat /etc/fstab"

Description: In example2  "cat /etc/fstab" is directory

Linux grep command

How to search string in file in linux?

grep "<search-string>" <file>

Search string by ignoring case with grep.

grep -i "<search-string>" <file>

Search text in one or more files

grep "<search-string>" <file-names-starting>*

Word count with grep?

Example:

grep "2015-05-25 20:19:00" connectProcess.log | grep "successful" | wc -l

Note: Above example give count of lines which has "2015-05-25 20:19:00" and "successful"


How to search string in direcoty using grep?
syntax: grep -nr string my_directory

-n ==> line number in the file
'string' ==> String for search, followed by a wildcard character
-r ==> Recursively search sub directories listed

Exmaple:
grep -nr 'prmconnect' .

here . repesents current directory

Thursday, 30 June 2016

Access Control Modifiers and non access modifiers

Access Control Modifiers:

Java provides a number of access modifiers to set access levels for classes, variables, methods and constructors. The four access levels are:

  •     Visible to the package, the default. No modifiers are needed.
  •     Visible to the class only (private).
  •     Visible to the world (public).
  •     Visible to the package and all sub classes (protected).

Non Access Modifiers:

Java provides a number of non-access modifiers to achieve many other functionality.

  •     The static modifier for creating class methods and variables
  •     The final modifier for finalizing the implementations of classes, methods, and variables.
  •     The abstract modifier for creating abstract classes and methods.
  •     The synchronized and volatile modifiers, which are used for threads.

How to set HTTPS proxy from console?

How to set HTTPS proxy from console?

Windows Syntax:
set https_proxy=https://<username>:<password>@<IP>:port
Example: set https_proxy=https://ram.krishna:ram%40123@172.168.2.3:8080

Linux:
export https_proxy=https://<username>:<password>@<IP>:port
Example: set https_proxy=https://ram.krishna:ram%40123@172.168.2.3:8080

note:if user password has any special charectors that has to replace with ASCII code
Example:ram@123 has to replaced with ram%40123

How to set HTTP proxy from console?


Windows Syntax:
set http_proxy=http://<username>:<password>@<IP>:port
Example: set http_proxy=http://ram.krishna:ram%40123@172.168.2.3:8080

Linux:
export https_proxy=https://<username>:<password>@<IP>:port
Example: set http_proxy=http://ram.krishna:ram%40123@172.168.2.3:8080

note:if user password has any special charectors that has to replace with ASCII code
Example:ram@123 has to replaced with ram%40123

Basic Linux Commands

Basic Linux Commands

Name: uname -a

description: Displays operating system bit(32-bit or 64-bit)
output:  Linux LAB-PERF-01 2.6.32-220.el6.x86_64 #1 SMP Wed Nov 9 08:03:13 EST 2011 x86_64 x86_64 x86_64 GNU/Linux

Name: uname -m

description: Displays operating system bit(32-bit or 64-bit)
output:  x86_64

Name: ps

description: displays list of running processes 
output: 
  PID TTY          TIME CMD
 5748 pts/3    00:00:00 bash
 6090 pts/3    00:00:00 ps

Name: ps -eaf | grep Tcat

description: displays list of processes which has name 'Tcat'

Name: top

description: 'top' also same as 'ps'
output: 
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
61026 contest   20   0 5117m 1.2g  17m S  0.3  3.8  13:20.77 /home/contest/prd/j
60932 contest   20   0 55180 2196 1592 S  0.0  0.0   0:00.01 /usr/libexec/openss
Shot cuts: q --> Quit
                z -->change style
                c-->Additional info
                Shift+o -->sort
                k--> kill process

Name: free -m

description: Displays free memory space(In MB)
output: 
            total       used       free     shared    buffers     cached
Mem:         32072      22123       9949          0        302      13877
-/+ buffers/cache:       7943      24129
Swap:        49151          0      49151

Name: free -g

description:displays free memory in gigabites(GB)
output: 
            total       used       free     shared    buffers     cached
Mem:            31         21          9          0          0         13
-/+ buffers/cache:          7         23
Swap:           47          0         47

Name: ls

description:list the file and directories
output: 
ant                 jacoco          prd                  test
a.txt               jacoco_contest  rollbackPatch.retry  testjava
bkp                 jacoco.tar      settlement           tiles.xml
installPatch.retry  keystore        sftp                 uninstall.retry
install.retry       logs            TcatConnect

Name: ls -l

description: displays links(with file and folder permissions)
output: 
total 13968
drwxrwxr-x 3 contest contest       63 Sep  1  2014 ant
-rw-r--r-- 1 contest contest     1430 Nov  6  2015 a.txt
drwxrwxr-x 3 contest contest       44 Apr 15 13:15 bkp
-rw-rw-r-- 1 contest contest       14 Apr 22 20:08 installPatch.retry
-rw-rw-r-- 1 contest contest       13 May 10 14:57 install.retry
drwxrwxr-x 6 contest contest      112 May 23 15:59 jacoco
drwxrwxr-x 3 contest contest       16 Sep 22  2014 jacoco_contest
-rw-rw-r-- 1 contest contest 14254080 Jun 23 12:39 jacoco.tar
-rw-rw-r-- 1 contest contest     1371 Dec  3  2015 keystore
drwxrwxr-x 2 contest contest        6 Aug 19  2015 logs
drwxrwxr-x 8 contest contest     4096 Mar 10 14:29 prd
-rw-rw-r-- 1 contest contest       14 Apr 22 19:35 rollbackPatch.retry
drwxrwxr-x 4 contest contest     4096 Apr 13  2015 settlement
drwxrwxrwx 4 contest contest       44 Aug  7  2014 sftp
drwxrwxr-x 2 contest contest       22 Nov 23  2015 TcatConnect
drwxrwxr-x 3 contest contest       43 Nov 18  2015 test
drwxrwxr-x 4 contest contest       34 Jun 26  2015 testjava
-rw-rw-r-- 1 contest contest    15726 Dec 11  2014 tiles.xml
-rw-rw-r-- 1 contest contest       14 Apr  6 17:42 uninstall.retry

Name: uptime

description:displays server up time
output: 
17:00:18 up 68 days,  2:43,  6 users,  load average: 0.06, 0.07, 0.06

Name: w

description: displays number of users logged in and from where they logged in.
output: 

 17:00:40 up 68 days,  2:44,  6 users,  load average: 0.05, 0.06, 0.05
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty3     -                11May16 50days  0.03s  0.03s -bash
contest  pts/0    mckd-5382.comviv Wed18   22:43m  3.29s  3.28s tailf catalina.
contest  pts/1    :1.0             20May16 41days  0.01s  0.01s -bash
contest  pts/2    mckd-5382.comviv Wed18   22:35m 13:24   0.01s -bash
contest  pts/3    mckd-5377.comviv 16:45    0.00s  0.03s  0.01s w
root     tty1     :0               11May16 68days 12:22  12:22  /usr/bin/Xorg :

Name: w -u root

description: displays how many users logged in as root
output: 

 17:02:49 up 68 days,  2:46,  6 users,  load average: 0.00, 0.04, 0.04
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty3     -                11May16 50days  0.03s  0.03s -bash
root     tty1     :0               11May16 68days 12:22  12:22  /usr/bin/Xorg :

Name: sar

description: displays core of CPU
output: 
Linux 2.6.32-220.el6.x86_64 (LAB-PERF-01)       06/30/2016      _x86_64_       (   4 CPU)

Name: iostat

description: displays statistics of cpu and disk (-c will give only cpu stats and -d will give only disk stats)
output: 
Linux 2.6.32-220.el6.x86_64 (LAB-PERF-01)       06/30/2016      _x86_64_        (4 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
                    1.87    0.00    1.10    0.01       0.00     97.02

Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
sda               8.05         1.92      1404.75   11303270 8267325638
dm-0              0.00         0.00         0.00       5240          0
dm-1              3.08         0.42       663.48    2457985 3904786737
dm-2              1.15         0.07       176.36     429989 1037912909
dm-3              0.49         0.00         9.60       8465   56508262
dm-4              2.29         0.36       530.17    2095847 3120198667
dm-5              0.11         0.26         4.78    1549926   28133787

Name: lsof

description:displays list of open file
output: 
java      61026     contest  272r      REG              253,5    189401  42138737 /home/contest/prd/TcatConnect/mule-libs/opt/jgrapht-jdk1.5-0.7.3.jar
java      61026     contest  273r      REG              253,5    152797  42138741 /home/contest/prd/TcatConnect/mule-libs/opt/jdom-1.1.1.jar
java      61026     contest  274r      REG              253,5    391588  42138747 /home/contest/prd/TcatConnect/mule-libs/opt/wss4j-1.6.3.

Name: lsof -u root

description:displays list of files opened by user root

Name: lsof -i

description: list the internet connection.

Name: lsof -i:9888

description:display which process is using port 9888

Name: lsof -i tcp:9888

description:display which process is using tcp port 9888

Name: lsof -i tcp:1-1000

description:display which process is using tcp port from 1 to 1000(range)


Name:less

description:'less' is using to display file content(shift+g will show bottom of file)

Name:tailf

description:displays live content of file(ex: end of log file)

How to view cron jobs?

crontab -e

How to switch user ?

su - <user-name>  then it may ask for user password 

How to do java remote debugging

How to do java remote debugging?

Prerequisite: Application has to be running  in sever and same code you should have in your eclipse.

Step 1:
  • -Xdebug is used  to debug remote JVM.
  • To debug remote application we have to start application in debug mode.
  • We can start application in debug mode by adding below -Xdebug options in JAVA_OPTS of server start-up file(i.e catalina.bat or may be catalina.sh as per your server start-up file)
         -Xdebug -agentlib:jdwp=transport=dt_socket,address=1311,server=y,suspend=n

Example:

JAVA_OPTS="-Xms2048m -Xmx2048m  -XX:MaxPermSize=256m -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1310 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.rmi.server.hostname=<IP-ADDRESS> -Xdebug -agentlib:jdwp=transport=dt_socket,address=1311,server=y,suspend=n";
export JAVA_OPTS;

Step 2 :
  • Add break points in java file which you want to debug and right click on project, go to debug as -->debug configurations  and double click on 'Remote Java Application'.
  • Now you will get to see host and port(1311 as per above example) fill those fields and click on debug.
Step 3:
  • Now send one request to your server after sending your eclipse will get control.
  • Now use f5,f6,f8 to monitor code flow and variables values.

How to Monitor server JVM Heap?

Note: 
  • In above Example few JMX(Java Management Extension) option, these options are using to monitor server JVM Heap.
  • If you want to monitor server JVM Heap, we have to add above example options.

What is enum in java

What is enum?

1) Enums in Java are type-safe and has there own name-space. It means your enum will have a type for example "Currency"
in below example and you can not assign any value other than specified in Enum Constants.

public enum Currency {PENNY, NICKLE, DIME, QUARTER};
Currency coin = Currency.PENNY;
coin = 1; //compilation error

2) Enum in Java are reference type like class or interface and you can define constructor,
methods and variables inside java Enum which makes it more powerful than Enum in C and C++ as shown in next example of Java Enum type.

3) You can specify values of enum constants at the creation time as shown in below example:
public enum Currency {PENNY(1), NICKLE(5), DIME(10), QUARTER(25)};
But for this to work you need to define a member variable and a constructor because PENNY (1) is actually calling a constructor which accepts int value ,
see below example.

public enum Currency {
        PENNY(1), NICKLE(5), DIME(10), QUARTER(25);
        private int value;

        private Currency(int value) {
                this.value = value;
        }
};
Constructor of enum in java must be private any other access modifier will result in compilation error.
Now to get the value associated with each coin you can define a public getValue() method inside java enum like any normal java class.
Also semi colon in the first line is optional.

4) Enum constants are implicitly static and final and can not be changed once created. For example below code of java enum will result in compilation error:

Currency.PENNY = Currency.DIME;
The final field EnumExamples.Currency.PENNY cannot be re assigned.

5) Enum in java can be used as an argument on switch statment and with "case:" like int or char primitive type.
This feature of java enum makes them very useful for switch operations. Let’s see an example of how to use java enum inside switch statement:

   Currency usCoin = Currency.DIME;
    switch (usCoin) {
            case PENNY:
                    System.out.println("Penny coin");
                    break;
            case NICKLE:
                    System.out.println("Nickle coin");
                    break;
            case DIME:
                    System.out.println("Dime coin");
                    break;
            case QUARTER:
                    System.out.println("Quarter coin");
    }

from JDK 7 onwards you can also String in Switch case in Java code.

6) Since constants defined inside Enum in Java are final you can safely compare them using "==" equality operator as shown in following example of  Java Enum:

Currency usCoin = Currency.DIME;
if(usCoin == Currency.DIME){
  System.out.println("enum in java can be compared using ==");
}

By the way comparing objects using == operator is not recommended, Always use equals() method or compareTo() method to compare Objects.

7) Java compiler automatically generates static values() method for every enum in java. Values() method returns array of Enum constants
in the same order they have listed in Enum and you can use values() to iterate over values of Enum  in Java as shown in below example:

for(Currency coin: Currency.values()){
        System.out.println("coin: " + coin);
}

And it will print:
coin: PENNY
coin: NICKLE
coin: DIME
coin: QUARTER
           
Notice the order its exactly same with defined order in enums.

8) In Java Enum can override methods also. Let’s see an example of overriding toString() method inside Enum
in Java to provide meaningful description for enums constants.

Example:
public enum Currency {
  @Override
  public String toString() {
       switch (this) {
         case PENNY:
              System.out.println("Penny: " + value);
              break;
         case NICKLE:
              System.out.println("Nickle: " + value);
              break;
         case DIME:
              System.out.println("Dime: " + value);
              break;
         case QUARTER:
              System.out.println("Quarter: " + value);
        }
  return super.toString();
 }
};    
And here is how it looks like when displayed:
Currency usCoin = Currency.DIME;
System.out.println(usCoin);

output:
Dime: 10

9) Two new collection classes EnumMap and EnumSet are added into collection package to support Java Enum.
These classes are high performance implementation of Map and Set interface in Java and we should use this whenever there is any opportunity.

10) You can not create instance of enums by using new operator in Java because constructor of Enum in Java can only be private
and Enums constants can only be created inside Enums itself.

11) Instance of Enum in Java is created when any Enum constants are first called or referenced in code.

12) Enum in Java can implement the interface and override any method like normal class It’s also worth noting that Enum
in java implicitly implement both Serializable and Comparable interface. Let's see and example of how to implement interface using Java Enum:

public enum Currency implements Runnable{
  PENNY(1), NICKLE(5), DIME(10), QUARTER(25);
  private int value;
  ............
   
  @Override
  public void run() {
  System.out.println("Enum in Java implement interfaces");
           
   }
}

13) You can define abstract methods inside Enum in Java and can also provide different implementation for different instances of enum in java.
  Let’s see an example of using abstract method inside enum in java
  public enum Currency implements Runnable{
          PENNY(1) {
                  @Override
                  public String color() {
                          return "copper";
                  }
          }, NICKLE(5) {
                  @Override
                  public String color() {
                          return "bronze";
                  }
          }, DIME(10) {
                  @Override
                  public String color() {

                          return "silver";
                  }
          }, QUARTER(25) {
                  @Override
                  public String color() {
                          return "silver";
                  }
          };
          private int value;

          public abstract String color();
   
          private Currency(int value) {
                  this.value = value;
          }
          ..............
  }  
In this example since every coin will have different color we made the color() method abstract and let each instance of Enum to define   there own color.
You can get color of any coin by just calling color() method as shown in below example of java enum:

System.out.println("Color: " + Currency.DIME.color());

Enum Java valueOf example


  1. One of my reader pointed out that I have not mention about valueOf method of enum in Java, which is used to convert String to enum in java.
  2. “You could also include valueOf() method of enum in java which is added by compiler in any enum along with values() method.
  3. Enum valueOf() is a static method which takes a string argument and can be used to convert a String into enum.
  4. One think though you would like to keep in mind is that valueOf(String) method of enum will
  5. throw "Exception in thread "main" java.lang.IllegalArgumentException: No enum const class" if you supply any string other than enum values.