简体   繁体   中英

Java Package doesnt not exist in intelliJ IDEA

I am new to Java and i have to execute a code snippet related to Kafka which is given below:

import java.util.*;
import org.apache.kafka.clients.producer.*;

public class Producer {

    public static void main(String[] args) throws Exception
    {

        String topicName = "SimpleProducerTopic";
        String key = "Key1";
        String value = "Value-1";

        Properties props = new Properties();
        props.put("bootstrap.servers", "localhost:9092,localhost:9093");
        props.put("key.serializer","org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");

        Producer<String, String> producer = new KafkaProducer <>(props);

        ProducerRecord<String, String> record = new ProducerRecord<>(topicName,key,value);
        producer.send(record);
        producer.close();

        System.out.println("SimpleProducer Completed.");
    }

}

I have downloaded IntelliJ Idea editor and running the above script there but it is giving me an error

Error:(2, 1) java: package org.apache.kafka.clients.producer does not exist

I know that i apache kafka is missing so i downloaded a jar file of apache and add it to modules but the error still persist. What should i do? How do install the pacakge?

Simply adding the jar to the corresponding module does not give you access to it. Have you tried right click on the jar Add as Library... option?

Edit: you could perhaps explore other options for external library usage like maven, or gradle.

One way to install the package is to use Maven. If you want to configure Maven and IntelliJ, take a look at this tutorial . Eventually, once you are done, you should add this to the auto-generated pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>Whatever you put during setup</groupId>
    <artifactId>Whatever you put during setup</artifactId>
    <version>1.0-SNAPSHOT</version>

    //Add this - copy and paste
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-clients -->
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-clients</artifactId>
            <version>2.1.0</version>
        </dependency>
    </dependencies>

</project>

You can add any additional packages/dependencies within the <dependencies></dependencies> tags. There are plenty of tutorials online on how to handle dependencies using Maven.

As being new to Java, you'll want to understand what is the classpath.

Putting JARs directly into your IDE isn't changing that

Even from the command line, you'd need to explicitly specify -cp kafka-clients.jar

There's multiple ways to modify the module classpath in Intellij, but manually downloading JARs should be avoided, and that problem is solved by dependency management tools such as Maven or Gradle (or sbt, etc)

Your profile mentions other languages, so think Nuget, npm, pip, etc. Apply that knowledge to Java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM