Solved

Ruuning a utility form a java applicaiton at real time in windows

Posted on 2004-10-15
10
223 Views
Last Modified: 2010-03-31
Hi,

I have a JAVA Applciation that needs a file in xsd format.
I have to pickup files that are in DTD format (updated often) from a  FTP public site
I have a tool (binary - no source) that accepts DTD and converts it to xsd beautifully.
I want to use this tool in my application.
Depending on what file user selects form the FTP website using my applicaiton's (JAVA) Interface,
I need to give that updated file to the converstion tool and pick up the produced XSD output file.
The tool can be run from commandline with 2 arguments and single command
e.g. c:\> DTD2XSD input.dtd output.xsd

Can someone please tell me how can I do this?

Thanks
Devang.
0
Comment
Question by:dkamdar
  • 5
  • 3
  • 2
10 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 420 total points
ID: 12324121
You can use the following thus:

String[] command = { "DTD2XSD", args[0], args[1] }; // (arguments to program)
RunAsync.main(command);


SNIP=======================================================================


import java.io.*;

/**
 *  Description of the Class
 *
 * @author     Administrator
 * @created    23 February 2004
 */
public class RunAsync {

  /**
   *  Description of the Method
   *
   * @param  args  Description of the Parameter
   */
  public static void main(String args[]) {

    try {

      if (args.length < 1) {
        System.out.println("Usage: java RunAsync <command string>");
        System.exit(-1);
      }
      Process pro = null;
      if (args.length > 1) {
        pro = Runtime.getRuntime().exec(args);
      }
      else {
        pro = Runtime.getRuntime().exec(args[0]);
      }
      InputStream error = pro.getErrorStream();
      InputStream output = pro.getInputStream();
      Thread err = new Thread(new OutErrReader(error));
      Thread out = new Thread(new OutErrReader(output));
      out.start();
      err.start();
      pro.waitFor();
    }
    catch (java.io.IOException e) {
      e.printStackTrace();
    }
    catch (java.lang.InterruptedException e) {
      e.printStackTrace();
    }

  }


  /**
   *  Description of the Class
   *
   * @author     Administrator
   * @created    23 February 2004
   */
  static class OutErrReader implements Runnable {
    InputStream is;


    /**
     *Constructor for the OutErrReader object
     *
     * @param  is  Description of the Parameter
     */
    public OutErrReader(InputStream is) {
      this.is = is;
    }


    /**
     *  Main processing method for the OutErrReader object
     */
    public void run() {
      try {
        BufferedReader in = new BufferedReader(new InputStreamReader(is));
        String temp = null;
        while ((temp = in.readLine()) != null) {
          System.out.println(temp);
        }
        is.close();
      }
      catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
}

0
 
LVL 92

Assisted Solution

by:objects
objects earned 80 total points
ID: 12324425
0
 

Author Comment

by:dkamdar
ID: 12324614
Hey tt,
I understand you have used exec() to execute the utility. But can you please commnet on rest of the code. I cannot understand why have you overridden runnable method.
please excuse me if this is avery silly question ... I am a beginer in JAVA.
Can ypu pls write 1 sentence comments in your snippet so that i can understand whats going on?

Thanks a lot
 
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

Author Comment

by:dkamdar
ID: 12324620
Sorry prev comment was for CEHJ
0
 
LVL 92

Assisted Solution

by:objects
objects earned 80 total points
ID: 12324632
the following article discusses using exec() and some of its pitfalls:

http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 420 total points
ID: 12324637
>>But can you please commnet on rest of the code.

The general idea is that the output stream and the error stream of the process are treated as separate threads. This helps to prevent deadlock. These streams are read until they are exhausted.
0
 
LVL 92

Assisted Solution

by:objects
objects earned 80 total points
ID: 12324719
> This helps to prevent deadlock.

the problems not really a deadlock.
see the article above for a discussion of the various problems, are how to deal with them.
0
 
LVL 92

Assisted Solution

by:objects
objects earned 80 total points
ID: 12324784
Basically the problem is that the stdout/stderr output from the program can fill the output buffer and block proceedings, so threads are used to ensure the buffers are cleared. So if the command does not produce any output you don't need to worry about extra threads and can simply just call exec:

Process p = Runtime.getRuntime().exec(command);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12341713
8-)
0
 
LVL 92

Expert Comment

by:objects
ID: 12342870
:)
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question