Tuesday, April 15, 2014

Java is passes by value or reference and what about passing object to a method.

Java is strictly is a pass by value Objects, however, work a bit differently. When you pass a Java object or array as a parameter, an object reference or array reference is passed into a method. 


Java is always pass-by-value. The difficult thing can be to understand that Java passes objects as references and those references are passed by value.
It goes like this:
public void foo(Dog d) {
  d.getName().equals("Max"); // true
  d = new Dog("Fifi");
  d.getName().equals("Fifi"); // true
}

Dog aDog = new Dog("Max");
foo(aDog);
aDog.getName().equals("Max"); // true
In this example aDog.getName() will still return "Max"d is not overwritten in the function as the object reference is passed by value
Likewise:
public void foo(Dog d) {
  d.getName().equals("Max"); // true
  d.setName("Fifi");
}

Dog aDog = new Dog("Max");
foo(aDog);
aDog.getName().equals("Fifi"); // true
http://stackoverflow.com/questions/9404625/java-pass-by-reference

Tuesday, March 18, 2014

Get the ProcessList

Below code gives all the processors of your machine.
It will create a ProcessList.txt file for you,with all the processor listed.

package General;

import java.io.*;
import java.util.StringTokenizer;

public class GetProcessList
{

 private String GetProcessListData()
 {
 Process p;
 Runtime runTime;
 String process = null;
 try {
 System.out.println("Processes Reading is started...");

 //Get Runtime environment of System
 runTime = Runtime.getRuntime();

 //Execute command thru Runtime
 p = runTime.exec("tasklist");      // For Windows
 //p=r.exec("ps ux");              //For Linux

 //Create Inputstream for Read Processes
 InputStream inputStream = p.getInputStream();
 InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
 BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

 //Read the processes from sysrtem and add & as delimeter for tokenize the output
 String line = bufferedReader.readLine();
 process = "&";
 while (line != null) {
 line = bufferedReader.readLine();
 process += line + "&";
 }

 //Close the Streams
 bufferedReader.close();
 inputStreamReader.close();
 inputStream.close();

 System.out.println("Processes are read.");
 } catch (IOException e) {
 System.out.println("Exception arise during the read Processes");
 e.printStackTrace();
 }
 return process;
 }

 private void showProcessData()
 {
 try {

 //Call the method For Read the process
 String proc = GetProcessListData();

 //Create Streams for write processes
 //Given the filepath which you need.Its store the file at where your java file.
 OutputStreamWriter outputStreamWriter =
 new OutputStreamWriter(new FileOutputStream("ProcessList.txt"));
 BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);

 //Tokenize the output for write the processes
 StringTokenizer st = new StringTokenizer(proc, "&");

 while (st.hasMoreTokens()) {
 bufferedWriter.write(st.nextToken());  //Write the data in file
 bufferedWriter.newLine();               //Allocate new line for next line
 }

 //Close the outputStreams
 bufferedWriter.close();
 outputStreamWriter.close();

 } catch (IOException ioe) {
 ioe.printStackTrace();
 }

 }

 public static void main(String[] args)
 {
 GetProcessList gpl = new GetProcessList();
 gpl.showProcessData();

 }
}


Starting Windows Services from the java code(Tomcat)

in order to start a particular service you need to confirm that the service is there in the services Tab of the Window Task Manager.Here i will be starting tomcat.

Copy and paste the following code

package General;

import java.io.*;
import java.util.*;

public class Service {
  public static void main(String args[]) {

 String arg1="start";
 String arg2="stop";
    String[] command = {"cmd.exe", "/c", "sc", arg1, "Tomcat7"};
    try {
      Process process = new ProcessBuilder(command).start();
      InputStream inputStream = process.getInputStream();
      InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
      BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
      String line;
      while ((line = bufferedReader.readLine()) != null) {
         System.out.println(line);
      }
    } catch(Exception ex) {
      System.out.println("Exception : "+ex);
    }
  }
}


Monday, November 18, 2013

What is a ClassNotFoundException

ClassNotFoundException is an Exception and will be thrown when Java program dynamically tries to load a Java class at Runtime and don’t find corresponding class file on classpath

Tuesday, November 13, 2012

Creating scenarios in FitNesse


|import           |
|net.indecomm.euil|


!|Scenario|abbc|url|def|data|ghi|locator|
|start    |UserInteractionWeb              |
|openURL  |@url                              |
|maximizeBrowser                             |

                       

|Abbc Def Ghi                                                       |
|url                       |data                |locator               |
|www.google.com|abhishekkumar|.//*[@id='Email']|
|www.gmail.com  |abhishekkumar|.//*[@id='Email']|

Friday, July 13, 2012

Log4j Example

Create a class where you need to implement log

package com.vaannila.dmin;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class SampleAdmin {
    static Logger logger = Logger.getLogger(SampleAdmin.class);

    public void generateReport() {
        logger.debug("Sample amit debug message");
        logger.info("Sample amit info message");
        logger.warn("Sample amit warn message");
        logger.error("Sample amit error message");
        logger.fatal("Sample fatal message");
        logger.trace("Sample trace message");
    };
}


A test class to run the above class.
package com.vaannila.admin;
import org.apache.log4j.Logger;

public class test {

    public static void main(String[] args) {
        SampleAdmin s = new SampleAdmin();
        s.generateReport();
    }
}

log4j.properties 
right click the src folder create a log4j.properties file.

log4j.rootLogger=TRACE
# AdminFileAppender - used to log messages in the admin.log file.
log4j.appender.AdminFileAppender=org.apache.log4j.FileAppender
log4j.appender.AdminFileAppender.File=admin.log
log4j.appender.AdminFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.AdminFileAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

log4j.logger.com.vaannila.admin=,AdminFileAppender


admin.log
Right click the project and create a admin.log file, which would conatin you log.

 log4j.jar file
Download the log4j.jar file which is log4j-1.2.15.

All other important points are highlighted, dont miss that.
If required result not comming, tell me.





















Wednesday, July 11, 2012

Handling Pop up in Selenium2

public class closeopen {
    public static void main(String[] args) {
       
         WebDriver driver = new FirefoxDriver();
         driver.get("http://www.javascripter.net/faq/alert.htm");
         driver.findElement(By.xpath("html/body/p[1]/table/tbody/tr/td[1]/form/input")).click();
         Alert alert = driver.switchTo().alert();
         alert.accept();
}
}

enjoy

java-8-streams-map-examples

package com.mkyong.java8; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; import java.util.List; im...