# Using custom Java keywords/library in Robot Framework using Jython

**URL:** <https://forum.robotframework.org/t/using-custom-java-keywords-library-in-robot-framework-using-jython/1955>\
**Category:** Extensions\
**Created:** [17 June 2021 12:51 UTC](https://forum.robotframework.org/t/using-custom-java-keywords-library-in-robot-framework-using-jython/1955 "2021-06-17T12:51:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![GijsCIM](https://avatars.discourse-cdn.com/v4/letter/g/ad7895/32.png) [@GijsCIM](https://forum.robotframework.org/u/GijsCIM)\
**Post date:** [17 June 2021 12:51 UTC](https://forum.robotframework.org/t/using-custom-java-keywords-library-in-robot-framework-using-jython/1955/1 "2021-06-17T12:51:56Z")

</div>

Im trying to import my own Java library into RFW to use the keywords. Importing Selenium works fine. I have exported the java class to a .jar.  
Things I have tried:

1. 

With the .jar and .robot file in the same folder:

```python
λ jython -m robot testcase1.robot

```

1. 

Pasting the MyTestLib.jar in C:\Program Files\jython2.7.2\Lib\site-packages and running:

```python
λ jython -m robot testcase1.robot

```

1. 

```python
jython -Dpython.path=C:\Users\Gijs\eclipse-workspace\testrobot\src\test\robotframework\acceptance\ -m robot testcase1.robot

```

1. Setting my CLASSPATH env variable to C:\Users\Gijs\eclipse-workspace\testrobot\src\test\robotframework\acceptance  
and running

```python
λ jython -m robot testcase1.robot

```

Every time i get the same error:

```python
[ERROR] Error in file 'C:\Users\Gijs\eclipse-workspace\testrobot\src\test\robotframework\acceptance\testcase1.robot' on line 5: Importing library 'MyTestLib.jar' failed: NoClassDefFoundError: com/cimsolutions/testautomation/testrobot/MyTestLib (wrong name: MyTestLib)

```

What am I doing wrong?

Here is my java class:

```python

public class MyTestLib {

	
    public static final String ROBOT_LIBRARY_SCOPE = "GLOBAL";

    
    public void hello(String name){
        System.out.println("Hello " + name + ".\n");
    }

    public void Nothing(){
    }

}

```

Here is my testsuite:

```python
***Settings***
Documentation This is a test file to test RFW
...    
Library SeleniumLibrary
Library MyTestLib.jar

***Test Cases***
Testcase0
    Log This is testcase 1.robot Starting logging
    Open Browser https://google.com chrome
    Set Browser Implicit Wait 1
    Click Button //button[.//text() = 'Ik ga akkoord']
    Sleep 1
    Input Text name=q Cimsolutions
    Press Keys name=q \ue00c
    Press Keys name=q \ue007
    Sleep 1
    Close Browser
    Log Test completed

MyLibTest
    Log My lib test is starting
    Nothing

```

I think I installed all requirements correctly:

```python
C:\                                                                                                                  
λ jython --version                                                                                                   
Jython 2.7.2                                                                                                         
                                                                                                                     
C:\                                                                                                                  
λ java --version                                                                                                     
java 11.0.10 2021-01-19 LTS                                                                                          
Java(TM) SE Runtime Environment 18.9 (build 11.0.10+8-LTS-162)                                                       
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.10+8-LTS-162, mixed mode)                                         
                                                                                                                     
C:\                                                                                                                  
λ jython -m robot --version                                                                                          
WARNING: An illegal reflective access operation has occurred                                                         
WARNING: Illegal reflective access by org.python.core.io.StreamIO (file:/C:/Program%20Files/jython2.7.2/jython.jar) t
o field java.io.FilterOutputStream.out                                                                               
WARNING: Please consider reporting this to the maintainers of org.python.core.io.StreamIO                            
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations                
WARNING: All illegal access operations will be denied in a future release                                            
Robot Framework 4.0.3 (Jython 2.7.2 on java11.0.10)                                                                  
                                                                                                                     
C:\                                                                                                                  
λ jython -m pip list                                                                                                 
WARNING: An illegal reflective access operation has occurred                                                         
WARNING: Illegal reflective access by org.python.core.io.StreamIO (file:/C:/Program%20Files/jython2.7.2/jython.jar) t
o field java.io.FilterOutputStream.out                                                                               
WARNING: Please consider reporting this to the maintainers of org.python.core.io.StreamIO                            
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations                
WARNING: All illegal access operations will be denied in a future release                                            
DEPRECATION: A future version of pip will drop support for Python 2.7.                                               
Package Version                                                                               
------------------------------ -------                                                                               
pip 19.1                                                                                  
robotframework 4.0.3                                                                                 
robotframework-seleniumlibrary 4.2.0                                                                                 
selenium 3.141.0                                                                               
setuptools 41.0.1                                                                                
urllib3 1.26.5                                                                                

```

---

<div class="post-metadata">

**Author:** ![HelioGuilherme66](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/helioguilherme66/32/34_2.png) [@HelioGuilherme66](https://forum.robotframework.org/u/HelioGuilherme66)\
**Post date:** [18 June 2021 11:05 UTC](https://forum.robotframework.org/t/using-custom-java-keywords-library-in-robot-framework-using-jython/1955/2 "2021-06-18T11:05:59Z")

</div>

Well, I am surprised to see using Java 11 with Jython 2.7.2, but if it works…

I would experiment, to remove the .jar from the library import, and make sure the MyTestLib.jar full pass is in CLASSPATH.

Then if that does not work, I would experiment, without using .jar, but just .java and .class files.

Can’t say more, except for you to double check the User Guide.

Good luck.

---

<div class="post-metadata">

**Author:** ![GijsCIM](https://avatars.discourse-cdn.com/v4/letter/g/ad7895/32.png) [@GijsCIM](https://forum.robotframework.org/u/GijsCIM)\
**Post date:** [22 June 2021 12:16 UTC](https://forum.robotframework.org/t/using-custom-java-keywords-library-in-robot-framework-using-jython/1955/3 "2021-06-22T12:16:53Z")

</div>

I tried downgrading to Java 8, but that was not the problem. Also the Classpath was not the problem as far as i can tell. But when I used the .class file it worked. Tried that before aswell, guess I made a little error somewhere.

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![Logan455](https://avatars.discourse-cdn.com/v4/letter/l/3da27b/32.png) [@Logan455](https://forum.robotframework.org/u/Logan455)\
**Post date:** [17 September 2021 13:18 UTC](https://forum.robotframework.org/t/using-custom-java-keywords-library-in-robot-framework-using-jython/1955/4 "2021-09-17T13:18:07Z")

</div>

Try adding the full path to your “MyTestLib.jar” in the `CLASSPATH` envvar, like:

```python
export CLASSPATH=\path\to\MyTestLib.jar

```

use Windows equivalent, of course.
