Service Virtualization

Expand all | Collapse all

How to print output of java code in Execute Script JSR-223

  • 1.  How to print output of java code in Execute Script JSR-223

    Posted 01-02-2019 12:17 PM

    Hi All,

     

    I have a Java code written in JSR-223 step. The output of java code is stored in variable of String type.

     

    Expected Output of JSR-223: abcdef [IDE gives correct output]

    Actual Output: Result is class trail [Not working]

     

    ISSUE: What should I do , so that java code output can store actual result. Kindly assist.

     

    Java code:

    public class trial {
       public static void main(String[] args) {
       String headerOne = "abc";
       String headerTwo = "def";

       String finalOutput = headerOne + headerTwo;

       System.out.println(finalOutput);
       }
    }

     

    abrsh01 Rick.Brown deoma03 Kevin_Bowen Josh_Hendrick



  • 2.  Re: How to print output of java code in Execute Script JSR-223

    Posted 01-02-2019 01:16 PM

    Just run, the below as the content of your script step:

     

       String headerOne = "abc";
       String headerTwo = "def";

       String finalOutput = headerOne + headerTwo;

       System.out.println(finalOutput);

     

    As the name of the step says, it runs the script that is stored in the step. For Beanshell this script can contain Java statements.

    You can think of it like the script step run the content of some main() method. The step does NOT run itself the main() method of some class.

     

    If you want to use your class then you need to put your compiled class in a jar file and add that jar to the HotdDeploy directory such that DevTest can load the class. Then your script can execute the statement: 

    trial.main();

     

    If you want to store your string concatenation for later use in your testcase and/or vsm then you can add:

     

    testExec.setStateValue("finalOutput", finalOutput);

     

    Now, after the script step has executed a property will be available within the environment which you can use in other steps as {{finalOutput}}

     

    Hope this helps,

    Danny



  • 3.  Re: How to print output of java code in Execute Script JSR-223

    Posted 01-11-2019 07:07 AM

    Hi, 

    I have created a jar file and placed it in hotDeploy folder.

    Please help me with the code to access it in lisa project. my class name is "xml".



  • 4.  Re: How to print output of java code in Execute Script JSR-223

    Posted 01-12-2019 11:27 AM

    Hi,

     

    Not enough information right now to help you out.

     

    Is your class name "xml" all in small caps? (Just asking because java best practices start class names with capital letter and as java is case sensitive it does matter)

    Have you declared your class in a java package? If so, what is the package name?

    What is the method inside the xml class that you want to call?

    Where do you want to use this class? Inside an execute script step? Or directly inside a property definition?

     

    To verify how to reach the class inside the .jar file can you add the output of following command:

     

    <yourJavaJdkHome>\bin\jar -tvf <yourJarFile>.jar

     

    Cheers,

    Danny