Search code examples
javaapache-flinkclassnotfound

classNotFound Apache Flink in java


i am trying to write the most simple flink application in java api. I have tried flink 1.19 with java 21 and tried flink 1.8 with java 11 and 17 but i get the same error each time on the same code :

import org.apache.flink.table.api.EnvironmentSettings;
import org.apache.flink.table.api.TableEnvironment;
public class Main {
    public static void main(String[] args) {
        EnvironmentSettings settings = EnvironmentSettings.inBatchMode();
        TableEnvironment tEnv = TableEnvironment.create(settings);
}}

i have all the dependencies in my pom.xml and i have tried the correct version for each try, once 1.18 and then 1.19 of dependencies. here is the error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/flink/table/api/EnvironmentSettings
    at nilian.Main.main(Main.java:9)
Caused by: java.lang.ClassNotFoundException: org.apache.flink.table.api.EnvironmentSettings
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
    ... 1 more

I have expected this code to run without any errors. i have tried flink 1.18 with jdk 11 and another time 17 and i tried flink 1.19 with jdk 21 and another time with 17 i don't know why i get this error


Solution

  • If you already have all the required Flink dependencies in your pom.xml, you probably don't have them implicitly loaded into the classpath.

    This can be fixed in IntelliJ IDEA by going to Run > Edit Configurations > Modify options > Select include dependencies with "Provided" scope.

    Checkout this example from the official doc: https://nightlies.apache.org/flink/flink-docs-release-1.19/docs/try-flink/datastream/#running-in-an-ide