1

I had a previous issue on the same topic described in this thread. I found a solution using maven-assembly-plugin however I encountered the below issue.

Sample code.

1)the following classes

      i) import clojure.java.api.Clojure; ,
     ii) import clojure.lang.IFn;

2)compile your clojure into an uberjar then import it into the java code.

I have opted for the 2nd option as it's more straight forward.

Here is the clojure code

(ns com.test.app.service
 (:gen-class
       :name com.test.app.service
       :main false
       :methods [^{:static true} [returned [int] int]]))

    (defn returned
      [number]
      (* 2 number))

    (defn -returned
      [number]
      (returned number))

Here is the Java code.

package com.s.profile;

import java.util.*;
import com.microsoft.azure.serverless.functions.annotation.*;
import com.microsoft.azure.serverless.functions.*;
import com.test.app.service;


/**
 * Azure Functions with HTTP Trigger.
 */
public class Function {
    /**
     * This function listens at endpoint "/api/hello". Two ways to invoke it using "curl" command in bash:
     * 1. curl -d "HTTP Body" {your host}/api/hello
     * 2. curl {your host}/api/hello?name=HTTP%20Query
     */
    @FunctionName("hello")
    public HttpResponseMessage<String> hello(
            @HttpTrigger(name = "req", methods = {"get", "post"}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request,
            final ExecutionContext context) {
        context.getLogger().info("Java HTTP trigger processed a request.");

        // Parse query parameter
        String query = request.getQueryParameters().get("name");
        String name = request.getBody().orElse(query);

        if (name == null) {
            return request.createResponse(400, "Please pass a name on the query string or in the request body");
        } else {
            service.returned(4);
            context.getLogger().info("process data" );
            return request.createResponse(200, "Hellos, " + name );
        }
    }
}

And here is the full stacktrace

    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]
          java.lang.ExceptionInInitializerError
    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]
            at clojure.lang.Namespace.<init>(Namespace.java:34)
    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]
            at clojure.lang.Namespace.findOrCreate(Namespace.java:176)
    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]
            at clojure.lang.Var.internPrivate(Var.java:151)
    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]
            at com.test.app.service.<clinit>(Unknown Source)
    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]
            at com.s.profile.Function.hello(Function.java:42)
    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]
            at java.lang.reflect.Method.invoke(Method.java:483)
    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]
            at com.microsoft.azure.webjobs.script.broker.JavaMethodInvokeInfo.invoke(JavaMethodInvokeInfo.java:22)
    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]
            at com.microsoft.azure.webjobs.script.broker.JavaMethodExecutor.execute(JavaMethodExecutor.java:59)
    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]
            at com.microsoft.azure.webjobs.script.broker.JavaFunctionBroker.invokeMethod(JavaFunctionBroker.java:40)
    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]
            at com.microsoft.azure.webjobs.script.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:33)
    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]
            at com.microsoft.azure.webjobs.script.handler.InvocationRequestHandler.execute(InvocationRequestHandler.java:10)
    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]
            at com.microsoft.azure.webjobs.script.handler.MessageHandler.handle(MessageHandler.java:43)
    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]
            at com.microsoft.azure.webjobs.script.JavaWorkerClient$StreamingMessagePeer.lambda$onNext$0(JavaWorkerClient.java:84)
    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]
            at com.microsoft.azure.webjobs.script.JavaWorkerClient$StreamingMessagePeer$$Lambda$10/293453555.run(Unknown Source)
    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]
            at java.util.concurrent.ForkJoinTask$AdaptedRunnableAction.exec(ForkJoinTask.java:1407)
    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]
            at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]
            at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:902)
    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]
            at java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1689)
    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]
            at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1644)
    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]
            at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]
          Caused by: java.io.FileNotFoundException: Could not locate clojure/core__init.class or clojure/core.clj on classpath:
    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]
            at clojure.lang.RT.load(RT.java:443)
    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]
            at clojure.lang.RT.load(RT.java:411)
    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]
            at clojure.lang.RT.doInit(RT.java:447)
    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]
            at clojure.lang.RT.<clinit>(RT.java:329)
    info: Worker.Java.917bfefd-b67a-49b2-b77b-6848c52c3a98[0]

The information I have on that issue is below as per the documentation.

Signals that an unexpected exception has occurred in a static initializer. An ExceptionInInitializerError is thrown to indicate that an exception occurred during evaluation of a static initializer or the initializer for a static variable.

Which static method had a problem the one in my clojure code? Seems unlikely because when I run the code from a more simple "hello world" Java example from the main function it returns fine, which leads me to believe my compiled clojure jar file is not the problem. So where is it stemming from? Are there rules behind how the azure functions work internally that limit what can get called?

sqwale
  • 554
  • 3
  • 24
  • 1
    It'd be helpful to provide a code sample which repros the issue as it's hard to guess from the stack trace/error (which is also an issue for us to improve, better error messages). If you could provide a full repro on GitHub, that might be best to track because it's possibly a bug (and the error message definitely needs improving) https://github.com/Azure/azure-functions-java-worker/issues – Chris Anderson Feb 15 '18 at 22:32
  • 1
    Or at least a full stacktrace. This neutered stacktrace is especially unhelpful. – amalloy Feb 16 '18 at 00:41
  • I have added the context from the previous thread which had a different issue. – sqwale Feb 16 '18 at 11:30
  • Any update @sqwale ? If you resolved this I'd be curious to hear your resolution. – fmjrey May 04 '18 at 15:01
  • There is some commentary here: https://github.com/Azure/azure-functions-java-worker/issues/55 – Derek Slager Jul 26 '18 at 17:50
  • I experimented a bit with approach 1 and it works. Here is a sample project: https://github.com/Hindol/clj-fn – Hindol Apr 17 '20 at 06:36

0 Answers0