URCap ProgramNode ScriptWriter Question

I can run getXmlRpc().home() command in openView() function in ProgramNode section.

I cannot run writer.appendLine(“getXmlRpc().home()”) in generateScript() function.

I have attached the image about the error.

How can we solve this situation?

getXmlRpc() is not a method in URScript. That’s a Java method, so it has no idea what to do with it when you run it in generateScript(). If you need to call Java methods from URScript, you have to go through a lot of steps. The Script manual is here: https://s3-eu-west-1.amazonaws.com/ur-support-site/234367/script_directory_PolyScope5.pdf
You will need to use the rpc_factory() function to create the URScript factory, and feed it the same port number as the server you’re running in java. Then you need to define the methods you want to call in Java and call them from URScript.

Bear with me here, I’m going to give you some barebones structure to hopefully get you on the right path.

//Setup the RPC server in Java
try {
				ScriptToJava myServer = new ScriptToJava(40406);
				myServer.start();
				System.out.println("XML_RPC server has started on port: 40406");
			} catch (Exception e) {
				System.out.println("XML_RPC server FAILED TO START on port: 40406");
				e.printStackTrace();
			}
//The ScriptToJava() class
import java.io.IOException;

import org.apache.xmlrpc.server.PropertyHandlerMapping;
import org.apache.xmlrpc.server.XmlRpcServer;
import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;
import org.apache.xmlrpc.webserver.WebServer;

 //Sets up the XMLRPC server running in Java to handle calls made from URScript and map them
 //to the apprpriate Java functions. These can be seen in the "JavaScriptHandler" class.
 

public class ScriptToJava extends XmlRpcServer{
	private WebServer webServer;
	
	public ScriptToJava(int port) throws Exception {		
		webServer = new WebServer(port);
        XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer();

        PropertyHandlerMapping phm = new PropertyHandlerMapping();
        phm.addHandler("yourMethodName", JavaScriptHandler.class);
        phm.addHandler("yourOtherMethodName", JavaScriptHandler.class);
        xmlRpcServer.setHandlerMapping(phm);

        XmlRpcServerConfigImpl config = (XmlRpcServerConfigImpl) xmlRpcServer.getConfig();
        config.setEnabledForExtensions(true);
        config.setContentLengthOptional(false);
	}
	
	public void start() throws IOException {
        webServer.start();
    }

    public void stop() {
        webServer.shutdown();
    }
}
//The JavaScriptHandler class
public class JavaScriptHandler {
	
	public int yourJavaFunction(int someInput) throws ScriptToJavaException{
		int someOutput = someInput + 1;
        System.out.println("Added 1 to the input from URScript" + someOutput);
		return someOutput ;
    }
	
	public int yourOtherJavaFunction(int input) throws ScriptToJavaException{
		//Do something
		return 0;  //You need to return something even if you don't do anything with it. Void functions don't work
	}
}
//The ScriptToJavaException class
public class ScriptToJavaException extends Exception{
	private static final long serialVersionUID = 0L;

    public ScriptToJavaException(String message) {
        super(message);

    }
}
// This is what you will need to have your scriptwriter write
myServer = rpc_factory("xmlrpc", "http://127.0.0.1:40406/RPC2") //<-- See that the port number (40406) matches what we had in the Java class
myServer.yourMethodName.yourJavaFunction(5) //Accessing your Java classes from URScript. Passing 5 as an arbitrary integer as an example

Pretty long-winded, but hopefully it can provide you with some guidance. Or at least get you Googling the right things.