jump to navigation

Capture Console Output in Java January 10, 2010

Posted by ratheeshnarayanan in Uncategorized.
add a comment

I have been working on a test automation project with Silk4J for the past one month. While working on this project I came across a scenario where I had to execute few commands (like ipconfig, net accounts etc.) from Java (Silk4J) and the result of those commands being executed had to be captured.  Here is the solution I had given to our development team. I hope this will help someone who might have similar requirements.

try {

//Put your command here..

Process p = Runtime.getRuntime().exec(“net accounts”);

BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));

StringBuffer output = new StringBuffer();StringBuffer error = new StringBuffer();

String line = null;

while ((line = stdInput.readLine()) != null) {

output.append(line+”\n”);

}

 while ((line = stdError.readLine()) != null) {

error.append(line+”\n”);

}

if(!””.equals(output.toString()))

{

System.out.println(“HERE IS THE STD -OUTPUT- OF THE COMMAND YOU HAD EXECUTED:\n\n”+

output.toString());

}

if(!””.equals(error.toString()))

{

System.out.println(“HERE IS THE STD -ERROR- (IF ANY) OF THE COMMAND YOU HAD EXECUTED:\n\n”+

error.toString());

}

}

catch (IOException e) {

 e.printStackTrace();

}

Advertisements

Database Connection Pooling: Tomcat + JNDI January 5, 2009

Posted by ratheeshnarayanan in HOWTO.
add a comment

Here let me tell you how to make your application get the connection from a connection pool.

 

The server environment here is Tomcat and the version is 6.0. We use Java Naming and Directory Interface (JNDI) to lookup the connection.

 

First I’ll show you the code you will use in your application to get the connection.

 

 

InitialContext ic = new InitialContext();

Context envCtx = (Context) ic.lookup(“java:comp/env”);

ds = (DataSource) envCtx.lookup(“jdbc/<jndiname>“);

 Connection connection = ds.getConnection();

 

 

Next we should configure the context.xml of the Tomcat Server. Configure your context.xml as given below: (This should go under the <Context> element)

 

<Resource

     name=”jdbc/<jndiname>

     auth=”Container”

     type=”javax.sql.DataSource”

     username=”<username>

     password=”<password>

    driverClassName=”com.mysql.jdbc.Driver”

    url=”jdbc:mysql://localhost:3306/<database>?

    autoReconnect=true”

    maxActive=”8″

    maxIdle=”4″

/>

 

 

Finally make a resource reference in your applications web.xml:

 

<resource-ref>

      <res-ref-name>jdbc/<jndiname> </res-ref-name>

     <res-type>javax.sql.DataSource</res-type>

     <res-auth>Container</res-auth>

</resource-ref>

 

You are ready to use Connections retrieved from Tomcat Server’s connection pool via configured JNDI.