Tech Me More

To quench our thirst of sharing knowledge about our day to day experience & solution to techincal problems we face in our projects.

Advertise with us !
Send us an email at diehardtechy@gmail.com

Friday, December 4, 2015

How to solve Failed to connect to server. Appium inspector Error

If you are unable to launch Appium inbuilt inspector and getting the error like failed to connect to server. Please chech that it is running. Even when your appium server is up and running. 

Please do the following changes in your appium Server.

1. Stop your appium Server.

2. Click Android Icon in appium and select application path.

3. click the choose button to locate you .apk file.



4. Click the settings icon and check the pre-launch application check box.




5. Restart your server.
6. Click on the inspector icon and click refresh.

7. It should be displayed. 


Appium element inspector is quite basic , we recommend to use Android's default UIAutomator viewer.

How to get rid of irritating SocketExceptions in TestNG?

While running your TestNG classes you may encounter some Exception like

java.net.SocketException: Software caused connection abort: socket write error

to avoid the SocketException in Eclipse please do the following changes.


  • Go to Windows from Menu Bar in Eclipse.
  • Select Preferences.
  • It will open a new window.
  • Choose TestNg and uncheck Use Project TestNG Jar.
  • Click apply and click OK.



Tuesday, October 27, 2015

How to configure Java complier and Java run time enviornment in Edit plus?

Edit plus is an easy to go software when it comes to small program execution, edit plus can be configured to compile and run any java program without opening the command prompt.

To configure the java compiler and JVM in edit plus please follow the below steps.

1. Go to tools-->configure user tool

below window will open.




2. Click add tool and select Program.

3. In the Menu Text give the name as Javac <You can give any name as you want.>

4. In the command section select the path of javac.exe file which is usually inside C:\Program Files\Java\jdk1.8.0_60\bin\javac.exe

5. In the argument section select fileName.

6. In initial directory section select file directory.

7. Click OK.

To set the Java run time environment repeat the step 1 and 2.

8. In the menu text give the name as Run <You can give any name as you want.>

9. In the command section select the path of java.exe file which is usually inside C:\Program Files\Java\jdk1.8.0_60\bin\java.exe

10. In the argument section select File name without extension.

11. In initial directory section select file directory.

12. Click OK.



you have successfully set up the java compiler and run time environment. 

To verify it just write a simple java program and compile it from tools-->Compile. 

To run tools-->Run

you can use the shortcut also to compile and run your program. Shortcuts are mentioned right beside tools-->the user command which you created.

Wednesday, August 26, 2015

Garbage collection in java : Process Explained



Garbage collection in java happens automatically , which is the biggest reason of Choosing Java over C++ or C programming languages.

Java objects are created in Heap area of Java virtual machine. Understanding how automatic garbage collection works can help you identify the potential performance barriers.

Java Heap space is divided into 3 sub areas.
  • New generation
  • Old generation
  • Perm Generation

New Generation is further divided into three categories.
  • EDEN SPACE
  • S0 Survivor 1
  • S1 Survivor 2


Below is the detailed information on how garbage collection takes place in Java.

  • Any new objects are allocated to the eden space. Both survivor spaces start out empty.
  • When the eden space fills up, a minor garbage collection is triggered.
  • Referenced objects are moved to the first survivor space. Unreferenced objects are deleted when the eden space is cleared.
  • At the next minor GC, the same thing happens for the eden space. Unreferenced objects are deleted and referenced objects are moved to a survivor space. However, in this case, they are moved to the second survivor space (S1). In addition, objects from the last minor GC on the first survivor space (S0) have their age incremented and get moved to S1. Once all surviving objects have been moved to S1, both S0 and eden are cleared. Notice we now have differently aged object in the survivor space.
  • At the next minor GC, the same process repeats. However this time the survivor spaces switch. Referenced objects are moved to S0. Surviving objects are aged.Eden and S1 are cleared.
  • After a minor GC, when aged objects reach a certain age threshold (8 in this example) they are promoted from young generation to old generation.
  • As minor GCs continue to occur objects will continue to be promoted to the old generation space.


So that pretty much covers the entire process with the young generation. 

Eventually, a major GC will be performed on the old generation which cleans up and compacts that space.

Stop the World Event - All minor garbage collections are "Stop the World" events. This means that all application threads are stopped until the operation completes. Minor garbage collections are always Stop the World events.


Thursday, July 16, 2015

Java program to validate the PAN card Number : Regular expression

Write a java program to validate a PAN Card number. A PAN card number is said to be valid when it starts with 5 uppercase letters 4 digits and Followed by single upper case letter. A Valid PAN have only 10 letters.

For example: a pan card number AMEPJ2585B is valid where as AM525LDKF is invalid.

Given below is the java program which makes use of regular expressions to validate the PAN number. 

ValidPan.java


import java.util.*;
class ValidPAN 
{
 public static void main(String[] args) 
 {
  Scanner scn=new Scanner(System.in);
  int t=scn.nextInt();
  String s[]=new String[t];
  boolean output[]=new boolean[t];
  for(int i=0; i<s.length;i++)
  {
   s[i]=scn.next();
   boolean ss=s[i].matches("[A-Z]{5}\\d{4}[A-Z]{1}");
   output[i]=ss;
  }
  for(boolean b:output)
  {
   System.out.println(b);
  }
  
  

 }
}



Sample Output: 





Monday, July 13, 2015

Java program to convert the number in time format


Given below is the java program to print the time given in number format to String format.

For example: if you give input as 5 45, the below program will print Quarter to six


