# Connecting java with robot framework

**URL:** <https://forum.robotframework.org/t/connecting-java-with-robot-framework/5253>\
**Category:** Robot Framework\
**Created:** [3 February 2023 07:01 UTC](https://forum.robotframework.org/t/connecting-java-with-robot-framework/5253 "2023-02-03T07:01:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shambhavi](https://avatars.discourse-cdn.com/v4/letter/s/8e8cbc/32.png) [@shambhavi](https://forum.robotframework.org/u/shambhavi)\
**Post date:** [3 February 2023 07:01 UTC](https://forum.robotframework.org/t/connecting-java-with-robot-framework/5253/1 "2023-02-03T07:01:20Z")

</div>

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 ???

---

<div class="post-metadata">

**Author:** ![robinmackaij](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/robinmackaij/32/126_2.png) [@robinmackaij](https://forum.robotframework.org/u/robinmackaij)\
**Post date:** [9 February 2023 20:04 UTC](https://forum.robotframework.org/t/connecting-java-with-robot-framework/5253/2 "2023-02-09T20:04:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Many](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/many/32/658_2.png) [@Many](https://forum.robotframework.org/u/Many)\
**Post date:** [10 February 2023 11:05 UTC](https://forum.robotframework.org/t/connecting-java-with-robot-framework/5253/3 "2023-02-10T11:05:48Z")

</div>

If you want to use Java (e.g. to develop a Keyword Library) I recommend to look into [JPype](https://jpype.readthedocs.io/en/latest/).

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}

```
