How can we use java file directly in the robot file as a library without manually compiling it to .class file ,as we can use python file as a library ???
With the end of life of Python 2 and thus Jython, that is no longer possible. I’m not entirely sure on the status of the alternatives, but I think you can still use Java libraries using the remote interface.
1 Like
If you want to use Java (e.g. to develop a Keyword Library) I recommend to look into JPype.
However, if I understand correctly you would still need to compile and package that file in a .jar.
An example. It’s just a calculator in java and the methods are called from Robot
JavaWrapper.py
# Boiler plate stuff to start the module
import jpype
import jpype.imports
from jpype.types import *
from inspect import getmembers, isfunction
# Launch the JVM
jpype.startJVM(classpath=['library/exercise/solution/JavaLibrary.jar'])
from org.calculator import JavaLibrary
class JavaWrapper:
def get_keyword_names(self):
for name, func in getmembers(JavaLibrary, isfunction):
if name.startswith('_'):
continue
yield name
def run_keyword(self, name, args, kwargs):
print("Running keyword '%s' with positional arguments %s and named arguments %s."
% (name, args, kwargs))
func = getattr(JavaLibrary, name)
return func(*args, **kwargs)
JavaLibrary.java
package org.calculator;
/*
* A class which provides functions for all basic arithmetic operations
* add, subtract, multiply, divide, and remainder.
*/
public class JavaLibrary {
public static int add(int a, int b) {
return a + b;
}
public static int subtract(int a, int b) {
return a - b;
}
public static int multiply(int a, int b) {
return a * b;
}
public static int divide(int a, int b) {
return a / b;
}
public static int remainder(int a, int b) {
return a % b;
}
}
test.robot
*** Settings ***
Library JavaWrapper.py
*** Test Cases ***
Use Dynamic Library
${result} Add ${1} ${2}
Log ${result}
${result} Subtract ${3} ${1}
Log ${result}
1 Like