Friday, August 17, 2012

Java - Run a System Command and Return Output

Run a system command and return output:
import java.io.*;

public static String cmdExec(String cmdLine) {
    String line;
    String output = "";
    try {
        Process p = Runtime.getRuntime().exec(cmdLine);
        BufferedReader input = new BufferedReader
            (new InputStreamReader(p.getInputStream()));
        while ((line = input.readLine()) != null) {
            output += (line + '\n');
        }
        input.close();
        }
    catch (Exception ex) {
        ex.printStackTrace();
    }
    return output;
}
Sample Usage:
public static void main(String[] args) {
    CmdExec cmd = new CmdExec();
    System.out.println(cmd.run("ls -a"));
}

Running system commands in Java applications

Update: This article has been replaced by my newer "Java exec with ProcessBuilder and Process" article. While the Java code shown in this tutorial works on simple "Java exec" cases, the new article shows how to properly read the output streams from your system command in Java threads, and also how to write to your command's standard input, if necessary.
Feel free to read this article for background/legacy information, but I strongly recommend that you use the source code I'm sharing in my newer "Java exec" article, because it resolves the standard input, output, and error problems that I didn't handle properly in the code below.

Introduction

I've read a lot about Java but one of the things I rarely see discussed is how you should go about running external system commands. Of course, you probably don't read much about this because it takes away from the portability of Java applications. For instance, if you write a Java application on a Unix system, you might be interested in running the "ps -ef" command, and reading the output of the command. For Unix systems this is great, but unfortunately, this same program won't work on a Windows system because the ps command isn't available on Windows.
Well, we're going to forget about portability for this article, and demonstrate a method that can be used to run system commands. We've received a lot of requests about this topic, so here goes.

Discussion (Runtime exec and Process)

Executing a system command is relatively simple - once you've seen it done the first time. It involves the use of two Java classes, the Runtime class and the Process class. Basically, you use the exec method of the Runtime class to run the command as a separate process. Invoking the exec method returns a Process object for managing the subprocess. Then you use the getInputStream() and getErrorStream() methods of the Process object to read the normal output of the command, and the error output of the command. What you do with the output of the command executed is entirely up to you and the application you're creating.
(Note: There is also a getOutputStream() method that you can use to write to the process, but we won't cover that method in this article. We'll cover that and a few other advanced features in a future article.)

A Java exec example

The code shown in Listing 1 provides a working example of our "Java exec" technique in a file named JavaRunCommand.java.
import java.io.*;

public class JavaRunCommand {

    public static void main(String args[]) {

        String s = null;

        try {
            
     // run the Unix "ps -ef" command
            // using the Runtime exec method:
            Process p = Runtime.getRuntime().exec("ps -ef");
            
            BufferedReader stdInput = new BufferedReader(new 
                 InputStreamReader(p.getInputStream()));

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

            // read the output from the command
            System.out.println("Here is the standard output of the command:\n");
            while ((s = stdInput.readLine()) != null) {
                System.out.println(s);
            }
            
            // read any errors from the attempted command
            System.out.println("Here is the standard error of the command (if any):\n");
            while ((s = stdError.readLine()) != null) {
                System.out.println(s);
            }
            
            System.exit(0);
        }
        catch (IOException e) {
            System.out.println("exception happened - here's what I know: ");
            e.printStackTrace();
            System.exit(-1);
        }
    }
}
Listing 1 (above): The file JavaRunCommand.java shows how you can run an external system command from within a Java program.

How our Java exec code works

