18 January 2026:


This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.



Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.

How do I call Java Code from scripts / Broken links

hari19hari19 Members
edited November -1 in Sahi - Open Source
How do I call Java Code from Sahi Scripts ?
I need to read a properties file and it looks much easier to do it in Java and then call the same from sahi scripts.

I saw a link for this page ( ( http://sahi.co.in/w/invoking-java-in-rhino) but this link is broken.

thanks,
Hari
Tagged:

Comments

  • narayannarayan Administrators
    Use http://sahi.co.in/w/sahi-scripting-calling-java
    Where was the broken link present?
  • hari19hari19 Members
    Thanks!

    Can only static java methods be called ?

    Will what is given in this page (http://www.mozilla.org/rhino/ScriptingJava.html ) on Rhino / Java work directly in Sahi ?

    Or do I need to wrap usages of Java like in the above page in <browser> </browser> tags in the script ?
    If so and I write some custom Java code where would I need to place them ?
    Is it in the browser's JRE or in Sahi Proxy's classpath ?

    I don't remember the page where the broken link was present right now.
    I'll update this post with the link soon.

    thanks,
    Hari
  • narayannarayan Administrators
    Rhino is on the proxy side. You should not enclose that code in browser tags. You can use the new operator to instantiate java objects and then invoke methods on them. Btw, a properties file can be read like this
    var props = new java.util.Properties();
    props.load(new java.io.FileInputStream(FILE_PATH));
    var $val = props.getProperty("my.property.key");
    _alert($val);
    

    Regards,
    Narayan
  • hari19hari19 Members
    Thanks!
    I think adding more examples like these to the Sahi Documentation will help adoption!
  • hari19hari19 Members
    If I write some custom class where to I need to add it so that it is available in my scripts ?
    Should it be added to the Sahi Proxy classpath or is it the browser's JRE ?
  • narayannarayan Administrators
    Agree. Examples section is coming up soon. Thanks.

    You should add it to the Sahi Proxy classpath. Note that, for custom classes you will need to use "Packages." prefix.
    eg.

    var obj = new Packages.com.company.path.MyClass()

    Regards,
    Narayan
  • hari19hari19 Members
    thanks!

    I couldn't find where I saw the broken link for http://sahi.co.in/w/invoking-java-in-rhino but I saw another broken link on this page :
    http://sahi.co.in/w/_callserver ( Link to http://sahi.co.in/w/w/adding-jars-to-sahis-classpath is broken )


    I'll update this thread when I find it.
  • hari19hari19 Members
    Should Java Objects be created/used like
    var props = new java.util.Properties();
    props.load(new java.io.FileInputStream("/path to my file"));
    var $val = props.getProperty("my.property.key");
    _alert($val);
    

    or
    var $props = new java.util.Properties();
    $props.load(new java.io.FileInputStream("/path to my file"));
    var $val = $props.getProperty("my.property.key");
    _alert($val);
    

    I would like to read my Properties File Once and store it to an object then get properties from the object.

    Right now I'm having code like
    var $testsuite_properties;
    function readPropertiesFile()
    {
        var $props = new java.util.Properties();
        $props.load(new java.io.FileInputStream("/path to my props file"));
        return $props
    }
    
    function getProperty($prop_name)
    {
        var $val = $testsuite_properties.getProperty($prop_name);
        return $val;
    }
    
    readPropertiesFile();
    

    in a file which I include in my scripts and then in my scripts I use
    getProperty("my.propery.key");

    This does't work. What am I doing wrong ?
  • narayannarayan Administrators
    Regarding versions 1 and 2, either would work. Use $ variables when you pass it to Sahi Action APIs. Otherwise you are free to ignore the $ prefix. As a matter of consistency I recommend use of $ through out so that people don't forget.

    In the second part of your question, you have not assigned

    var $testsuite_properties = readPropertiesFile();

    before

    getProperty("my.propery.key");
  • hari19hari19 Members
    I did actually assign the variable but there seems to have been some copy-paste mistake.
    but now got it working!

    Thanks for the clarification..
  • fionfion Members
    Hi Narayan,

    I don't understand about "Add it to the Sahi Proxy classpath", and the link under "Add it to Sahi’s classpath:" in http://sahi.co.in/w/_callserver is removed, can you kindly to reply me?

    Thanks in advance,
    Fion
    narayan wrote:
    Agree. Examples section is coming up soon. Thanks.

    You should add it to the Sahi Proxy classpath. Note that, for custom classes you will need to use "Packages." prefix.
    eg.

    var obj = new Packages.com.company.path.MyClass()

    Regards,
    Narayan
Sign In or Register to comment.