Solved

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

Posted on 2004-10-15
10
225 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 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
Technology Partners: 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 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb example issues 3 53
Problem to Alipay 10 71
Notify sent to other threads in Java 9 43
JDBC performance 8 29
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

749 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