The first thing you do is specify the command you want to run by supplying this command to the Runtime class. Because you can't create your own instance of the Runtime class, you first use the getRuntime method to access the current runtime environment and then invoke the Runtime exec method. This returns a Process object.
Everything else you do involves methods of the Process object. In this case, because we're running the "ps -ef" command on a Unix system, we just need to read the output of the command. Reading the standard error probably isn't required in this case, but I thought at the very least it was at least worth showing, if not good programming practice.
I convert the input streams with the InputStreamReader and BufferedReader so I can use the readLine() method of the BufferedReader class. Because I use these classes, this application will not compile properly with an older JDK 1.0.x compiler (these classes weren't available in 1.0.x).

Download the "Java exec" example source code

I could go on at length about this topic, but the best thing I can recommend is that you download the source code and work with it for a while. Try running different commands to see if you can get them to work properly, and try to run a command that requires input (this will be a bit more complicated).
To download the JavaRunCommand.java source code shown in Listing 1, click here. Once the file is displayed in your browser you can select the File | Save As ... option of your browser to save the code to your local filesystem.
related articles

Wednesday, August 15, 2012

SOAP Client in java

SOAP Client in java

This article will teach you how to create a SOAP client in java. That is creating a client in java which requests soap server (no need to be in java) and get response from it. First create request message as follows:
1SOAPMessage message = MessageFactory.newInstance().createMessage();
2SOAPHeader header = message.getSOAPHeader();
3header.detachNode();
If you have namespace required in SOAP request then add those namespaces to envelope element as follows:
1SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
2envelope.setAttribute("namspace","namespaceUrl");
You can add as many attribute as you want. Now time to create request message body.
Following body is made assuming that SOAP server where this client will connect will have a public service method called getResponse(name) available.
1SOAPBody body = message.getSOAPBody();
2QName bodyName = new QName("getResponse");
3SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
4SOAPElement symbol = bodyElement.addChildElement("name");
5symbol.addTextNode(“Harry joy”);
Now that request message is ready it’s time to connect to soap server and send request. Following code will do this:
1SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
2SOAPMessage response = connection.call(message, endpoint);
3connection.close();
In above code endpoint is the SOAP server URL without “?wsdl”. To parse response you can do as follows:
1SOAPBody responseBody = response.getSOAPBody();
2SOAPBodyElement responseElement = (SOAPBodyElement)responseBody.getChildElements().next();
3SOAPElement returnElement = (SOAPElement)responseElement.getChildElements().next();
4if(responseBody.getFault() != null) { //-- If response has any fault.
5    System.out.println(returnElement.getValue()+" "+responseBody.getFault().getFaultString());
6else  {
7    System.out.println(returnElement.getValue());
8}
Here request/response messages are totally dependent on SOAP server, how you have configured it.

Bonus: How to print request/response xml?
1String getXmlFromSOAPMessage(SOAPMessage msg) throws SOAPException, IOException {
2    ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
3    msg.writeTo(byteArrayOS);
4    return new String(byteArrayOS.toByteArray());
5}
Use:
1System.out.println(getXmlFromSOAPMessage(message));
2System.out.println(getXmlFromSOAPMessage(response));

Full source code:




public class SOAPClient {

 private static final String endpoint = "http://localhost/SOAPService/MySoapService";

 public static void main(String[] args) throws SOAPException {
  SOAPMessage message = MessageFactory.newInstance().createMessage();
  SOAPHeader header = message.getSOAPHeader();
  header.detachNode();

  SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
  envelope.setAttribute("namespace","namespaceUrl");

  SOAPBody body = message.getSOAPBody();
  QName bodyName = new QName("getResponse");
  SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
  SOAPElement symbol = bodyElement.addChildElement("name");
  symbol.addTextNode("Harry Joy");

  SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
  SOAPMessage response = connection.call(message, endpoint);
  connection.close();

  SOAPBody responseBody = response.getSOAPBody();
  SOAPBodyElement responseElement = (SOAPBodyElement)responseBody.getChildElements().next();
  SOAPElement returnElement = (SOAPElement)responseElement.getChildElements().next();
  if(responseBody.getFault()!=null){
   System.out.println(returnElement.getValue()+" "+responseBody.getFault().getFaultString());
  } else {
   System.out.println(returnElement.getValue());
  }

  try {
   System.out.println(getXmlFromSOAPMessage(message));
   System.out.println(getXmlFromSOAPMessage(response));
  } catch (IOException e) {
   e.printStackTrace();
  }

 }

 private static String getXmlFromSOAPMessage(SOAPMessage msg) throws SOAPException, IOException {
  ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
  msg.writeTo(byteArrayOS);
  return new String(byteArrayOS.toByteArray());
 }

}

Tuesday, August 14, 2012

websites for job hunters

http://bww.jobamatic.com/a/jobs/find-jobs

www.indeed.com
www.monster.com

Sunday, August 12, 2012

chkconfig VS sysv-rc-conf

RedHat/CentOS: 
[root@bigboy tmp]# chkconfig mysqld on

Debian/Ubuntu:
user@ubuntu:~$ sudo sysv-rc-conf mysql on 

Tuesday, August 7, 2012

Media Release: CLARITY

FOR IMMEDIATE RELEASE
Tuesday, 07 August 2012, 9:00am

CONTACT:
Keri Stedman
Boston Children’s Hospital
617-919-3110

Thirty teams compete to interpret three families’ genomes
International contestants vie to bring “CLARITY” to DNA sequencing information

DNA sequencing is rapidly getting faster and cheaper, but it’s still unclear how physicians and patients will be able to use this information. In a contest led by Boston Children’s Hospital, 30 teams from around the world are vying to interpret the DNA sequences of three children with rare conditions whose cause remains a mystery—with the goal of establishing "best practices" for interpreting genomic data.

Participants in the competition, known as the CLARITY challenge, range from small biotech startups to the National Institutes of Health (see list below), representing the United States, Canada, China, India, Israel, Italy, Germany, the Netherlands, Singapore, Slovenia, Spain, Switzerland and Sweden.

Results of the challenge will be announced in November at the American Society of Human Genetics annual meeting in San Francisco (Nov. 6-10) by the contest’s organizers—David Margulies, MD, executive director of The Gene Partnership at Boston Children’s, Isaac Kohane, MD, PhD, director of the hospital’s Informatics Program, and Alan Beggs, PhD, director of the Manton Center for Orphan Disease Research at the hospital.

The goals of CLARITY (Children’s Leadership Award for the Reliable Interpretation and Transmission of Your genomic information) are to address technical and bioinformatics questions in analyzing DNA sequence results, bring standardization to the analysis of genetic variants and generate a comprehensive, actionable report that can guide decision-making by doctors, genetic counselors and patients. Contestants have a deadline of September 30 to submit their findings and reports.

"The last major barrier to widespread clinical use of DNA sequencing is the creation of accurate, understandable interpretations of sequence findings for doctors and patients,” says Margulies, also affiliated with the Center for Biomedical Informatics, Harvard Medical School. "The goal of this contest is to define norms, standards and models for reporting findings from exomes and genomes. We are excited about the number and quality of participants, and we look forward to seeing their entries."

All contestants have been given raw DNA sequence data (both whole-genome and whole-exome sequences) and de-identified clinical data from three children and their immediate relatives identified by The Manton Center for Orphan Disease Research at Boston Children’s.

Of the three children, two have a neuromuscular disorder and the third a cardiovascular disorder. While all three are believed to have a genetic cause for their disorder, they have come up negative on all known genetic tests.

"Traditional genetic tests examine our genes one by one, requiring doctors to have a good idea ahead of time which of our roughly 20,000 genes is the likely cause,” says Beggs, also a professor of Pediatrics at HMS. “The beauty of whole-genome sequencing is that it provides results for virtually all of our genes at once. The challenge for our contestants is to pick out that one disease-causing mutation from the vast numbers of genetic differences that make each of us unique.”

Contest sponsors Life Technologies Corporation and Complete Genomics generated the genome sequences being used in the Challenge. Boston Children’s will award a prize of $25,000 to the winning research team, which will be selected by a panel of judges according to pre-specified criteria.

“We wanted researchers at small institutions and startups, anywhere in the world, to be able to compete with the ‘big boys’ of genomics and in so doing find better solutions,” says Kohane, who co-directs the Center for Biomedical Informatics at HMS. “This contest gives everyone a level playing field on which to innovate.”

Challenge contestants:

1.      Beijing Genomics Institute (Shenzhen, China)
2.      Brigham & Women's Hospital, Division of Genetics (Boston)
3.      British Columbia Cancer Agency (Vancouver)
4.      Children's Hospital of Eastern Ontario (Ottawa)
5.      Clinical Institute of Medical Genetics (Ljubljana, Slovenia)
6.      Genedata AG (Basel, Switzerland)
7.      Genomatix Software GmbH (Munich, Germany)
8.      Genome Institute of Singapore  Agency for Science, Technology and Research (A*STAR) (Biopolis, Singapore)
9.      HudsonAlpha Institute for Biotechnology (Huntsville, Alabama)
10.  Institute for Systems Biology (Seattle)
11.  IRCCS Casa Sollievo della Sofferenza (San Giovanni Rotondo, Foggia, Italy)
12.  National Institutes of Health (Bethesda, Maryland)
13.  NextBio (Santa Clara, California)
14.  Omicia, Inc. (Emeryville, California)
15.  Pearlgen (Chapel Hill, North Carolina)
16.  Radboud University Nijmegen Medical Center (Nijmegan, Netherlands)
17.  Sanofi  (Cambridge, Massachusetts)
18.  Science For Life Laboratory (SciLifeLab), Karolinska Institute (Solna, Sweden)
19.  Scripps Genomic Medicine (San Diego)
20.  Seven Bridges Genomics (Cambridge, Massachusetts)
21.  SimulConsult / Geisinger (Chestnut Hill, Massachusetts / Danville, Pennsylvania)
22.  SNPedia (wiki)
23.  Strand Life Sciences (Bangalore, India)
24.  Tel Aviv University (Israel)
25.  The Medical College of Wisconsin (Milwaukee)
26.  The Research Institute at Nationwide Children's Hospital (Columbus, Ohio)
27.  The University of Texas Health Science Center at Houston, The Brown Foundation Institute of Molecular Medicine
28.  Universidad de Cantabria (Santander, Spain)
29.  University of Iowa (Iowa City)
30.  Yale School of Public Health, Division of Biostatistics (New Haven, Connecticut)

Full information about the Challenge is available online at www.childrenshospital.org/CLARITY. For further background on CLARITY and genome sequencing, see these recent posts on our science and innovation blog, Vector:


Boston Children’s Hospital is home to the world’s largest research enterprise based at a pediatric medical center, where its discoveries have benefited both children and adults since 1869. More than 1,100 scientists, including nine members of the National Academy of Sciences, 11 members of the Institute of Medicine and nine members of the Howard Hughes Medical Institute comprise Boston Children’s research community. Founded as a 20-bed hospital for children, Boston Children’s today is a 395 bed comprehensive center for pediatric and adolescent health care grounded in the values of excellence in patient care and sensitivity to the complex needs and diversity of children and families. Boston Children’s also is the primary pediatric teaching affiliate of Harvard Medical School. For more information about research and clinical innovation at Boston Children’s, visit: http://vectorblog.org.

- ### -