Service Virtualization

 View Only
  • 1.  How to find a length of string in beanshell...?

    Posted Sep 22, 2022 04:14 AM
    Actually my requirement is need to replace the last two characters of a string, below is the script PFA.

    import java.lang.*;
    import com.itko.Lisa.Outputlogmessage.rsp;
    import org.apache.commons.lang3;


    String Str = "Hello world";
    int strlen = Str.length();

    Str = Str.Substring(0,strlen- 2);




    below is the error 
    ============================================================================
    | Error in Script
    ============================================================================
    | Step:        Execute script (JSR-223)
    ----------------------------------------------------------------------------
    | Message:     bsh.EvalError: Sourced file: inline evaluation of: ``import java.lang.*;  import com.itko.Lisa.Outputlogmessage.rsp;  import org.apac . . . '' : Error in method invocation: Method Substring(int, int) not found in class'java.lang.String' : at Line: 9 : in file: inline evaluation of: ``import java.lang.*;  import com.itko.Lisa.Outputlogmessage.rsp;  import org.apac . . . '' : Str .Substring ( 0 , strlen - 2 )
    in inline evaluation of: ``import java.lang.*;  import com.itko.Lisa.Outputlogmessage.rsp;  import org.apac . . . '' at line number 9
    ----------------------------------------------------------------------------
    | Trapped Exception: bsh.EvalError: Sourced file: inline evaluation of: ``import java.lang.*;  import com.itko.Lisa.Outputlogmessage.rsp;  import org.apac . . . '' : Error in method invocation: Method Substring(int, int) not found in class'java.lang.String' : at Line: 9 : in file: inline evaluation of: ``import java.lang.*;  import com.itko.Lisa.Outputlogmessage.rsp;  import org.apac . . . '' : Str .Substring ( 0 , strlen - 2 )
    in inline evaluation of: ``import java.lang.*;  import com.itko.Lisa.Outputlogmessage.rsp;  import org.apac . . . '' at line number 9
    | Trapped Message:   javax.script.ScriptException: bsh.EvalError: Sourced file: inline evaluation of: ``import java.lang.*;  import com.itko.Lisa.Outputlogmessage.rsp;  import org.apac . . . '' : Error in method invocation: Method Substring(int, int) not found in class'java.lang.String' : at Line: 9 : in file: inline evaluation of: ``import java.lang.*;  import com.itko.Lisa.Outputlogmessage.rsp;  import org.apac . . . '' : Str .Substring ( 0 , strlen - 2 )
    in inline evaluation of: ``import java.lang.*;  import com.itko.Lisa.Outputlogmessage.rsp;  import org.apac . . . '' at line number 9
    ----------------------------------------------------------------------------
    STACK TRACE
    javax.script.ScriptException: bsh.EvalError: Sourced file: inline evaluation of: ``import java.lang.*;  import com.itko.Lisa.Outputlogmessage.rsp;  import org.apac . . . '' : Error in method invocation: Method Substring(int, int) not found in class'java.lang.String' : at Line: 9 : in file: inline evaluation of: ``import java.lang.*;  import com.itko.Lisa.Outputlogmessage.rsp;  import org.apac . . . '' : Str .Substring ( 0 , strlen - 2 )
    in inline evaluation of: ``import java.lang.*;  import com.itko.Lisa.Outputlogmessage.rsp;  import org.apac . . . '' at line number 9
        at bsh.BshScriptEngine.evalSourceac(BshScriptEngine.java:102)
        at bsh.BshScriptEngine.eval(BshScriptEngine.java:61)
        at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:264)
        at com.itko.lisa.test.ScriptExecHandler.executeScript(ScriptExecHandler.java:674)
        at com.itko.lisa.test.ScriptExecHandler.executeScript(ScriptExecHandler.java:427)
        at com.itko.lisa.test.UserScriptNode._execute(UserScriptNode.java:210)
        at com.itko.lisa.editor.UserScriptNodeEditor.execute(UserScriptNodeEditor.java:214)
        at com.itko.lisa.editor.UserScriptNodeEditor.access$000(UserScriptNodeEditor.java:49)
        at com.itko.lisa.editor.UserScriptNodeEditor$1.actionPerformed(UserScriptNodeEditor.java:73)
        at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
        at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
        at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
        at java.awt.Component.processMouseEvent(Component.java:6539)
        at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
        at java.awt.Component.processEvent(Component.java:6304)
        at java.awt.Container.processEvent(Container.java:2239)
        at java.awt.Component.dispatchEventImpl(Component.java:4889)
        at java.awt.Container.dispatchEventImpl(Container.java:2297)
        at java.awt.Component.dispatchEvent(Component.java:4711)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
        at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
        at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
        at java.awt.Container.dispatchEventImpl(Container.java:2283)
        at java.awt.Window.dispatchEventImpl(Window.java:2746)
        at java.awt.Component.dispatchEvent(Component.java:4711)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
        at java.awt.EventQueue.access$500(EventQueue.java:97)
        at java.awt.EventQueue$3.run(EventQueue.java:709)
        at java.awt.EventQueue$3.run(EventQueue.java:703)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
        at java.awt.EventQueue$4.run(EventQueue.java:733)
        at java.awt.EventQueue$4.run(EventQueue.java:731)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
    ============================================================================



  • 2.  RE: How to find a length of string in beanshell...?
    Best Answer

    Broadcom Employee
    Posted Sep 22, 2022 04:37 AM
    Hi Sravan

    Please check the syntax. It should be Str.substring and not Str.Substring
    (case sensitivity).

    --
    regards
    Sankar Natarajan

    --
    This electronic communication and the information and any files transmitted
    with it, or attached to it, are confidential and are intended solely for
    the use of the individual or entity to whom it is addressed and may contain
    information that is confidential, legally privileged, protected by privacy
    laws, or otherwise restricted from disclosure to anyone else. If you are
    not the intended recipient or the person responsible for delivering the
    e-mail to the intended recipient, you are hereby notified that any use,
    copying, distributing, dissemination, forwarding, printing, or copying of
    this e-mail is strictly prohibited. If you received this e-mail in error,
    please return the e-mail to the sender, delete it from your computer, and
    destroy any printed copy of it.




  • 3.  RE: How to find a length of string in beanshell...?

    Posted Sep 22, 2022 06:27 AM
    Thanks a lot, Its working fine.


  • 4.  RE: How to find a length of string in beanshell...?

    Posted Sep 29, 2022 12:11 PM
    Nice Catch Sankar :)

    ------------------------------
    Regards,
    Vaibhav Jain
    Capgemini
    ------------------------------