Solved

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

Posted on 2004-10-15
10
221 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Understanding websocket example in spring 1 54
unix in java example 9 52
advertisement module in core php 4 144
replace a word with other 1 40
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 Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

930 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

13 Experts available now in Live!

Get 1:1 Help Now