Hmm well I mean I made it work, but it’s not pretty nor is it particularly easy. Not as easy as I would have thought at least.
Essentially, you start an XML-RPC server in Java, and connect to it using a URScript command at runtime, passing the value of the runtime variable in. The Java server can then use that value in your methods. Bear with me here, it was a while ago that I did this, so I don’t really remember what/why I did it.
ScriptToJava class (this establishes the server that the URScript will pass information to):
package com.myCompany.myCAP;
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;
public class ScriptToJava extends XmlRpcServer{
//public class ScriptToJava {
private WebServer webServer;
public ScriptToJava(int port) throws Exception {
webServer = new WebServer(port);
XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer();
PropertyHandlerMapping phm = new PropertyHandlerMapping();
phm.addHandler("method1", JavaScriptHandler.class);
phm.addHandler("method2", JavaScriptHandler.class);
phm.addHandler("method3", 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();
}
}
I initialize this class in my activator with the following (you can use whatever port number you want, just make sure you use the same one everywhere):
//Start an XMLRPC server to be used from URScript
try {
ScriptToJava myServer = new ScriptToJava(40406);
myServer.start();
System.out.println("XML_RPC server has started on port: 40406");
} catch (Exception e) {
e.printStackTrace();
}
JavaScriptHandler class (this is what is being used in the “addHandler” method in the previous class):
package com.myCompany.myCAP;
public class JavaScriptHandler {
public int saveLastCount(int input) throws ScriptToJavaException{
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
undoRedoManager.recordChanges(() -> {
model.set(last_variable_value, input);
});
}
});
return 0
}
public int method2Function(int input) throws ScriptToJavaException{
System.out.println("Example method2 prints " + input);
return 0;
}
public int method3Function(int input) throws ScriptToJavaException{
System.out.println("Example method3 prints " + input);
return 0;
}
}
And finally the exception class being referenced above:
package com.myCompany.myCAP;
public class ScriptToJavaException extends Exception{
private static final long serialVersionUID = 0L;
public ScriptToJavaException(String message) {
super(message);
}
}
You should be able to add these 3 class files to your project, and rename a few things as necesary.
From the URScript side:
First you will need to create a variable and attach it to the server:
myServerVar= rpc_factory("xmlrpc", "http://127.0.0.1:40406/RPC2")
This allows us to do things like “myServerVar.method1.saveLastCount(current_count)” inside the URScript, and execute that method in java.
Finally, I think I added a dependency for the XMLRPC server:
<dependency>
<groupId>org.apache.xmlrpc</groupId>
<artifactId>xmlrpc-server</artifactId>
<version>3.1.3</version>
</dependency>
Which I have embedded with the following line:
<Embed-Dependency>xmlrpc-server, servlet-api, xmlrpc-common, xmlrpc-client</Embed-Dependency>
No idea if that’s necessary, I can only guess that something wasn’t working, so I added that at some point.
And that should be everything (I think). I probably forgot something, but I’m sure back in the day I had just been Googling something like “xmlrpc java” so you could always try that too.