Azure Function in Java does not work locally

Viewed 10119

I am trying to run Azure function locally on my mac with IntelliJ. I did everything, step by step in this tutorial: https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-maven-intellij but when I start the project console show me error that

`Azure Functions Core Tools (3.0.2912 Commit hash: bfcbbe48ed6fdacdf9b309261ecc8093df3b83f2)
Function Runtime Version: 3.0.14287.0
Failed to start Worker Channel. Process fileName: %JAVA_HOME%/bin/java
System.Diagnostics.Process: No such file or directory.
Failed to start language worker process for runtime: (null). workerId:9c6a8dfb-6dcc-4e3c-82a6-0ce0c6949991
No job functions found. Try making your job classes and methods public. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).
For detailed output, run func with --verbose flag.
Hosting environment: Production
Content root path: /Users/mariojaros/Library/Mobile Documents/com~apple~CloudDocs/azure-function-examples
Now listening on: http://0.0.0.0:7071
Application started. Press Ctrl+C to shut down.
Host lock lease acquired by instance ID '00000000000000000000000086CB0170'.
^CApplication is shutting down...

` I think that problem is with language worker.

JAVA_HOME:

which java
/usr/bin/java

I was trying func start too and it was same result and finally I tried it with Visual studio code and the same error.

Only way how I can run Azure Function locally in java is when I generate project with archetype mvn archetype:generate -DarchetypeGroupId=com.microsoft.azure -DarchetypeArtifactId=azure-functions-archetype -DjavaVersion=8 and then mvn package mvn azurefunctions:run

 [INFO] Scanning for projects...
[INFO] 
[INFO] ----------------------< sk.devy:rendering-skuska >----------------------
[INFO] Building Azure Java Functions 1.0.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- azure-functions-maven-plugin:1.4.0:run (default-cli) @ rendering-skuska ---
[WARNING] Azure Functions only support JDK 8, which is lower than local JDK version 14.0.1
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.microsoft.applicationinsights.core.dependencies.xstream.core.util.Fields (file:/Users/mariojaros/.m2/repository/com/microsoft/azure/applicationinsights-core/2.5.1-BETA/applicationinsights-core-2.5.1-BETA.jar) to field java.util.TreeMap.comparator
WARNING: Please consider reporting this to the maintainers of com.microsoft.applicationinsights.core.dependencies.xstream.core.util.Fields
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
[INFO] Azure Function App's staging directory found at: /Users/mariojaros/rendering-skuska/target/azure-functions/rendering-skuska-1600871852849
[INFO] Azure Functions Core Tools found.
Azure Functions Core Tools (3.0.2912 Commit hash: bfcbbe48ed6fdacdf9b309261ecc8093df3b83f2)
Function Runtime Version: 3.0.14287.0
Worker process started and initialized.
OpenJDK 64-Bit Server VM warning: Options -Xverify:none and -noverify were deprecated in JDK 13 and will likely be removed in a future release.

Functions:

HttpTrigger-Java: [GET,POST] http://localhost:7071/api/HttpTrigger-Java

For detailed output, run func with --verbose flag.
Hosting environment: Production
Content root path: /Users/mariojaros/rendering-skuska/target/azure-functions/rendering-skuska-1600871852849
Now listening on: http://0.0.0.0:7071
Application started. Press Ctrl+C to shut down.
Host lock lease acquired by instance ID '000000

Has anybody relative problem ? Is func start working for java functions ?

9 Answers

Path to Java for language worker java is set in configuration file worker.config.json in the installation directory azure-function-core-tools@3/%version/workers/java

{
    "description": {
        "language": "java",
        "extensions": [".jar"],
        "defaultExecutablePath": "/usr/bin/java",
        "defaultWorkerPath": "azure-functions-java-worker.jar",
        "arguments": ["-XX:+TieredCompilation -XX:TieredStopAtLevel=1 -noverify -Djava.net.preferIPv4Stack=true -jar", "%JAVA_OPTS%"]
    }
}

You need to change defaultExecutablePath from "%JAVA_HOME%/bin/java to the absolute path of your java executable (e.g. /usr/bin/java).

You can find the installation directory with readlink -f $(which func)

I started receiving this error after installing the latest versions of IDEA IntelliJ w/ Azure Toolkit for IntelliJ plugin, & Azure Core Tools v3 with and existing project on a new laptop.

The first error in the console when running the Azure Functions locally was:
Failed to start Worker Channel. Process fileName: %JAVA_HOME%/bin/java

So, for anyone getting this error using IntelliJ with Azure Functions, the JAVA_HOME can be set in the Run/Debug configuration for the Run Functions configuration.

Just add it to the App Settings, pointing to the correct location for Java root folder.

For example, on Windows if you've installed the Azure Supported SDK v11 (LTS version) (aka Zulu v11 for Azure) then it is installed into the Home path C:\Program Files\Zulu\zulu-11 whereby java.exe is located in the fully qualified path C:\Program Files\Zulu\zulu-11\bin\java.exe

So you would add the Key Value Pair: Key= "JAVA_HOME" Value = "C:\Program Files\Zulu\zulu-11"

As shown here: IntelliJ IDEA Run Configuration for JAVA_HOME variable

i had this issue and found the solution provided by zeretbird almost worked for me. but i had to put JAVA_HOME and the path in my local.setting.json like so.

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "java",
    "StoreServiceendpoint": "blabla",
    "StoreServiceKey": "blabla",
    "JAVA_HOME": "/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home"
  }
}

In macOS, the variable name is $JAVA_HOME, but azure function still find the window format of %JAVA_HOME%. So it cannot find any java executive file in path.

So add JAVA_HOME in application run config setting.

If your are using core-tool@3 on a Mac you can also put the JAVA_HOME as App Setting in the intellij run configuration. https://i.stack.imgur.com/ED3YQ.png

Also: Using the which func does not work, you have to use the path of the actual binary located next to the wrapper that is found by which. It is located here (if you installed using npm): /usr/local/lib/node_modules/azure-functions-core-tools/bin/func

You can add JAVA_HOME in local.settings.json in the VS Code.

Here is an example. OS: WSL Ubuntu-20.04 Code editor: VS Code

{
  {
     "IsEncrypted": false,
     "Values": {
       "FUNCTIONS_WORKER_RUNTIME": "java",
       "JAVA_HOME": "/usr"
  }
}

Solution for macOS

  1. Confirm the jvms installed, execute this in a terminal

/usr/libexec/java_home -V

  1. Go to Run, then Edit Configurations. Edit the Run Function in Azure Functions. In App Settings add JAVA_HOME as key and the path to the JVM obtained in 1)

I am using windows 10 professional and using azure function using IntelliJ IDEA 2020.2.3 (Community Edition) Build #IC-202.7660.26, built on October 6, 2020. I ran into the same issue .

Apparently it is looking for environment variable JAVA_HOME. I have added the in windows environment variables EnvironmentVariable. After setting this up I was able to run the java function locally. working

Generally %JAVA_HOME% notation is used for windows operating system. It might be a bug or try setting JAVA_HOME at profile level and make sure it points to right JDK .

This was just a fluke , somehow it worked . Hope it works for you too .

Related