admin管理员组文章数量:1125561
I am trying to run an azure function that I wrote in Java locally using func start
. This doesn't work and this is the error message that I'm getting:
`PS C:\Users\UserName\Pictures\PowerPages\styringsgruppen-powerpages-site\Java\vscode> func start
Azure Functions Core Tools Core Tools Version: 4.0.6610 Commit hash: N/A +0d55b5d7efe83d85d2b5c6e0b0a9c1b213e96256 (64-bit) Function Runtime Version: 4.1036.1.23224
[2025-01-07T13:56:37.775Z] 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. [2025-01-07T13:56:43.088Z] Host lock lease acquired by instance ID '000000000000000000000000618D1A8C'.`
The class I'm trying to run is public and the method inside is also public, so this shouldn't be the issue. The error about binding extensions shouldn't be a problem either since I'm not using any startup code in my program. If anyone could help me with this, please respond, any feedback is appreciated.
I am trying to run an azure function that I wrote in Java locally using func start
. This doesn't work and this is the error message that I'm getting:
`PS C:\Users\UserName\Pictures\PowerPages\styringsgruppen-powerpages-site\Java\vscode> func start
Azure Functions Core Tools Core Tools Version: 4.0.6610 Commit hash: N/A +0d55b5d7efe83d85d2b5c6e0b0a9c1b213e96256 (64-bit) Function Runtime Version: 4.1036.1.23224
[2025-01-07T13:56:37.775Z] 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. [2025-01-07T13:56:43.088Z] Host lock lease acquired by instance ID '000000000000000000000000618D1A8C'.`
The class I'm trying to run is public and the method inside is also public, so this shouldn't be the issue. The error about binding extensions shouldn't be a problem either since I'm not using any startup code in my program. If anyone could help me with this, please respond, any feedback is appreciated.
Share Improve this question asked 2 days ago AdmirAdmir 12 bronze badges 5 |2 Answers
Reset to default 0please find below to step by step guide to run a java function app. there are some steps required to setup env and project in vs code. you can run cmd to launch the function app but ideally you would like to run it directly in vs code for debugging etc.
https://learn.microsoft.com/en-us/azure/azure-functions/create-first-function-cli-java?tabs=windows%2Cbash%2Cazure-cli%2Cbrowser
https://learn.microsoft.com/en-us/azure/azure-functions/create-first-function-vs-code-java
To run an HTTP trigger function in Java locally, you can use the command mvn azure-functions:run
.
I tried your code, ran it using the above command, and it worked fine for me.
Sales.java :
import com.microsoft.azure.functions.annotation.*;
import com.microsoft.azure.functions.*;
import java.sql.*;
import java.util.*;
import java.util.logging.Logger;
public class Sales {
@FunctionName("Sales")
public HttpResponseMessage run(
@HttpTrigger(name = "req", methods = {HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS)
HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
Logger logger = context.getLogger();
logger.info("Processing a request in Sales function.");
String formBody = request.getBody().orElse("");
FormParser formParser = new FormParser(formBody);
String url = "jdbc:sqlserver://<sqlServerName>.database.windows.net:1433;database=<dbName>;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30";
String username = "<userName>";
String password = "<Password>";
try (Connection connection = DriverManager.getConnection(url, username, password)) {
logger.info("Database connection successful.");
String query = "EXEC AddSale @product_name=?, @production_cost=?, @price=?, @units_sold=?, @profit=?, @date=?, @month_number=?, @year=?, @indirect_costs=?";
PreparedStatement preparedStatement = connection.prepareStatement(query);
preparedStatement.setString(1, formParser.getProductName());
preparedStatement.setInt(2, formParser.getProductionCost());
preparedStatement.setFloat(3, formParser.getPrice());
preparedStatement.setInt(4, formParser.getUnitsSold());
preparedStatement.setFloat(5, formParser.getProfit());
preparedStatement.setString(6, formParser.getDate());
preparedStatement.setInt(7, formParser.getMonthNumber());
preparedStatement.setInt(8, formParser.getYear());
preparedStatement.setFloat(9, formParser.getIndirectCosts());
preparedStatement.executeUpdate();
logger.info("Stored procedure executed successfully.");
} catch (SQLException e) {
logger.severe("SQL exception: " + e.getMessage());
return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
.body("Error processing request: " + e.getMessage())
.build();
}
return request.createResponseBuilder(HttpStatus.OK)
.body("Sales data processed successfully.")
.build();
}
}
FormParser.java :
public class FormParser {
private String productName;
private int productionCost;
private float price;
private int unitsSold;
private float profit;
private String date;
private int monthNumber;
private int year;
private float indirectCosts;
public FormParser(String formBody) {
this.productName = "SampleProduct";
this.productionCost = 100;
this.price = 150.5f;
this.unitsSold = 10;
this.profit = 500.0f;
this.date = "2025-01-10";
this.monthNumber = 1;
this.year = 2025;
this.indirectCosts = 50.0f;
}
public String getProductName() { return productName; }
public int getProductionCost() { return productionCost; }
public float getPrice() { return price; }
public int getUnitsSold() { return unitsSold; }
public float getProfit() { return profit; }
public String getDate() { return date; }
public int getMonthNumber() { return monthNumber; }
public int getYear() { return year; }
public float getIndirectCosts() { return indirectCosts; }
}
local.settings.json :
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "<storageConnectString>",
"FUNCTIONS_WORKER_RUNTIME": "java"
}
}
pom.xml :
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>12.2.0.jre11</version>
</dependency>
Terminal Output :
mvn clean install
mvn clean package
mvn azure-functions:run
I successfully sent the POST request for the above HTTP trigger function.
http://localhost:7071/api/Sales
I got the following logs after sending the POST request.
Azure SQL db data :
本文标签: javaCannot run azure function locally using func startStack Overflow
版权声明:本文标题:java - Cannot run azure function locally using func start - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736632028a1945801.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
@FunctionName
to your method and runmvn clean package
beforefunc start
. Can you share your code in the question? – Dasari Kamali Commented yesterdaymvn azure-functions:run
to run your Java function locally. Refer this MS DOC for more details. – Dasari Kamali Commented yesterdaymvn azure-functions:run
command to run the function. – Dasari Kamali Commented yesterday