azure-functions-java-worker icon indicating copy to clipboard operation
azure-functions-java-worker copied to clipboard

Java function host failed to discover main classes for spring cloud function

Open Flanker32 opened this issue 5 years ago • 2 comments

Function host can't find the main class of spring cloud functions and report error. Both in azure services and local.

Investigative information

Function core tools 2.7.1948

Repro steps

  1. Prepare a spring cloud function project, could do it by git clone https://github.com/Azure-Samples/hello-spring-function-azure.git.
  2. Build the spring cloud function project by mvn clean package
  3. Invoke the function locally by mvn azure-functions:run.
    • This issue could also repo in service side, just remove the MAIN_CLASS settings in azure function configuration

Expected behavior

The main class of the function should be automatically found. The MAIN_CLASS property should not need to be specified and users could access the function by curl http://localhost:7071/api/hello -d "{\"name\":\"Azure\"}"

Actual behavior

No response from curl, function host reports

 java.lang.IllegalStateException: Failed to discover main class. An attempt was made to discover main class as 'MAIN_CLASS' environment variable, system property as well as entry in META-INF/MANIFEST.MF (in that order).

Known workarounds

Set the MAIN_CLASS in app settings, for local run, add a local.settings.json file with MAIN_CLASS specified

Related information

Referred https://github.com/microsoft/azure-maven-plugins/issues/912

Flanker32 avatar Dec 04 '19 05:12 Flanker32

I'm now doing the same thing with our new Gradle plugin and I have the same issue again.

To fix this temporarily, I need to do the same trick, which is copying the local.settings.json file after the plugin has run, but this isn't a nice developer experience.

jdubois avatar Mar 25 '20 08:03 jdubois

More context from Julien: image

kulkarnisonia16 avatar Apr 06 '20 18:04 kulkarnisonia16

Any progress with this problem?

In my case, even adding the local.settings.json file with the MAIN_CLASS attribute correctly, when I package in .zip format the stagingDirectory and deploy it in Azure with az functionapp deployment source config-zip ... I am still encountering this problem when making a request to the function:

Exception: IllegalArgumentException: Failed to locate main class
Stack: java.lang.IllegalStateException: Failed to discover main class. An attempt was made to discover main class as 'MAIN_CLASS' environment variable, system property as well as entry in META-INF/MANIFEST.MF (in that order).
    at org.springframework.cloud.function.utils.FunctionClassUtils.getStartClass(FunctionClassUtils.java:86)
    at org.springframework.cloud.function.utils.FunctionClassUtils.getStartClass(FunctionClassUtils.java:63)
    at org.springframework.cloud.function.adapter.azure.FunctionInvoker.<init>(FunctionInvoker.java:94)

ferblaca avatar Jan 20 '23 11:01 ferblaca

Hi @ferblaca , I can not reproduce the issue on my local, can you share a repo that include the your code I can take a look then. Thanks.

kaibocai avatar Jan 20 '23 14:01 kaibocai

Hi @ferblaca , I can not reproduce the issue on my local, can you share a repo that include the your code I can take a look then. Thanks.

The problem is not reproduced locally, nor does deploying to Azure directly with the Azure Functions maven plugin.

As far as I can see, the problem is that when deploying in .zip format with the Azure CLI, the MAIN_CLASS environment variable is not automatically created in the function configuration.

However, deploying in Azure with the Azure Functions maven plugin does create it automatically:

image

All this would not affect if in the manifest of the generated .jar it had the MAIN_CLASS attribute instead of Main-Class: image

ferblaca avatar Jan 23 '23 12:01 ferblaca

Hi @ferblaca , few things we should be clear about

  1. If you want to add a appsetting when you deploy through azure function maven plugin or azure cli, it's set by you. When you deploy from azure function maven plugin, you are possibly add the appsetting in your pom file, for example: https://github.com/microsoft/azure-maven-plugins/wiki/Azure-Functions:-Configuration-Details image

When you deploy from azure cli, I am not sure if we have an option to add appsetting during the deployment, but you can use this command https://learn.microsoft.com/en-us/cli/azure/functionapp/config/appsettings?view=azure-cli-latest to config your appsetting once you have function app created.

  1. I don't think appsetting MAIN_CLASS is needed. I am referring to spring cloud function example at https://github.com/spring-cloud/spring-cloud-function/tree/main/spring-cloud-function-samples/function-sample-azure, I deployed to azure and everything works fine for me. I don't have appsetting MAIN_CLASS. image image

I am spring-cloud-function-dependencies 4.0.0-SNAPSHOT, please check your version. Thanks.

kaibocai avatar Jan 23 '23 20:01 kaibocai

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

ghost avatar Jan 29 '23 19:01 ghost

@msftbot please keep this opened

jdubois avatar Jan 30 '23 08:01 jdubois

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

ghost avatar Feb 03 '23 10:02 ghost

Seems like azure-functions-java still doesn't add MAIN_CLASS to MANIFEST.MF

sergey124 avatar Jun 28 '23 12:06 sergey124

Please reopen, the azure-functions-java still doesn't add MAIN_CLASS to MANIFEST.MF

Can you share the code? Ensure you are using the latest spring function dependency, they have already updated this logic. azure-function-java should never operate customers' MAINFEST.MF file.

kaibocai avatar Jun 28 '23 13:06 kaibocai

@kaibocai , thanks, you're right, with latest dependencies it works if specify

  • MAIN_CLASS in local.settings.json
  • MAIN_CLASS in src/main/azure/local.settings.json
  • start-class in pom.xml

Made a sample project with that

sergey124 avatar Jun 29 '23 11:06 sergey124