Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when executing java function with maven dependency of Gson #153

Open
dumplingsMing opened this issue Apr 5, 2023 · 1 comment
Open

Comments

@dumplingsMing
Copy link

I have successfully deployed the newest commit in the deploy branch of Knix on my local machine with ansible.

In my deployment, I tried several simple Java functions and found them works fine. However, when I try to execute some Java functions with maven dependency as described in the java tutorials, I found things not that simple.

This is my Java Function's source code:

import org.microfunctions.mfnapi.MicroFunctionsAPI;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;


public class Test
{
	public Object handle(Object event, MicroFunctionsAPI context)
	{
	    String jsonString = (String)event;
            JsonParser parser = new JsonParser();
            JsonObject jsonObject = parser.parse(jsonString).getAsJsonObject();
            return jsonObject.toString();
	}
}

And this is the associated pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>io.knix</groupId>
  <artifactId>example_maven</artifactId>
  <version>1.0.0</version>

  <name>JavaRequestHandlerMavenTest</name>
  <url>https://knix.io/</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
      <dependency>
          <groupId>com.google.code.gson</groupId>
          <artifactId>gson</artifactId>
          <version>2.8.2</version>
      </dependency>
  </dependencies>

</project>

I created a function named Test with the code and pom.xml mentioned above with the web interface, and use the Test function for testing.

I successfully passed the preparing function phase, found the microfn/sandbox_java container running.

However, when I tried execute the function, I got the following exception in the execution log:

[2023-04-05 13:28:34.675] [INFO] [0l] [Test] [FunctionWorker] Started:Test, user: xxxxxxx, workflow: 833a372ca348a098b47d2562a46e591b, sandbox: 833a372ca348a098b47d2562a46e591b, pid: 151
[2023-04-05 13:29:07.352] [ERROR] [d752c5f1d3b511edaa890242ac110003] [Test] Child exception: com/google/gson/JsonParser
Traceback (most recent call last):
  File "/opt/mfn/FunctionWorker/python/FunctionWorker.py", line 460, in _fork_and_handle_message
    function_output = self._state_utils.exec_function_catch_retry(self._function_runtime, exec_arguments, self._sapi)
  File "/opt/mfn/FunctionWorker/python/StateUtils.py", line 389, in exec_function_catch_retry
    ret_value = self._exec_function(runtime, exec_arguments, sapi)
  File "/opt/mfn/FunctionWorker/python/StateUtils.py", line 310, in _exec_function
    raise Exception(error_type)
Exception: com/google/gson/JsonParser

It seems that the java executor cannot find the classes of the gson package.

@dumplingsMing dumplingsMing changed the title Error when executing java function with maven dependency Error when executing java function with maven dependency of Gson Apr 6, 2023
@MoralCode
Copy link

MoralCode commented Apr 29, 2024

While not a solution, this may be a useful workaround:

Alternatively, you can download your own maven dependencies and extract them into the target/classes and upload your zip package.

https://github.com/knix-microfunctions/knix/wiki/Using-Java-functions#dependencies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants