skip to Main Content

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:UsersUserNamePicturesPowerPagesstyringsgruppen-powerpages-siteJavavscode> 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.

2

Answers


  1. please 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

    Login or Signup to reply.
  2. 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 
    

    enter image description here

    I successfully sent the POST request for the above HTTP trigger function.

    http://localhost:7071/api/Sales
    

    enter image description here

    I got the following logs after sending the POST request.

    enter image description here

    Azure SQL db data :

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search