Executing C program from with a java program

How do you call a c program from within a java program, and parse arguments into that program and return something.
Cheers for any reply...

Read http://www.javaworld.com/jw-12-2000/jw-1229-traps.html

Similar Messages

  • Executing a Java Program from within a Java Program

    I need to execute the following Java Program from withing another Java Program. The office toolbar command line is
    D:\WINDOWS\system32\java.exe -cp E:\Development\Eclipse\UpdateServer\Classes -server -showversion UpdateServer
    I can find no combination of ProcessBuilder commands, including those that include "Cmd.exe /c" that will make this program run from within another Java Program. All the examples I can find only show how to run Windows *.exe programs. I keep getting error 123 from ProcessBuilder.start(), but I can find no documentation for error 123.

    Assuming your code didn't get mangled by the forum
    (it's missing one "), it may be that your "-cp
    E:\\Develop.." argument is getting quoted as it has a
    space in it; try passing "-cp" and "E:\\Develop..."
    as two arguments.That worked; specifically the following tested OK:
    ProcessBuilder pb = new ProcessBuilder("D:\\WINDOWS\\System32\\Java.exe", "-cp", "E:\\Development\\Eclipse\\UpdateServer\\Classes\\", "-server", "-showversion", "UpdateServer" );
    pb.directory(new File("E:\\Development\\Eclipse\\UpdateServer\\Classes\\"));
    try{
         Process p = pb.start();
         InputStream is = p.getErrorStream();
         InputStreamReader isr = new InputStreamReader(is);
         BufferedReader br = new BufferedReader(isr);
         String line;
         while ((line = br.readLine()) != null)
              System.out.println(line);
         p.waitFor();
    }catch(IOException ioe){
    I was sure I tried that exact same code before, and it did not work, but now it does, at least at the top level (when it is in main of a test program). I will have to wait to try it until later when it is buried deep in a subroutine.

  • How to run java programs from a master java program?

    Hello,
    I have several java programs which run from the command prompt. I am seeking help with code for starting java programs from within a java program. For example, a program called master.java works something like this:
    import java.*;
    create connection pool
    create variables and result sets
    start/run slave1.java (var1, var2);
    start/run slave2.java (var3, var4, var5);
    start/run slave3.java (var1, var4);
    end of program master.java
    Each of the slave.java programs will run for up to an hour. I do not want the master.java program to pause for each slave program to stop. Instead, the master program will keep running and multiple slave programs will be running simultaneously with the master program. When a slave program starts, it is on its own. Also, if possible, I would like to have each of these slave.java programs open in a new separate command window, so I can observe each slave program running in separate windows.
    Any suggestions for code or helpful documentation are greatly appreciated.
    Thank you,
    Logan

    Thank you all.
    At the bottom of master.java I have successfully started a batch file with these lines:
    String jcmd = "cmd.exe /c start c:/data/simulations/MsgViewCount2.bat";
    Process proc = Runtime.getRuntime().exec(jcmd);
    But I still cannot get a java program to start. Here is one variation I have tried:
    String [] cmdArray = new String[2];
    cmdArray[0] = "java";
    cmdArray[1] = "slave1";
    Runtime runtime = Runtime.getRuntime();
    Process process = runtime.exec(cmdArray);
    This compiles, and no errors occur, but nothing happens.
    Regarding this comment:
    Why Runtime.exec? Either make the slaves Runnable or
    just call their main() methods.
    Oh, I see. Sepearate output. :PNone of the slave.java programs have any output.
    Thanks again.

  • How to terminate and restart a java program from a controling java program?

    I have a situation where my program needs to run exactly once each one
    hour. This is a huge program that creates a lot of threads and opens
    lot of resources. After one hour has lapsed, I want to terminate this
    program (as if Exit(0) was performed on it or as if Ctrl C was pressed
    from the command line to terminate a running program) and then run it again and so on repeat the cycle each hour. This will guarantee that
    all the resouces taken by the programs are freed or if some threads were still blocked on i/o, are indeed terminated each hour.
    The java program does not have any user interface and it is run from the
    command line.

    if you can change the program to be controlled, you could make it listen for commands on some networkport for shutting it down.
    If this is not an option you could start OS programs to kill the first program. On Unix System kill will do, but you need the process id so you'd need another os call ...
    You could do the os calls in two ways
    a) execute shell commands (search the forum on how to do that
    b) write a little C wrapper for the os call so you can call them from JNI (Java Native Interface)
    I do not know of any way do this kind of thing directly
    regards
    Spieler

  • How to call a remote external c++ program from a local java program?

    local java program pass some parameters to a remote C++ program and get the result from that C program output.

    You could use a tcp/ip connection (e.g. with Sockets), and pass the parameters as Strings in the socketstream.

  • How to execute a Perl program from within a Java prog

    How do I execute a Perl program from within a Java program.
    Lets say the Perl program that I want to execute is 'abc'. Now, 'abc' requires some input that I want to give it from within the Java program. How do I do it?
    And finally, how do I execute that Perl program from within the Java program.
    If I execute the Perl program alone then I do it in the following way -
    perl abc inp1 inp2 inp3
    where inp1, inp2, inp3 are inputs to the Perl program. I will not be able to change or modify the coding of the Perl program - 'abc' as I do not have access to its code. Its a kind of an application whose usual method of execution is in the above shown way. So, how do I execute 'abc' from within a Java program.

    what part of don't crosspost, don't you understand?
    http://forum.java.sun.com/thread.jsp?forum=4&thread=427193

  • Executing a Perl program from within a Java prog

    How do I execute a Perl program from within a Java program.
    Lets say that the Perl program that I want to execute is 'abc'.'abc' requires some input that I want to give from within the java program. How do I do that? Then I want to execute the Perl program from within the Java prog. How do I do it?

    don't crosspost.
    http://forum.java.sun.com/thread.jsp?forum=31&thread=427211&tstart=0&trange=100

  • Problem with creation of a jar file from inside a java program

    Hi,
    I am trying to create a jar file at runtime from within a java program.
    I am able to create a jar file just fine using:
    String[] jarArgs = new String[3];
    jarArgs[0] = "cvf";
    jarArgs[1] = "C:\temp\myjar.jar";
    jarArgs[2] = "C:\temp\this";
    sun.tools.jar.Main main1 = new sun.tools.jar.Main(System.out, System.err, "jar");
    main1.run(jarArgs);However, when I look at the jar it puts the absolute path to the files inside such as:
    C:\temp\this\is\my\package\Class.class
    instead of only this\is\my\package\Class.class
    When running the jar command from the command line it works just fine and I have the relative paths in my jar file.
    Does anyone have any experience with this and could help me out?
    Thanks in advance
    Edited by: mruf on Apr 11, 2008 1:51 AM

    Shouldn't jarArgs[2] = "-C C:\temp\this"

  • Start a WS application from within a java program

    Hi,
    I need to start a WebStart application from within a java program. Therefore I develeoped a class which starts javaws.exe within its main-method like this:
    try {
    Runtime.getRuntime().exec(
    "c:\\java web start\\javaws.exe http://a.b/c.jnlp"
    } catch (Exception e) {
    e.printStackTrace();
    And that did not work. Java Web Start tries to start the .jnlp program but returns with the message, that the app-desc|applet-desc|installer-desc|component-desc is missing. But it is there: It is an applet and thus I defined an applet-dec.
    I tried to do it from a command line with the followinf command:
    java -Djnlpx.home="C:\abc" -cp "C:\abc\javaws.jar" com.sun.javaws.Main http://a.b/c.jnlp
    And that works. But using this command from within my jjava prog using the Runtime.exec() method does not work.
    By the way, simply type "javaws http://a.b/c.jnlp" at the command line does not work.
    Can anybody help me? How may I start a .jnlp program within another java program.
    Kind Regards,
    Tobias Neubert

    Hi,
    I recently had a quite similar problem. At least the error message by the Java Web Start application on Mac OS X complained about the same error (app-desc|applet-desc|installer-desc|component-desc). It turned out to be some bad invisible characters in the jnlp file. I copied some sample from a web page which for some reason contained some unicode chars that the parser doesn't like. Use a different text editor or the less command on unix to see if there are some strange characters in you jnlp file.
    But it seems strange that it does work from the command line and not from your code.
    -Stefan

  • How does one run a *.exe with a Java program?

    Hi,
    I'm looking for a way to run an executable (*.exe) or batch file (*.bat) from within a Java program.
    Any suggestions, leads, or examples appreciated.
    Thanks!

    Runtime.getRuntime.exec("pathTo/yourapp.exe");
    For any type of interpreted scripts, the interpreter should be added:
    Runtime.getRuntime().exec({ "command.com", "mybat.bat"});
    or
    Runtime.getRuntime().exec({"/bin/sh", "configure.sh"});

  • Getting resultset from some other java program

    how could i getting resultset from some other java program?
    plz

    a resultset is directly linked to your (prepared) query.
    for performance(and synchronization) reasons, you do want to retrieve the data as fast as possible and then release it.

  • How to call up an executable file (eg. MSPaint) from within my java program

    How to call up an executable file (eg. MSPaint) from within my java program

    Ummm... why would you want to get MSPaint anyway? Even in the absense of real software, Java's own graphics tools are way more sophisticated - with a little time and effort, you could write a simple paint package that beat MSPaint hands down.

  • Running executable jar from within a java program

    Is there a way to launch an executable jar in a java program? I tried wrapping the jar in an exe and calling it using Runtime exec() and though this approach works it does only for windows. I want it to work for unix/linux too.

    jaki wrote:
    Yes, but it's a sub process.Nope.
    Hence the calling process doesn't quit unless the called one returns.Wrong.
    My calling program is actually a jar and what Im trying to do is delete the jar after it's done running. But the above method (of launching a separate jar doing the deletion) doesn't seem to work for the above mentioned reason.Wrong.
    If you could tell me any other way it would really be a big help.You don't need any other way. Maybe you need to allow some time for the calling program to wind up. 20 seconds may be overkill, but I'm not in the mood to find how short it can be and still succeed.
    The two classes are packaged in separate jars.import java.io.IOException;
    public class Deletee {
        public static void main(String[] args) {
          try {
             String[] cmds = {"java", "-jar", "E:/temp/Deleter.jar"};
             Runtime.getRuntime().exec(cmds);
             System.exit(0);
          } catch (IOException ex) {
             ex.printStackTrace();
    import java.io.File;
    import javax.swing.JOptionPane;
    public class Deleter {
       public static void main(String[] args) {
          File file = new File("E:/temp/Deletee.jar");
          try {
             Thread.sleep(20000);
          } catch (InterruptedException ex) {
             ex.printStackTrace();
          if (file.delete()) {
             JOptionPane.showMessageDialog(null, "Deleted");
          } else {
             JOptionPane.showMessageDialog(null, "Oops");
    }Try it and see for yourself.
    db

  • Ternimating a process that is started from insite a java program

    Hi,
    I have a question:
    I am using Java 1.1 running on a HP-UX platform(HP-UX 11.0).
    I start a "unix shell script" from inside a java process. Everything starts and runs perfectly fine. If for some reason I MANUALLY kill the java program with a "kill" command, the unix process that was started by the java process is not getting killed. (effectively the signal is not getting passed on to the child process). Is there a way to fix this?
    Thanks in Advance.
    Srikanth
    [email protected]

    Here is what the java API docs say about the Process class:
    The Runtime.exec methods create a native process and return an instance of a subclass of Process
    that can be used to control the process and obtain information about it. The class Process
    provides methods for performing input from the process, performing output to the process,
    waiting for the process to complete, checking the exit status of the process, and destroying
    (killing) the process.
    The Runtime.exec methods may not work well for special processes on certain native platforms,
    such as native windowing processes, daemon processes, Win16/DOS processes on Win32, or
    shell scripts. The created subprocess does not have its own terminal or console. All its
    standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process
    through three streams (Process.getOutputStream(), Process.getInputStream(), Process.getErrorStream()). The
    parent process uses these streams to feed input to and get output from the subprocess.
    Because some native platforms only provide limited buffer size for standard input and output
    streams, failure to promptly write the input stream or read the output stream of the
    subprocess may cause the subprocess to block, and even deadlock.
    The subprocess is not killed when there are no more references to the Process object, but
    rather the subprocess continues executing asynchronously.
    There is no requirement that a process represented by a Process object execute
    asynchronously or concurrently with respect to the Java process that owns the Process object.
    [/b[
    So the docs state that this class may not work with shell scripts. Which means that it may be no way to deal with this.
    Good luck
    Calin

  • Displaying a shell from within a Java program

    Hi,
    For some long and twisted reason, I need to execute native programs from a Java program. Since this program is only every intended to be run on Linux, this doesn't bother me breaking the x-platform rules for Java! So, Runtime.exec() is the normal way to do this.
    However, Runtime.exec() runs executes the command and then dishes basck the output once it's finished. The external program I wish to run takes a while and actually prints various pieces of feedback during its processing. I'd like to display this feedback to the user as it happens, rather than dumping it all at the end.
    I can't think of anyway to do this. I've been looking for "Java shells" but they are reimplemented shells with only let you run classes on the classpath. Any ideas from the gurus around here?
    Cheers

    import java.util.*;
    import java.io.*;
    class StreamGobbler extends Thread
    InputStream is;
    String type;
    StreamGobbler(InputStream is, String type)
    this.is = is;
    this.type = type;
    public void run()
    try
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String line=null;
    while ( (line = br.readLine()) != null)
    System.out.println(type + "> " + line);
    } catch (IOException ioe)
    ioe.printStackTrace();
    public class GoodLinuxExec
    public static void main(String args[])
    if (args.length < 1)
    System.out.println("USAGE: java GoodLinuxExec <cmd>");
    System.exit(1);
    try
    Runtime rt = Runtime.getRuntime();
    System.out.println("Execing " + args[0]);
    Process proc = rt.exec(args);
    // any error message?
    StreamGobbler errorGobbler = new
    StreamGobbler(proc.getErrorStream(), "ERROR");
    // any output?
    StreamGobbler outputGobbler = new
    StreamGobbler(proc.getInputStream(), "OUTPUT");
    // kick them off
    errorGobbler.start();
    outputGobbler.start();
    // any error???
    int exitVal = proc.waitFor();
    System.out.println("ExitValue: " + exitVal);
    } catch (Throwable t)
    t.printStackTrace();
    stolen from this article and adapted to suit an os which does not start with the letter W.

Maybe you are looking for

  • PDF newbie: image in PDF

    I am trying to learn the PDF language and make some PDF files from scratch. I have made some text files which work and now I want to insert some images, but I can't get it to work! I have made a testfile consisting of just an image inserted as an XOb

  • I am having trouble transferring files

    I have an iPhone 5c running on iOS 7.0.6 and my sister has also has an iPhone 5c running ios 7.0.4. She has flappy bird is there any way I can transfer the app from her phone to mines

  • What does this crash log mean (mds)

    I see a bunch of files under the CrashReporter log called mds2011-04-14-xxxxxxlocalhost.crash that look like this: Process: mds [6104] Path: /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/Metadat a.framework/Versions/A/Suppor

  • Register the iPod

    can I register my iPod online? Art

  • White/Blank Screen After QT 7.0.3 Update

    Ok, I am having the same problem as others on this board, and, after having sifted through every single thread, I still have not come up with any correctable answers to this problem. I, like others, recently updated to QT 7.0.3, and now any .mov or o