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.

Similar Messages

  • 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

  • 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..

  • 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.

  • 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 one WAD Report from another WAD Report

    Hi All,
              I want to Call one WAD report from another WAD Report and I want to show that Report in a Container of the Calling Report Itself. Kindly give the Solution. Thanks for your support in advance.
    Thanks & Regards
    Shiva

    Hi Shiva,
    There is Command called "SET_TEMPLATE" Set Web Template.
    Here you can select any of your existing web template and choose to open it in current page of in a new window.
    Hope it helps.
    Regards,
    Pratap Sone

  • Invoke one java application from another?

    Hello,
    Can anyone tell me how to Invoke one java application from another?
    Suppose I have a small java application say, Hello.java which has its own main() method and
    I also have another java application, say World.java which has its own main() method too.
    What I want to do is invoke or startup World.java from Hello.java.
    If possible kindly give code examples?

    main is just a normal method so Hello can invoke the main method of World just as it would invoke any other method.
    Kaj

  • How sholud we call one jframe class from another jframe class

    Hi
    In my application i am calling one jframe class from another jframe clas.
    how sholud we make previous jframe inactve when another jframe is invoked?(user sholud not able to make any changes on on parent jframe window when another jframe is invoked)
    Pls reply.

    Sorry for me it is not possible to change existing code,
    pls suggest me any other solution so that i can inactive parent jframe when child jframe execution is going on.

  • Calling one java program (with main method) 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!");
    }

    Stop posting here. The crosspost remark was to alert others that there is a duplicate topic - all answers should be centralized in one so as to not waste people's time duplicating answers when they don't see both topics and the answers therein.

  • 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

  • Itz possible to call one report program into another program

    hi guys,
                i had a doubt that can i call a report program into another report program by declaring their name into the second program like as we calling Includes ,function modules and like class and their methods .
    if  anybody done this or know how to do this,  give me the details and explination about this  friends
    if it possible with screenshots and codings
    Thanks & Regards
    Saigijeo

    Please search before posting, do not ask basic questions here.
    Always do your own work before turning to the community with specific problems.
    Thread locked.
    Thomas
    [Asking Good Questions in the Forums to get Good Answers|/people/rob.burbank/blog/2010/05/12/asking-good-questions-in-the-forums-to-get-good-answers]

Maybe you are looking for