Search code examples
javavisual-studio-codeapache-flinkflink-streaming

Unable to debug Flink example in Visual Studio due to absence of mainClass


My aim is to debug this code to see it live as suggested in the tutorial.

My IDE is Visual Studio Code and the language of this example is Java. I am running a Flink Datastream API tutorial.

When I put a breakpoint at any of the line, I get the following error:

Cannot find a class with the main method in the folder 'frauddetection'.

I was hinted towards created a launch.json file and so, this is what I created:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "type": "java",
            "name": "Debug (Launch) - Current File",
            "request": "launch",
            "mainClass": "FraudDetectionJob"
        }
    ]
}

Even after configuring the above file and specifying the mainClass, I am getting the same error in the debugger terminal:

Error: Could not find or load main class FraudDetectionJob

The code that I am trying to execute is as follows:

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package spendreport;

import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.walkthrough.common.sink.AlertSink;
import org.apache.flink.walkthrough.common.entity.Alert;
import org.apache.flink.walkthrough.common.entity.Transaction;
import org.apache.flink.walkthrough.common.source.TransactionSource;

/**
 * Skeleton code for the datastream walkthrough
 */
public class FraudDetectionJob {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStream<Transaction> transactions = env
            .addSource(new TransactionSource())
            .name("transactions");

        DataStream<Alert> alerts = transactions
            .keyBy(Transaction::getAccountId)
            .process(new FraudDetector())
            .name("fraud-detector");

        alerts
            .addSink(new AlertSink())
            .name("send-alerts");

        env.execute("Fraud Detection");
    }
}

How do I debug this file?


Solution

  • I believe you are not able to compile and run the application. In your pom.xml, this is causing the issue

    <dependency>
        <groupId>org.apache.flink</groupId>
        <artifactId>flink-streaming-java_${scala.binary.version}</artifactId>
        <version>${flink.version}</version>
        <scope>provided</scope>
    </dependency>
    

    Change the scope to compile in order to resolve this issue. Also, more technical info here.

    -- How I went about finding the issue--

    I tried running the example in the tutorial (in Intellij IDEA) and got this error similar to you:

    Error: Unable to initialize main class spendreport.FraudDetectionJob
    Caused by: java.lang.NoClassDefFoundError: org/apache/flink/streaming/api/functions/source/SourceFunction
    

    On searching further I found this answer which is similar.