Programming to call other programs

I am in need of direction as I am unable to find any help on my current problem. I am developing a program that will create a database of triples from an xml document. My use of existing programs such as 4suite's '4rdf' which is called by the command prompt has left me with the issue of how do you program in java, to call other programs and/or the command line. I need to pass in arguments and execute the program from within one java program. Is this possible? Help is urgently required. Please help. Cheers Dave.

Allways happy to assist my follow programmers, this is a class I developed (with help from online documents of course) to contain all my executing needs. Note that you need to place these two classes in a package yourself, they are part of quite a complex one where I use them.
public class Semaphore {
  protected boolean blocked;
  public Semaphore() { blocked = false; }
  public synchronized void waitUntilSignalled() {
       if(blocked == false){
         try {
                blocked = true;
                wait();
            } catch(  InterruptedException e ) { }
  public synchronized void setSignalled() {
    if (blocked == true){
          blocked = false;
          notifyAll();
     public static class ExecStreamThread extends Thread
         InputStream is;
         OutputStream os;
         Semaphore sem;
         public ExecStreamThread(InputStream is, OutputStream redirect)
             this.is = is;
             this.os = redirect;
             sem = null;
         public ExecStreamThread(InputStream is, OutputStream redirect, Semaphore theSem)
             this.is = is;
             this.os = redirect;
             sem = theSem;
         public void run()
              try
                   PrintWriter pw = pw = new PrintWriter(os);
                   InputStreamReader isr = new InputStreamReader(is);
                   BufferedReader br = new BufferedReader(isr);
                   String line=null;
                   while ( (line = br.readLine()) != null)
                            pw.println(line);
                       pw.flush();
              } catch (IOException ioe){
                  // don't have to do anything, the parent thread will have found out the process errored
              // run is complete, signal the semaphore
              if(sem != null){
                   sem.setSignalled();
     public static final int OSTYPE_WINDOWS = 1;
     public static final int OSTYPE_WINNT = 2;
     public static final int OSTYPE_WINCE = 3;
     public static final int OSTYPE_LINUX = 4;
     public static final int OSTYPE_MAC = 5;
     public static final int OSTYPE_SOLARIS = 6;
     public static final int OSTYPE_NETWARE = 7;
     public static final int OSTYPE_OS2 = 8;
     public static final int OSTYPE_UNKNOWN = 9;
     private static int type = OSTYPE_UNKNOWN;
     private ExecUtil()
          @return an integer identifying the OS (one of the OSTYPE constants)
     public static int getOs()
          if(type == OSTYPE_UNKNOWN){
               String osname = System.getProperty("os.name").toLowerCase();
               if(osname.indexOf("windows") != -1){
                    if(osname.indexOf("nt") != -1 || osname.indexOf("2000") != -1 || osname.indexOf("xp") != -1){
                         type = OSTYPE_WINNT;
                    } else if(osname.indexOf("ce") != -1){
                         type = OSTYPE_WINCE;
                    } else {
                         type = OSTYPE_WINDOWS;
               } else if(osname.indexOf("linux") != -1 || osname.indexOf("bsd") != -1){
                    type = OSTYPE_LINUX;     
               } else if(osname.indexOf("mac os") != -1 || osname.indexOf("macos") != -1){
                    type = OSTYPE_MAC;
               } else if(osname.indexOf("solaris") != -1){
                    type = OSTYPE_SOLARIS;     // could also be old freebsd version
               } else if(osname.indexOf("netware") != -1){
                    type = OSTYPE_NETWARE;
               } else if(osname.indexOf("os/2") != -1){
                    type = OSTYPE_OS2;
               } else {
                    type = OSTYPE_UNKNOWN;     
          return type;
          @return the prefix to execute a shell command. For example "command /c " to execute a shell command on windows9X machines.
     public static String getShellString()
             String appendStr = "";
             int ostype = getOs();
             if(ostype == OSTYPE_WINDOWS){
                  appendStr = "command /c ";
             } else if(ostype == OSTYPE_WINNT || ostype == OSTYPE_WINCE){
                  appendStr = "cmd /c ";     
             } else if(ostype == OSTYPE_LINUX || ostype == OSTYPE_MAC){
                  appendStr = "/bin/sh -c ";     
             } // add other shell executers
             return appendStr;
     /** execute a command and ignore any output it generates (output is sent to System.out and System.err).
          It is valid to pass a command containing multiple parameters
          @param command the command to execute
          @return the exit code the process generated
     public static int exec(String command)
          throws IOException
          return exec(command, false, System.out, System.err);
     /** execute a (shell) command and ignore any output it generates (output is sent to System.out and System.err).
          It is valid to pass a command containing multiple parameters.
          NOTE: only windows (command), winnt (cmd) and linux (sh) shell executions are supported.
          @param command the command to execute
          @param shellCommand should the command be handled as an internal shell command?
          @return the exit code the process generated
     public static int exec(String command, boolean shellCommand)
          throws IOException
          return exec(command, shellCommand, System.out, System.err);
     /** execute a (shell) command and catch the output it generates.
          It is valid to pass a command containing multiple parameters.
          NOTE: only windows (command), winnt (cmd) and linux (sh) shell executions are supported.
          @param command the command to execute
          @param shellCommand should the command be handled as an internal shell command?
          @param output the output stream to send the output of the process to (output is handled as textual data)
          @return the exit code the process generated
     public static int exec(String command, boolean shellCommand, OutputStream output)
          throws IOException
          return exec(command, shellCommand, output, System.err);
     /** execute a command and catch the output it generates.
          It is valid to pass a command containing multiple parameters.
          @param command the command to execute
          @param output the output stream to send the output of the process to (output is handled as textual data)
          @return the exit code the process generated
     public static int exec(String command, OutputStream output)
          throws IOException
          return exec(command, false, output, System.err);
     /** execute a command and catch the output (stdout) and errors (stderr) it generates.
          It is valid to pass a command containing multiple parameters.
          @param command the command to execute
          @param output the output stream to send the output of the process to (output is handled as textual data)
          @param error the output stream to send the error output of the process to (output is handled as textual data)
          @return the exit code the process generated
     public static int exec(String command, OutputStream output, OutputStream error)
          throws IOException
          return exec(command, false, output, error);
     /** execute a command and catch the output (stdout) and errors (stderr) it generates.
          It is valid to pass a command containing multiple parameters.
          NOTE: only windows (command), winnt (cmd) and linux (sh) shell executions are supported.
          @param command the command to execute
          @param shellCommand should the command be handled as an internal shell command?
          @param output the output stream to send the output of the process to (output is handled as textual data)
          @param error the output stream to send the error output of the process to (output is handled as textual data)
          @return the exit code the process generated
     public static int exec(String command, boolean shellCommand, OutputStream output, OutputStream error)
          throws IOException
         if(shellCommand == true){
              String appendStr = getShellString();
              command = appendStr + command;
         String[] realcommand;
         if(command.indexOf(" ") != -1){
              realcommand = command.split(" ");
         } else {
              realcommand = new String[1];
              realcommand[0] = command;     
         try{
              Process ls_proc = Runtime.getRuntime().exec(realcommand);
               ExecStreamThread execOutput;
               ExecStreamThread execError;
               execOutput = new ExecStreamThread(ls_proc.getInputStream(), output);
               execOutput.start();
               execError = new ExecStreamThread(ls_proc.getErrorStream(), error);
               execError.start();
             int exitvalue = ls_proc.waitFor();     
               if(output != null) { output.flush(); }
               if(error != null) { error.flush(); }
               return exitvalue;
          } catch(Exception e){
               throw new IOException(e.getMessage());
     public static void main(String[] args)
          try{
               int code = ExecUtil.exec("javac test.java", System.out, System.err);
               System.out.println("Exit code was : " + code);
               code = ExecUtil.exec("java test", System.out, System.err);
               System.out.println("Exit code was : " + code);
          } catch(IOException e){
               System.out.println("failed due to exception: " + e);
          try{
               int code = ExecUtil.exec("dir/w", true, System.out, System.err);
               System.out.println("Exit code was : " + code);
          } catch(IOException e){
               System.out.println("failed due to exception: " + e);
}

Similar Messages

  • How to call other program in java

    how do i write code to call a program, software in window envirenment?
    such as opening Microsoft Excel files using Microsoft excel?
    plz help

    public static void main(String[] args)
             try
                     Runtime.getRuntime().exec("Book1.xls");
             catch (IOException ed)
    }i tried, but it not working , plz help

  • Programs that manage other programs

    I just saw a program called gambitron that basically plays chess on yahoo games on behalf of the owner. What fascinated me was that the program was somehow aware of the positions of the pieces on the board, and able to move them. How is something like this accomplished? It thoroughly peaked my curiousity.
    Thanks

    What does that mean/how does it work?Yeah, I wondered that too. Wikipedia has an article:
    http://en.wikipedia.org/wiki/Screen_scraping
    (including the fact that it's quite an old procedure
    in computing).
    Also Google, where I'm looking now...Yes, it's a very old technique, but there aren't much else that you can do if you can't hook into the other program, or communicate with some kind of service.
    Kaj

  • How to create a job thru ABAP program for calling a program with variant???

    Hello experts,
    can u give me step wise procedure to create jobs for  a program with a variant name thru ABAP???
    Also, can a transaction can be scheduled as a job to run in background with a variant name???
    Edited by: SAP USER on Jul 22, 2008 6:08 AM

    Hi,
    To create a job through ABAP program you can do the following.
    Go to Menu bar.
    In there, go to   SYTSTEM> SERVICES> JOBS--> DEFINE JOB.
    Then give the JOB NAME and CLASS in the screen that comes up.
    This is how we schedule a program.
    Now, to create a variant for a program -
    First activate your program in SE38. Then execute it .
    Now, click on SAVE button. It will open up  the variant creation screen. Give the details there like variant name and value for the fields. Save and come back.
    Hope this helps.
    Regards,
    Hari Kiran

  • Call other programs from Java

    There is this command line program I want to wrap with a Java class in order to make it available so that it can be transparently called.
    How can I call or execute external programs/commands from JAVA...
    is not native code that I want to run...
    Thank you

    Runtime.getRuntime().exec("your command line")... you could read more about it in the API documentation.

  • My firefox program open with other program and i don't know why

    when i open firefox program another program (ie8) open in the place of firefox and i don't know why
    and also i prefere to use firefox program
    please help me
    thanks
    == This happened ==
    Every time Firefox opened
    == i open firefox ==
    == User Agent ==
    Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; FunWebProducts; .NET CLR 2.0.50727; OfficeLiveConnector.1.3; OfficeLivePatch.0.0; AskTbGLSV5/5.8.0.12217)

    Have you read [[Links do not open in Firefox]]

  • Running programs from within other programs

    I have a problem with running small java-applications form within another.
    If I put the BufferedReader for errors first, sometimes it gets stuck
    (without throwing any exceptions) when I want to run applications that
    are working without any errors, when I run them manually. But if I put
    the standardoutput-Reader first, it gets stuck (again without throwing
    any exceptions), when an error occurs. This "getting stuck" always happens
    in the line where the "while" is. It seems that nothing can be read, but its
    not null too.
    Thats part of my code:
    String cmd = "java -cp rightDirectory MyFile";
    Runtime rt = Runtime.getRuntime();
    Process pr = rt.exec(cmd);
    BufferedReader pout = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
    String line2;
    while ((line2 = pout.readLine()) != null) {
    System.out.println(line2);
    pout.close();
    BufferedReader peout = new BufferedReader(new InputStreamReader(pr.getInputStream()));
    String line;
    while ((line = peout.readLine()) != null) {
    System.out.println(line);
    peout.close();
    How can this be done, that it works for all cases?

    hi, Have you read http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html ?

  • How to get the layout values in F4 help from Other program ?

    Hello All,
           I have a program P1which calls other program P2 .
    When I execute P1 I have a parameter for Layout with F4.
    When I press  F4 I want the help values which are there in the lay out of the other program P2.
    For this I'm using the following code :-
    DATA  spec_layout        TYPE  disvariant.  "specific layout
    DATA  v_save             TYPE  c.           "Save mode
    DATA  gs_variant         TYPE  disvariant.  "for parameter IS_VARIANT
    DATA  gv_exit            TYPE  c.
    PARAMETERS:  p_vari  TYPE disvariant-variant.
    AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_vari.
    *  gs_variant-report  = sy-repid.
    *  gs_variant-variant = p_vari.
      CLEAR gs_variant.
      MOVE  '/BSHP/FP_CALL_OF_PLAN' TO gs_variant-report. "Report von Original CALL_OF_PLAN
      gs_variant-variant = p_vari.
      CALL FUNCTION 'LVC_VARIANT_F4'
        EXPORTING
          is_variant = gs_variant
          i_save     = v_save
        IMPORTING
          e_exit     = gv_exit
          es_variant = spec_layout
        EXCEPTIONS
          not_found  = 1
          OTHERS     = 2.
      IF sy-subrc NE 0.
        MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      ELSE.
        IF gv_exit NE 'X'.
    *     set name of layout on selection screen
          p_vari    = spec_layout-variant.
        ENDIF.
      ENDIF.
    But still I'm not able to get the values.
    Can anyone help me out ?
    Regards,
    Deepu.K
    null

    This question has been asked and answered many times before.  Please read the following blog for a good start:
    /people/yohan.kariyawasan/blog/2009/03/18/ui-framework-news-f4-help
    Before posting further please do a search in this forum and also read the rules of engagement listed here:
    https://www.sdn.sap.com/irj/scn/wiki?path=/display/home/rulesofEngagement
    I'm now locking this thread as it is a duplicate of an already answered question.
    Thank you,
    Stephen
    CRM Forum Moderator

  • Call two programs from one program.

    HI all,
    I have two BDC interface in seperate programs.
    I want to call that two programs from one program based on a condition.
    Is there any way.
    I tried with call transaction 'SE38'.
    set parameter ID but it is taking buffered value.
    Thanks,
    sri

    use submit program to call other program.
    SUBMIT prog|(field) [AND RETURN] [options].
    also if you want BDC prog, then probably you can call the BDC recording using PERFORM bdc_prog (in program name).
    PERFORM (<fsubr>)[IN PROGRAM (<fprog>)][USING   ... <pi>... ]
                                           [CHANGING... <pi>... ]
                                           [IF FOUND].
    http://help.sap.com/saphelp_nw2004s/helpdata/en/9f/db9dd035c111d1829f0000e829fbfe/content.htm

  • How can we call creen of one program from some other dynpro Program

    Hi,
    Is it possible to call a screen of one program from some other program as pop up or full scree.
    please give you input if it is possible.
    Thanks in advance.
    Thanks and Regards,
    Praveen.

    Hi,
    But is there is any function module through we can call screen of some other program. PLease let me know if you are aware of that.
    Thanks

  • Language other than bash for calling external programs

    Hi,
    sorry for the unspecific title, I couldn't think of a better summary.
    The problem is this: I have written a script to encode a DVD to H264 and Vorbis. Since it basically just performs some (OK, by now: A lot of.) management and then calls a external programs, Bash was the obvious choice.
    However, the administrative stuff the script does (evaluating user input, calculations etc.) was already a nightmare to code in Bash (lack of arithmetic, lack of data types, proper functions, lack of c-like structs etc.), and I now want to make it even more flexible in what the user (me) can ask of it (it's also going to use a config file, which is another thing that gets ugly fast). Frankly, I can't stomach that.
    So the question is this: What language would be sensible for a program the most important function of which is calling other programs?
    Simply executing them from the main program isn't enough, unfortunately, because I want to make use of multicore system by for example simultaneously extracting streams and encoding them (right now that is done through named pipes), ideally I'd need a way to multithread not internal functions/routines but external programs (Through pipes or whatever).
    I'd prefer an interpreted language, but it's not a requirement.

    I second what peets said. Perl is definitely you're best option here, in my opinion anyways. It has the best (by which I mean easiest) system interface of any scripting langauge I've worked with, and if you want a simple configuration file reader, perl's regex'es are king. Perl also takes a lot of features from the shells, such as the file test operations. If the project get's really big and hairy though, it might be worth considering python as a cleaner, stricter alternative.
    Hope that helps!

  • Trying to call a program within a Java application

    Hi,
    I am trying to call a program from within a Java application.
    Here's my code:
    // the program siftwin32 takes it's input via < and outputs using >
    String command = "C:/Demo/siftDemoV4/siftDemoV4/siftWin32.exe < c:/demo/query.pgm >c:/demo/query.key";
    Process p = Runtime.getRuntime().exec(command);
    int val = p.waitFor();
    The program "siftwin32" never executes. That is, the .key file is never generated. The value returned from the waitFor() is 1 which I know is bad.
    I'm not sure what the problem is. Running siftwin32 from the command line works fine.
    Any help would be greatly appreciated!
    Thanks,
    Neal
    Edited by: nealchecka on Nov 28, 2007 10:01 PM

    It's probably being executed; you're just passing it bad arguments and confusing it.
    The < and > operators are something that the shell uses. They're not arguments to programs typically. When you run it from the command line, it's going through a shell (the command line is the shell, that is to say a program for running other programs).
    When you run Runtime.exec, it doesn't go through the shell. The program is executed directly. If you use the version of exec() that takes a single string argument, the string is tokenized on whitespace and the tokens are used as the executable and its arguments.
    You have a few options.
    One is to write a shell script (or I guess a batch file on windows) that does the i/o redirection, and invoke that script.
    Another can be to send all these arguments to the shell by passing them on the command line to the shell, or to "start" or something (I'm not sure about what Windows does specifically). You can find examples online.
    Or you can do the redirection yourself, in the Java program. After you exec, use the resulting Process object to get the standard input, standard output, and standard error streams, and write/read/read those streams. If you like you can read from or write to files with those streams. Personally I generally prefer this option, since it has less dependency on the OS, and it's more flexible, and it's more tightly integrated into the Java class.

  • C programming System calls

    Ok I am starting to program in C and I would like to make useful apps
    but I don't know how to call other programs within linux from C
    I'm looking for guides that help on linux "C" programming.
    I've tried some guides I googled but I tried their examples and don't get anything
    they appear obsolete (1996 or 1999)
    Guide I found
    Any sugestions?
    Thanks

    toofishes wrote:Note that calling other programs is not a common occurrence for C programs...nor is that a "system call". A system call is calling a kernel-provided function such as stat or access, not calling another user-space program on the system.
    U call other programs with a system call, u can open a file and do operations on it with system calls too.
    What he want is a GUI like gtk or a framework

  • Calling java program from PL/SQL code

    Dear,
    How to develop and call a java program from PL/SQL code?
    What about if this program will call other java programs?

    Perhaps the Java Developer's Guide would be a good place to start
    http://download.oracle.com/docs/cd/B19306_01/java.102/b14187/toc.htm
    Justin

  • Call Selection screen of other program..

    Hello ABAP Experts,
    I have a situation where in I need to call the selection screen of one sap generated standard program without executing that program & get the values of those selection variables in to my custom program. Is there a way to do that? Please help.
    Thanks,
    Pandu

    If it is static or one program I could have done like that. But the nature of the  program is dynamic & not just one program.

Maybe you are looking for

  • Apple told me to basically suck one....?

    So, long story short, I've been using Itunes for 5 years now. I set my security questions along time ago, and, over time, I've forgotten them. Just recentely I redeemed a 25 dollar code, and now, when I go to download something, it asks me my questio

  • Help! No signal and blank recordings

    We have had BT vision for 6 months, and have been quite disappointed so far! We have a brand new digital aerial and can watch freeview perfectly through the inbuilt box in our tv. However, when we switch to the BT vision box, almost all the 'good' ch

  • Sap isu-utility help find

    Hi, i am new to  industry specific solution utility,now i need to download help file for that.can anybody pls tell me how to do it? thanks, raman

  • Using the officejet4500 with the chrome book

    i have the officejet 4500 wireless printer and need to figureout how to get it to work with the chrome books? i got as far as emailaddress for the cloud printing and have no clue how to make an email address for the printer. is there an app for the c

  • Using timemachine after installing lion

    If I install Lion on a clean harddisk in my MacBook, will I be able to copy items from the TimeMachine backup I made under SnowLeopard? And will it be possible to use the TM backup for migration? Thanks in advance!