1

I have a user defined java class with a static method, in a classpath directory under src/test/java

package com.latch.configPackage;

public class base64Tohex {
    public static void converter(String[] args) {
        String guid = "YxRfXk827kPgkmMUX15PNg==";
        String hexString = Hex.encodeHexString(guid.getBytes());
        System.out.println(hexString);
        return hexString;
    }
}

I am calling the java class and method within my karate feature file as below

  * def doWork =
        """
        function(val) {
            var configPackage = Java.type('com.mycompany.configPackage.base64Tohex');
            var gc = new configPackage();
            return gc.converter();
        }
        """

where com.mycompany is also the group id in. my pom file. When I run the karate feature file on my Visual Studio, I get an error as below

<<<<
org.graalvm.polyglot.PolyglotException: TypeError: Access to host class com.mycompany.genevaconfig.base64Tohex is not allowed or does not exist.
- <js>.:anonymous(Unnamed:2)

How do I fix this?

mnc
  • 13
  • 1
  • 4
  • The answer is because the Java class is not on the classpath. Maybe you are using the "fat jar" to run Karate. – Peter Thomas Aug 10 '22 at 04:39
  • Thanks for the quick response @PeterThomas and for such an amazing open source tool, we really enjoy using karate for our team. I am not using fat jar but using the karate with maven set up , (karate version 1.1.0). I am new to Java and was wondering if you could send any links that would help the set up. Do i need to compile my custom java file to generate a jar first and then add it to VS code classpath or do i need to run the `mvn test` command for my feature which will auto generate the jar? i am still unclear on the exact steps. Appreciate any pointers in this direction. Thanks. – mnc Aug 10 '22 at 12:57
  • great :) first - maybe you messed up the class-name: `com.latch.configPackage` ? okay, if you are not using the fat-jar, if your java maven (or gradle) project is properly set up, it should "just work". since you are new to java, here's what I suggest, use the quick-start. this will give you a maven project that is "well formed" and you can try to create a new project and get a simple example to work: https://github.com/karatelabs/karate#quickstart, then you should be able to figure it out. maybe getting the help of a friend who knows java may be faster – Peter Thomas Aug 10 '22 at 13:22
  • 1
    thanks so much @PeterThomas. I was able to work it out after reaching out to a java friend. The custom code works through a mvn command correctly. It does not work via VS Code's Karate Runner but that must be the classpath set up for IDE I will need to figure out. Thanks so much for all the pointers and help. – mnc Aug 11 '22 at 12:12

0 Answers0