Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: How to pass a return value from a jar file to a batch file  (Read 22697 times)

0 Members and 1 Guest are viewing this topic.

simhadri1985

    Topic Starter


    Rookie

    How to pass a return value from a jar file to a batch file
    « on: August 19, 2009, 09:08:28 AM »
    Hello All,
                    I am running a jar (test.jar) file from a batch file(mybatch.bat) using the command "java -jar test.jar input1". The jar is executed properly. Now in a method in the jar file i am setting a flag bit(true/false) in an if condition. I want to retrieve this value in to batch file(mybatch.bat) which called the jar file and store it in a variable in the same batch file. In this batch file I will put an if condition which will run another jar. The second batch will be called only if the IF condition in the batch file is satisfied.

    I am struggling to retrieve the flag bit value from the first jar into the same batch file after the execution of first jar is completed.

    Salmon Trout

    • Guest
    Re: How to pass a return value from a jar file to a batch file
    « Reply #1 on: August 19, 2009, 10:25:09 AM »
    if the jar file can echo to the console (stdout) then you can use FOR to capture the output, or if it can write a file you can read that file.

    simhadri1985

      Topic Starter


      Rookie

      Re: How to pass a return value from a jar file to a batch file
      « Reply #2 on: August 19, 2009, 10:43:13 AM »
      Suppose If i have the code like this,

      class  Test
      {
         public static void main(String[] args)
         {
          Test test = new Test();
          int ret = test.doNothing();
      System.out.println("------------ ret ------------"+ret);
         }

         int doNothing(){
            int i = 33;
            return i;
         }
      }

      Now if the ret value is printed on the console as :

      ------------ ret ------------"33

      Now how can i capture the value 33. Can you please provide me the code for that.
      « Last Edit: August 19, 2009, 11:31:55 AM by simhadri1985 »

      Salmon Trout

      • Guest
      Re: How to pass a return value from a jar file to a batch file
      « Reply #3 on: August 19, 2009, 10:58:20 AM »
      Did you miss a double quote off the beginning of your specimen output?

      Did you mean

      "------------ ret ------------"33

      Or did you mean

      ------------ ret ------------33

      ?

      Are the quotes actually present in the console output?

      simhadri1985

        Topic Starter


        Rookie

        Re: How to pass a return value from a jar file to a batch file
        « Reply #4 on: August 19, 2009, 11:00:37 AM »
        Sorry.

        I mean

        ------------ ret -----------33

        Salmon Trout

        • Guest
        Re: How to pass a return value from a jar file to a batch file
        « Reply #5 on: August 19, 2009, 11:07:17 AM »
        By setting the token delimiter to be the minus sign - we can separate the second token from the output and put it into a variable

        Please try this

        Code: [Select]
        for /f "tokens=1-2 delims=-" %%A in ( ' java -jar test.jar input1 ' ) do set token2=%%B
        echo The second token is %token2%
        « Last Edit: August 19, 2009, 11:17:53 AM by Salmon Trout »

        simhadri1985

          Topic Starter


          Rookie

          Re: How to pass a return value from a jar file to a batch file
          « Reply #6 on: August 19, 2009, 11:19:45 AM »
          This is working. Thank you.


          But now I wanted to print the value directly using:

          System.out.println(ret);

          Now I have just the value 33.

          So without using deliminators how can i directly capture this value

          Salmon Trout

          • Guest
          Re: How to pass a return value from a jar file to a batch file
          « Reply #7 on: August 19, 2009, 11:22:41 AM »
          I do not understand what you are asking.



          BatchFileBasics



            Hopeful

            Thanked: 18
            Re: How to pass a return value from a jar file to a batch file
            « Reply #8 on: August 19, 2009, 11:28:01 AM »
            i believe he wants to capture the output with out deliminators  ???
            When the power of love overcomes the love of power the world will know peace - Jimi Hendrix.

            simhadri1985

              Topic Starter


              Rookie

              Re: How to pass a return value from a jar file to a batch file
              « Reply #9 on: August 19, 2009, 11:29:02 AM »
              In the previous case when the output in the console is

              ------------ ret ------------33

              So you have given the code which uses deliminators


              Now i modified my java program like this


              class  Test
              {
                 public static void main(String[] args)
                 {
                  Test test = new Test();
                  int ret = test.doNothing();
              System.out.println(ret);
                 }

                 int doNothing(){
                    int i = 33;
                    return i;
                 }
              }


              which will just print the value:

              33


              Now what is the code to just store the value "33" in a variable in the batch file.

              BatchFileBasics



                Hopeful

                Thanked: 18
                Re: How to pass a return value from a jar file to a batch file
                « Reply #10 on: August 19, 2009, 11:34:03 AM »
                ohh,

                Code: [Select]
                for /f "tokens=1-2" %%A in ( ' java -jar test.jar input1 ' ) do set token2=%%B
                echo The second token is %token2%

                just remove the delims
                When the power of love overcomes the love of power the world will know peace - Jimi Hendrix.

                simhadri1985

                  Topic Starter


                  Rookie

                  Re: How to pass a return value from a jar file to a batch file
                  « Reply #11 on: August 19, 2009, 11:41:19 AM »
                  Thank you very much. Will this works if the batch file in executed in silent mode.

                  Actually When i perfrom some action in my application this bat file will be run in silent mode. I mean user can not see any command prompt window open. This batch file will be copied from server into user's local PC in C:\temp path and will be run in a hidden mode.

                  So is it possible to capture the value "33" in this case.

                  BatchFileBasics



                    Hopeful

                    Thanked: 18
                    Re: How to pass a return value from a jar file to a batch file
                    « Reply #12 on: August 19, 2009, 11:44:32 AM »
                    so your saying is it possible to capture the "33" in 'silent mode'?
                    yes.

                    or do you want to know how to make it in 'silent mode'?
                    When the power of love overcomes the love of power the world will know peace - Jimi Hendrix.

                    simhadri1985

                      Topic Starter


                      Rookie

                      Re: How to pass a return value from a jar file to a batch file
                      « Reply #13 on: August 19, 2009, 11:46:28 AM »
                      I just wanted to know is it possible to capture the value 33 with the code which you have given

                      Salmon Trout

                      • Guest
                      Re: How to pass a return value from a jar file to a batch file
                      « Reply #14 on: August 19, 2009, 11:58:12 AM »
                      I used delimiters and tokens because you showed the java output to be in this format:


                      ------token1-------token2


                      and you said you wanted token2

                       ::)

                      if the output is just one single string then the code to capture the output becomes simpler.


                      Code: [Select]
                      for /f "delims=" %%A in ( ' java -jar test.jar input1 ' ) do set retvalue=%%A