Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2004-10-15
10
Medium Priority
?
228 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 1260 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 240 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

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

Assisted Solution

by:objects
objects earned 240 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 1260 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 240 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 240 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

610 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