import java.util.*;
class TimeTest 
{
 public static void main(String[] args) 
 {
  String addString="";
  HashMap<Integer,String> hm=new HashMap<Integer,String>();
  hm.put(1,"one");hm.put(2,"two");hm.put(3,"three");hm.put(4,"four");hm.put(5,"five");
  hm.put(6,"six");hm.put(7,"seven");hm.put(8,"eight");hm.put(9,"nine");hm.put(10,"ten");
  hm.put(11,"eleven");hm.put(12,"twelve");hm.put(13,"thirteen");hm.put(14,"forteen");hm.put(15,"fifteen");
  hm.put(16,"sixteen");hm.put(17,"seventeen");hm.put(18,"eighteen");hm.put(19,"ninteen");hm.put(20,"twenty");
  hm.put(21,"twenty one");hm.put(22,"twenty two");hm.put(23,"twenty three");hm.put(24,"twenty four");hm.put(25,"twenty five");
  hm.put(26,"twenty six");hm.put(27,"twenty seven");hm.put(28,"twenty eight");hm.put(29,"twenty nine");hm.put(30,"thirty");

  Scanner scn=new Scanner(System.in);

  int hour=scn.nextInt();
  int minute=scn.nextInt();

  if(minute==00)
  {
   addString=hm.get(hour)+" o' clock";
  }
  else
   if(minute==15)
  {
   addString="quarter past "+hm.get(hour);
  }
  else
   if(minute==30)
  {
   addString="half past "+hm.get(hour);
  }
  else 
   if(minute>30&&minute==45)
  {
   addString="quarter to "+hm.get(hour+1); 
  }
  else
   if(minute>30&&minute!=45)
  {
   minute=60-minute;
   String midString="minute";
   String min=String.valueOf(minute);
   if(min.length()>1)
   {
    midString="minutes";
   }
   addString=hm.get(minute)+" "+midString+" to "+hm.get(hour+1); 
  }
  
  else
  {
   String midString="minute";
   String min=String.valueOf(minute);
   if(min.length()>1)
   {
    midString="minutes";
   }
   
   addString=hm.get(minute)+" "+midString+" past "+hm.get(hour); 
  }

  System.out.println(addString);

 }
}

Sample output

Monday, March 2, 2015

How to access the private method of a class outside of the class in Java. [Reflection]

How to access the private method of a class in other class. For example check the below class Return , it has a private method rate(). How to access method rate() outside of class Return.


package reflection;

public class Return {
 int price, discount;

 private int rate() {
  return (price - discount);
 }
}



Although, private methods are meant to be accessed in the same class they are declared and when we try to access it outside of the declared class it gives a compile time error. But Reflection API of java provides a way to access private method outsides of class. 

Check the below code which access the rate() method of Return class in Test class.


package reflection;

import java.lang.reflect.Method;

class Test {
 public static void main(String[] args) throws Exception {
  Return p = new Return();
  Method m = p.getClass().getDeclaredMethod("rate", null);
  m.setAccessible(true);
  System.out.println(m.invoke(p));
 }
}



Under the hood - no private stuff is actually private.

Thus, the field or method can be accessed.
In byte code - there is a flag which is byte code level implementation of private-ness.

Saturday, February 28, 2015

Write a Java program to print the following pattern. [Interview Question]

Write a Java program to print the following pattern.

1
3*2
4*5*6
10*9*8*7
11*12*13*14*15

Given above is a frequently asked question in interviews from Java developers.


class TrianglePattern 
{
 public static void main(String[] args) 
 {
  int n=15;
  int i=1;
  int cnt=1;
  boolean ltr=true;
  while(i<n)
  {
   String s="";
     if(ltr)
      for(int j=0;j<cnt;j++)
    s=s+(s.length()>0?"*":"")+i++;
     else
     for(int j=0;j<cnt;j++)
    s=i++ +(s.length()>0?"*":"")+s;
     cnt=cnt+1;
     ltr=!ltr;
     System.out.println(s);
  }
  
 }
}


Output:





Friday, February 27, 2015

[Webdriver] Selenium test script to scroll and print all items name from a web page.

Write a selenium web driver test script to scroll and print all items name from a web page. Below is an example using FlipKart.

Scenario

  • Log in to Flipkart.

  • Type Samsung in search box and Click search button.

  • Click mobile link.
  • Scroll and print all the names.


Scrolling is the only challenging portion here, until when we have to scroll is the question. In the above scenario we will scroll the web page until no-more-results are displayed.


package chubJava;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class FKart_Test {
 
 static WebDriver driver=new FirefoxDriver();
 
 public static void main(String[] args) {
  driver = new FirefoxDriver();
  driver.get("http://www.flipkart.com");
  driver.findElement(By.id("fk-top-search-box")).sendKeys("Nokia");
  driver.findElement(By.xpath("//form[@id='fk-header-search-form']/div/div/div[2]/input[1]")).click();
  driver.findElement(By.partialLinkText("Mobiles")).click();
  JavascriptExecutor jse = (JavascriptExecutor) driver;
  int count = 1500;
  while (!driver.findElement(By.id("no-more-results")).isDisplayed()) {
   count = count + 500;
   jse.executeScript("scroll(0, " + count + ")");
   try {
    Thread.sleep(500);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   if (driver.findElement(By.id("show-more-results")).isDisplayed()) {
    driver.findElement(By.id("show-more-results")).click();
   }
  }

  List<WebElement> al = driver.findElements(By.xpath("//div[contains(@class,'pu-title')]/a"));

  System.out.println(al.size());

  for (int i = 0; i < al.size(); i++) {
   System.out.println(al.get(i).getText());
  }
  
  driver.close();
 }

}

Ping us for any help. Like us at Facebook.