Search code examples
javamavenintellij-ideadiscord-jda

Java: cannot find symbol. JDA


Working on a bot for discord in Java (JDA of course), there was a similar problem of compilation, I’m a beginner, in half a day thinking, tell me the solution, please. IDE - Intellij IDEA (Maven)

main code:

package me.n1ghtsoul;


import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;

public class Bot {
    private static JDA jda;
    private static final String Token_Api = "MTEwNzcxMzEzNDUxMDIzMTYxNQ.G7NnYJ.Zcy_Ij80ey0oLivIKZ4pam6pP6A8MwG3pp4Ndk";

    public static void main(String[] args) {
        botInit();
    }

    private static void botInit(){
        JDABuilder jdaBuilder = new JDABuilder.createDefault(Token_Api);

        jda = jdaBuilder.setActivity(Activity.listening("крутая музыка"))
                .build();
    }
}

pom.xml:

<?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>me.n1ghtsoul</groupId>
    <artifactId>NullifierReworked</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>


    <dependencies>
        <dependency>
            <groupId>net.dv8tion</groupId>
            <artifactId>JDA</artifactId>
            <version>5.0.0-beta.9</version>
            <exclusions>
                <exclusion>
                    <groupId>club.minnced</groupId>
                    <artifactId>opus-java</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>


</project>

Error:

17:47
java: cannot find symbol
  symbol:   class createDefault
  location: class net.dv8tion.jda.api.JDABuilder

Looking for answers on the forums, found a couple of tips:

  1. Create jar and somehow do something with the plugin that one of the conversation participants
  2. Reinstall IDE
  3. Import packages and classes incorrectly

Actually, that’s all I’ve tried so far. It’s all in vain.


Solution

  • Delete the new

    The method JDABuilder.createDefault is a static factory method. Calling a method returns an object.

    So eliminate new. Use new only to access constructor.

    Change this:

    JDABuilder jdaBuilder = new JDABuilder.createDefault(Token_Api);
    

    … to this:

    JDABuilder jdaBuilder = JDABuilder.createDefault(Token_Api);
    

    You will find other examples of static factory methods in the java.time classes. For example, to capture today’s date:

     LocalDate today = LocalDate.now() ;  // Capture the current date as seen in the JVM’s current default time zone.
    

    Or represent a specific date:

    LocalDate localDate = LocalDate.of( 2024 , Month.JANUARY , 23 ) ;
    

    By the way, static is not object-oriented.

    A static factory method is some code (a method) attached arbitrarily to a class itself (static) rather than to instances (objects) of that class. You can think of this as a hack, but a handy hack, for when we want to make available some behavior without the state and other methods contained in an object.

    This distinction may not be clear when starting out learning object-oriented programming. Do not worry about it now, but revisit the idea when you have more experience.