Solved

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

Posted on 2004-10-15
10
220 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
 

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.

762 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now