how to call ProC or C exe file from Java program


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: how to call ProC or C exe file from Java program

  1. #1
    Join Date
    Apr 2005
    Posts
    15

    how to call ProC or C exe file from Java program

    HI all,

    Very nice to meet u..

    I need ur help...

    I am trying to ProC(c program using Oracle) from Java program...So i need ur help for some tips...IF u have any tips or u know any sites regarding this perpose, pls help me...

    thanks,

    Naveen Yerramaneni.

  2. #2
    Join Date
    Mar 2005
    Location
    Sendling, MUC, .de
    Posts
    100
    If you mean starting an executable, this is (eg on Window$)
    Code:
    try {
      String[] cmdLine = new String[] {"myProgram.exe", "arg1"};  // program name (possibly with path) + zero or more command line args
      String[] env = new String[] {"PATH=%PATH%;C:\\somedir"}; // just an example, null is also valid
      java.io.File dir = new java.io.File("C:\\Program Files\\workingdir"); // null for current directory
      Process myProcess = Runtime.getRuntime().exec(cmdLine, env, dir);
    } catch (java.io.IOException ioX) {
      ioX.printStackTrace();
    }
    which starts "myProgram.exe" as a subprocess of the Java VM. You can also feed (standard) input to and get output from this newly created process via
    Code:
    java.io.OutputStream stdin = Process.getOutputStream(); //STDIN of myProgram.exe
    java.io.InputStream stdout = Process.getInputStream();  //STDOUT of myProgram.exe
    java.io.InputStream stderr = Process.getErrorStream();  //STDERR of myProgram.exe
    Note that from your java program's point of view STDIN of myProgram.exe is something you want to write to and hence an OutputStream, while STDOUT and STDERR are streams you want to read from and hence InputStreams.

    If you need not set environment variables or the working directory, there are also abbreviated versions of Runtime.exec(). There are also the methods Process.exitValue() and Process.waitFor().
    Consider to explicitely kill the process - via Process.destroy() - if you are not sure that it does properly terminate since the subprocess will execute asynchronously when there are no more references to the Process object (or maybe you want that).
    Finally, Java 1.5 offers the more convenient and flexible ProcessBuilder to create Processes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center