简体   繁体   中英

Maven java: package does not exist

I am making a discord bot in java with JDA and I wanted to try to host it. I found out the best way to build an IntelliJ project is with maven. If I run mvn install i get the following error.

> java: package net.dv8tion.jda.api does not exist

I searched stack overflow and think I need to add a dependency but because this is my first time using java I really can't find out how to implement it in my project. Can anyone tell me how to fix the problem and how to implement it?

Here is my pom 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>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <groupId>bananaaction</groupId>
    <artifactId>DiscordBot</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>net.dv8tion</groupId>
            <artifactId>JDA</artifactId>
            <version>4.2.0_222</version>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>jcenter</id>
            <name>jcenter-bintray</name>
            <url>https://jcenter.bintray.com</url>
        </repository>
    </repositories>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId> <!-- In Red -->
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.bananaaction.Main</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

As you guessed, the problem is that you didn't include JDA as a dependency (library).

In order to do this, add this to the pom.xml :

<dependencies>
    <dependency>
        <groupId>net.dv8tion</groupId>
        <artifactId>JDA</artifactId>
        <version>4.2.0_222</version>
    </dependency>
</dependencies>

<repositories>
    <repository>
        <id>jcenter</id>
        <name>jcenter-bintray</name>
        <url>https://jcenter.bintray.com</url>
    </repository>
</repositories>

See the README of JDA for details.

The first section ( <dependencies> ) declares your dependencies (libraries).

Here, you add a dependency with the group id net.dv8tion and artifact id JDA with version 4.2.0_222 (the latest version at the time of writing this (may 2 nd , 2020).

The second section ( <repositories> ) defines where to look for. JDA is not in the maven central (the default) repository but in the jcenter repository so you need to add the jcenter repository to your pom.xml .

If you package it to a JAR, dependencies will not be included. In order to do this, you can use the maven-assembly-plugin , for example.

You can add the plugin to the pom.xml wth the following code in the <plugins> secion:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
        <archive>
          <manifest>
            <mainClass>yourPackage.yourClass</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
      <executions>
        <execution>
          <id>make-assembly</id>
          <phase>package</phase>
          <goals>
            <goal>single</goal>
          </goals>
        </execution>
      </executions>
</plugin> 

Integrated in your pom.xml , it could look like this:

<?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>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>

    </properties>
    <groupId>com.bananaaction</groupId>
    <artifactId>IdeaProjects</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>net.dv8tion</groupId>
            <artifactId>JDA</artifactId>
            <version>4.2.0_222</version>
        </dependency>
    </dependencies>
    
    <repositories>
        <repository>
            <id>jcenter</id>
            <name>jcenter-bintray</name>
            <url>https://jcenter.bintray.com</url>
        </repository>
    </repositories>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                    <archive>
                    <manifest>
                        <mainClass>yourPackage.yourClass</mainClass>
                    </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    </execution>
                </executions>
            </plugin> 
        </plugins>
    </build>


</project>

Don't forget to change the main class, however.

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