azure-functions-java-worker
azure-functions-java-worker copied to clipboard
Java function host failed to discover main classes for spring cloud function
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
- Prepare a spring cloud function project, could do it by
git clone https://github.com/Azure-Samples/hello-spring-function-azure.git. - Build the spring cloud function project by
mvn clean package - Invoke the function locally by
mvn azure-functions:run.- This issue could also repo in service side, just remove the
MAIN_CLASSsettings in azure function configuration
- This issue could also repo in service side, just remove the
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
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.
More context from Julien: 
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)
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.
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:

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

Hi @ferblaca , few things we should be clear about
- 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

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.
- I don't think appsetting
MAIN_CLASSis 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 appsettingMAIN_CLASS.

I am spring-cloud-function-dependencies 4.0.0-SNAPSHOT, please check your version. Thanks.
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.
@msftbot please keep this opened
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.
Seems like azure-functions-java still doesn't add MAIN_CLASS to MANIFEST.MF
Please reopen, the azure-functions-java still doesn't add
MAIN_CLASStoMANIFEST.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 , thanks, you're right, with latest dependencies it works if specify
MAIN_CLASSinlocal.settings.jsonMAIN_CLASSinsrc/main/azure/local.settings.jsonstart-classinpom.xml