Calling a Java Program from another

Hi,
I am putting together a Java program which will launch other Java programs.
I was thinking of doing this by calling unix scripts from my java program by getting an instance of the Runtime object and then calling the exec method with the script name.
I am just wondering what is the best practice when doing this? i.e., dealing with timeout issues, and errors in the scripts etc.
I have read this article, which is quite good: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1
Is there any code available on the web that deals with all of the basic error scenarios?
Is it good practice to use a java program to call another java program? Or is there a better way of doing this?
The main reason I am using java is because I am most familiar with it and I need to connect to a database as well and do some manipulation of results returned.
Any thoughts or suggestions would be much appreciated.
Message was edited by:
LondonJavaDev

Thanks Prometheuzz
Is the main difference between your suggestion and
using the Runtime.getRuntime().exec(cmd) way of doing
things is that in the former both eill run as the
same process (Thread) but in the second scenario the
.getRuntime().exec(cmd) will start a separate new
thread?
Many ThanksCorrect. But you can create a sort of wrapper class for A which implements Runnable and stuff it in it's own thread. If you're dealing with Java code only, I advise you to create threads yourself instead of letting your OS handle them.
Here's a demo:
class Foo {
    public static void main(String[] args) {
        (new Thread(new ARunner())).start();
        B.main(null);
class ARunner implements Runnable {
    public void run() {
        A.main(null);
class A {
    public static void main(String[] args) {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) { /* ... */ }
        System.out.println("A");
class B {
    public static void main(String[] args) {
        System.out.println("B");
}Of course, you can create a wrapper for B as well.

Similar Messages

  • Calling one java program from another

    Hello,
    How can I start another java program from one? Lets
    say I want Second.java to start by calling it from
    First.java. How do I do it? The two programs are given
    below. Any help is appreciated.
    Thanks,
    Amanda
    First.java
    import java.io.*;
    import java.lang.reflect.*;
    public class  First
         public static void main(String[] args)
              Process theProcess=null;
              System.out.println("Hello World from First.java!");
              String second=new String("Second.java");
              //System.load(second);
              //Runtime.getRuntime().load(second);
              try
                   theProcess=Runtime.getRuntime().exec( "Second.java"
                   System.out.println("after exec");
              catch (IOException ioe)
                   System.out.println(">>IOException thrown in
    First.java while calling
    Second.java."+ioe.getMessage());
    Second.java
    public class  Second
         public static void main(String[] args)
              System.out.println("Hello World from Second.java!");
    }

    Thanks, warnerja
    What if Second.java is on a remote machine i.e.
    First.java and second.java are on different machines?
    Will I have to use RMI?Well, you'd have to do some kind of remote invocation. Depends on what kinds of apps house the classes. There are all kinds of ways - web services, servlets, RMI, (maybe others...)
    That's a totally different question than what you originally posted, and would have been very relevant to say so in the first place.

  • Not able to execute one Java program from another one via. exec()

    Hi,
    I am new to this forum, so I might be asking a very trivial quetion.
    My program Ap1 is :
    class Ap1
    public static void main(String a[]) throws Exception
    Runtime rt=Runtime.getRuntime();
    Process p=null;
    p=rt.exec("javac,Pr.java");
    Runtime.getRuntime().exec("javac,Pr.java");
    When I run the above program, I get below error at the line 7(bold one):
    D:\batchwe>javac Ap1.java
    D:\batchwe>java Ap1
    Exception in thread "main" java.io.IOException: CreateProcess: javac,Pr.java err
    or=2
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(ProcessImpl.java:81)
    at java.lang.ProcessImpl.start(ProcessImpl.java:30)
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:451)
    at java.lang.Runtime.exec(Runtime.java:591)
    at java.lang.Runtime.exec(Runtime.java:429)
    at java.lang.Runtime.exec(Runtime.java:326)
    at Ap1.main(Ap1.java:7)
    Path is set as below(which seems correct):
    D:\batchwe>path
    PATH=C:\Program Files\Java\jdk1.5.0_05\bin
    I am just wondering how to call one Java program from another one?!
    Please help.
    thanks
    Rashmi

    Hi diptaPB,
    Thanks for your suggestion.
    Here is how I tried it again, but not getting the desired output:
    Class Pr looks like below -->
    class Pr
    public static void main(String a[])
    System.out.println("hi from process");
    When I run 'Pr' from command prompt, i get the desired output:
    D:\batchwe>java Pr
    hi from process
    However, when I call 'Pr' from another program Ap2.java, it seems that it does not call 'Pr':
    class Ap2
    public static void main(String a[]) throws Exception
    Runtime rt=Runtime.getRuntime();
    Process p=null;
    String[] cmd=new String[2];
    cmd[0]="javac";
    cmd[1]="Pr.java";
    p=rt.exec("cmd");
    D:\batchwe>javac Ap2.java
    D:\batchwe>java Ap2
    ************* no output appears here***********************
    Looking forward for your help.
    regards
    Rashmi

  • How to call a Java class from another java class ??

    Hi ..... can somebody plz tell me
    How to call a Java Class from another Java Class assuming both in the same Package??
    I want to call the entire Java Class  (not any specific method only........I want all the functionalities of that class)
    Please provide me some slotuions!!
    Waiting for some fast replies!!
    Regards
    Smita Mohanty

    Hi Smita,
    you just need to create an object of that class,thats it. Then you will be able to execute each and every method.
    e.g.
    you have developed A.java and B.java, both are in same package.
    in implementaion of B.java
    class B
                A obj = new A();
                 //to access A's methods
                 A.method();
                // to access A's variable
                //either
               A.variable= value.
               //or
               A.setvariable() or A.getvariable()

  • CALL A JAVA PROGRAM FROM ABAP -- NEED HELP

    Hi all,
    Can somebody tell me how to call a java xml code from an ABAP program? First let me know if at all, is it possible to call a java program from SAP that is from an ABAP executable progam?
    Please suggest me in this. All your inputs in this is valuable to me and highly appreciated.
    Thanks in advance,
    Vaishnavi Varadarajan

    Hi,
    Previous explanation i gave is not correct.
    ABAP web services expose the function modules outside R/3, so that other technologies like Java can consume that webservice and read the data in R/3 using the function module.
    For your requirement, create a Java web service to read the XML. Consume this webservice in ABAP. This way you can read the XML from ABAP.
    Regards
    Srikanth KV.

  • Calling a java program from a perl CGI

    The perl program takes form data, does stuff with it, writes it to a file, and then calls a java program to encrypt it, and email it.
    When I telnet to the server, I can run the java program using:
    java CryptoMail.class [email protected] data.txt
    The java program uses jar files which are defined in the Classpath in an .sh script in the etc/profile.d/ directory.
    When I run the perl cgi from the web, it doesn't work. I figured that it wasn't getting the classpath defined because the user wasn't actually logging in so I tried it with java -classpath etc...
    Any ideas???
    I tried the following:
    ==================
    $cmdtorun='/usr/j2sdk1.4.0_02/bin/java CryptoMail $email $file';
    system($cmdtorun);
    ==================
    and also tried variations of the following:
    ==================
    /usr/j2sdk1.4.0_02/bin/java -classpath .:/home/sites/site1/web/cgi-bin/activation.jar:/home/sites/site1/web/cgi-bin/mail.jar /home/sites/site1/web/cgi-bin/CryptoMail [email protected] /home/sites/site1/web/cgi-bin/wellsfargo.txt
    ==================

    >
    Any ideas???Write a script file that explicitly paths everything.
    Test it by setting the class path and path to nothing in the environment and seeing if it works.
    Redirect errors from everything to a log file.
    Then run the script from your perl program. Verify that it did not return any errors. Verify there are no errors in the log file.

  • Requesting an overview of how to call a Java program from EBS.

    Hi,
    I am an experienced Java developer in the middle of an implementation of EBS 12g. I am very new to EBS and I'm not sure where to start on this. I need to provide users the ability to call a Java program that prints a report from EBS. I would be grateful if somebody would outline the general approach that should be used. Links to documentation/tutorials are appreciated also.
    Thanks,
    Mike

    Hi,
    I need to provide users the ability to call a Java program that prints a report from EBS. Are you referring to Java concurrent programs? If yes, please see the documents referenced in this thread.
    Java Concurrent Programs
    Java Concurrent Programs
    Regards,
    Hussein

  • Can I call a Java program from a SQL Server Trigger?

    Hello,
    I want to encrypt some data in a database column in SQL Server. Today I am using java code to encrypt the value and store it in the database using JDBC.
    Now I want to use a VB client to store the encrypted value in the SQL Server DB. Since the encryption is handled by a java class, can I write a trigger in SQL Server that while inserting the raw data, calls the java class for encrypting the value and then inserts the encrypted value into the column?
    In general, is it possible to call a java class from a SQL Server trigger?
    Thanks
    Bipin

    Here are 3 examples of code for insert, update and delete:
    CREATE TRIGGER [PLI_INSERT_TRIGGER] ON [dbo].[PLI]
    FOR INSERT
    AS
    Declare @cmd sysname, @code sysname, @list sysname
         Select @code = PLI_K_COD, @list = PLI_K_LISTINO from inserted
         Set @cmd = 'java mirrorDb.Copy PLI INSERT ' + @code + ' ' + @list
         EXEC master..xp_cmdshell @cmd
    CREATE TRIGGER [PLI_UPDATE_TRIGGER] ON [dbo].[PLI]
    FOR UPDATE
    AS
    Declare @cmd sysname, @code sysname, @list sysname
         Select @code = PLI_K_COD, @list = PLI_K_LISTINO from inserted
         Set @cmd = 'java mirrorDb.Copy PLI UPDATE ' + @code + ' ' + @list
         EXEC master..xp_cmdshell @cmd
    CREATE TRIGGER [PLI_DELETE_TRIGGER] ON [dbo].[PLI]
    FOR DELETE
    AS
    Declare @cmd sysname, @code sysname, @list sysname
         Select @code = PLI_K_COD, @list = PLI_K_LISTINO from deleted
         Set @cmd = 'java mirrorDb.Copy PLI DELETE ' + @code + ' ' + @list
         EXEC master..xp_cmdshell @cmd
    you must go "sql server entreprise manager" right click on the table you want to add triggers and select: all activities, manage triggers.
    You have 3 examples: for an insert, for an update and for a delete
    ON [dbo].[PLI] specify the table on which you want to setup trigger.
    FOR DELETE, INSERT, UPDATE specify the event.
    The Declare statement create the variables in which I want to put some values to pass to the java program, for example which table, which event, which key fields.
    the "Select @code = PLI_K_COD, @list = PLI_K_LISTINO from inserted" set the variables with the value of the columns of the table I am interested to read from my java program, for example the variable @code receive the value of the column pli_k_kod (is the key) of the table PLI.
    The "Set @cmd = 'java mirrorDb.Copy PLI DELETE ' + @code + ' ' + @list " prepared the variable @cmd with the java command followed by the package.classname and parameters.
    The EXEC launch the command to the operating system.
    Daniele

  • Calling a JAVA program from an RFC in SAP ABAP

    Hi All,
    I have an RFC for a specific purpose in which I need to call a JAVA program whose path is given to me. Please suggest a possible process to do the same .
    Your's <removed by moderator> responce is highly appreciated.
    Thank you.
    Edited by: Thomas Zloch on Nov 26, 2010 1:33 PM - priority normalised

    Hi Priyanth,
      I am not sure but check this out
      If you are using any commands , check if that command is configured in SM69

  • Calling one report program from another

    Hi All,
             i am calling one report program from my program & exporting it's output to SAP-SPOOL using SUBMIT statement. I want to fetch the spool no in my program for this spool. Please guide me how this can be achieved? i have tried SYST- SPONO but it's not working.
    Thanks & Regards,
    Chetan.

    Hi,
    Initially check this it might be helpful  to u.
    http://help.sap.com/saphelp_sm40/helpdata/en/30/cc154219fce12ce10000000a1550b0/content.htm.
    Even use the function module slike Job_open , Job_close  this is also one of the methid to achive spool requests.
    Regards,
    Sana.
    Reward with points if found helpful..

  • How to call a C function calling a Java Method from another C function ?

    Hi everyone,
    I'm just starting to learn JNI and my problem is that I don't know if it is possible to call a C function calling a Java Method (or doing anything else with JNI) from another C function.
    In fact, after receiving datas in a socket made by a C function, I would like to create a class instance (and I don't know how to do it too ; ) ) and init this instance with the strings I received in the socket.
    Is all that possible ?
    Thank you very much for your help !

    Hard to understand the question, but by most interpretations the answer is going to be yes.
    You do of course understand that JNI is the "API" that sits between Java and C and that every call between the two must go through that. You can't call it directly.

  • How to call a Dialog Program from another Dialog Program

    Dear All,
    How can I call a dialog program with return value from another dialog program?
    Regards,
    Alok.

    Hi Alok,
    1. Insted of creating 2 different Dialog program. It's good to create as many screens as you want in same module pool program. Any way you can use the different TCODE for each screen.
    2. Another and The best way is to create a function group and then inside function group use the function 2 module... In the function group define a global variable which will be present for both the function group if they are getting executed in sequence. and inside the Function Module call the screens using command " call screen <screenno>".
    3. You can use set / get parameter to pass values of a field between two dynpro program.

  • Trouble in Unix calling a Java utility from another shell script

    Hi,
    I'm able to run a shell script (call it script B) in Unix which calls a Java utility. When I call script B from another script (script A) I get the error below. I have done much work to make sure the environment variables are the same.
    java.util.zip.ZipException: error in opening zip f
    ile
    at java.util.zip.ZipFile.open(Native Method)
    at java.util.zip.ZipFile.<init>(ZipFile.java:69)
    at com.something.runtime.ZipFileApplicationSource.<init>(ZipFileApplicationSource.java:22)
    at com.something.runtime.ZipFileRuntime.main(ZipFileRuntime.java:35
    ANY HELP WOULD BE GREATLY APPRECIATED.
    Thanks

    Here's my guess, if this doesn't work, post your scripts and maybe that will help.
    When you run your script B, you said it works. I think the problem is that when you run script B, your shell and envirenment variables are set by the window you're working in. When you call script B from script A, it creates a new shell to run it and it doesn't run the files that are run when you start a terminal or a telnet(it won't run .cshrc, .kshrc or .login). Depending on what OS you're using, I think the default shell is sh for most of them. The first line of script B should be
    #!/(your bin directory)/(shell you're using)
    for me it's
    #!/bin/tcsh
    then you might want to run your startup file to set envirenment variables, paths, etc...
    source (your user directory)/(the file name)
    for me it's
    source ~/.cshrc
    Then if you need to set the environment variables in your script.
    set CLASSPATH /usr/local/j2ee/bin.....
    set PATH /bin:/xyz....
    If that doesn't help post your scripts, or if I didn't explain it will enough, just ask.

  • Please help with executing a java program from another java program

    Hi, I have tried to execute a very simple java program which is printed out "Hello world" from another java gui program when the user click on the "START" button. However, I don't receive any print out on the terminal when they click it. These two programs are being developed in Unix environment. Here is the code that I am trying to use.
    Runtime r = Runtime.getRuntime();
    r.exec("java HelloWorld");
    p.s. HelloWorld is java class of the HelloWorld.java

    I think you have to luanch your MS Prompt first before executing the command.

  • Calling a java application from another

    Hello,
    i have noticed a strange behaviour of my java app when i open it from another java app. When i run the core application with a bat file using jre1.6 everything works fine. Now what i want to do is create an application that checks the existance of jre 1.6, downloads it if it does not exist and run real application with the new jre. The second small app runs with a small jre 1.4.2. In reality it seem to work fine, it downloads the jre1.6 folder and exits normally. However when the real application is launched it behaves strangely without throwing any exceptions. And this behaviour seems to be random. for example, a jpanel may not show any components, or the whole jframe may close suddenly. Does anyone have any idea what is going on? Is there a chance that some classes of the first jre remain in memory and conflict when the second application is launched?

    Just a wild guess: maybe you're invoking the java 1.6 binaries, but JAVA_HOME or something is pointing to java 1.4 libraries (or vice-versa), and you've found some weird interaction issue.

Maybe you are looking for

  • DataGrid.placeSortArrow(). How to use it

    I want to have a dataGrid sorted upon dataProvider update, and I want the grid to display correctly the sort Arrow so that the user know what is the sorted column, and in what order. The placeSortArrow method seems to be a good candidate, and the doc

  • ABAP Debugger: assign user to external breakpoint

    Hi, when I assign different users to an external breakpoint the program still stops when it is executed with the "original" user which is not assigned to the BP anymore. Has anybody any solution for this behavior? The procedure I use is the following

  • Restricting Good Receipt Unless Production Order Confirmed

    Hi Experts, I want to Restricting Good Receipt Unless Production Order Confirmed I am doing following thing but the error is occured. Use Tcode BS02 ( Customising USer status profile). 2. Select the order type which you want to configure (eg:PP000001

  • 1602 errors when trying to install the Extended Asian Language font pack for Adobe Reader XI

    My company is trying to deploy Adobe Reader XI 11.0.10 with the Extended Asian Language font pack via SCCM 2007, and some computers are failing the install of the Extended Asian Language font pack with 1602 error.  Reader is installing successfully,

  • How to increase painting performance ?

    First off all, i'd like to apologize for my poor english, because I'm from brazil, and even knowing very well how to speak english, my spelling sometimes comes out really nasty. Now getting to the subject... ;^) I study computer science here in brazi