0

我正在尝试使用 geoip2 为 BungeeCord (Minecraft) 编写插件。我试图在我的 jar 文件中包含依赖项“com.maxmind.geoip2”和 BungeeCord API jar 文件,因为我得到了一个java.lang.NoClassDefFoundError: com/maxmind/geoip2/exception/GeoIp2Exception

java.lang.NoClassDefFoundError: com/maxmind/geoip2/exception/GeoIp2Exception
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
at java.lang.Class.getConstructor0(Class.java:3075)
at java.lang.Class.getDeclaredConstructor(Class.java:2178)
at net.md_5.bungee.api.plugin.PluginManager.enablePlugin(PluginManager.java:305)
at net.md_5.bungee.api.plugin.PluginManager.loadPlugins(PluginManager.java:212)
at net.md_5.bungee.BungeeCord.start(BungeeCord.java:259)
at net.md_5.bungee.BungeeCordLauncher.main(BungeeCordLauncher.java:56)
at net.md_5.bungee.Bootstrap.main(Bootstrap.java:15)
Caused by: java.lang.ClassNotFoundException: com.maxmind.geoip2.exception.GeoIp2Exception
at net.md_5.bungee.api.plugin.PluginClassloader.loadClass0(PluginClassloader.java:53)
at net.md_5.bungee.api.plugin.PluginClassloader.loadClass(PluginClassloader.java:27)
at java.lang.Cl

我试过这个,但它对我不起作用。

在研究更多时,我遇到了 maven 着色器,我目前在我的 pom.xml 中有这个:

<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>general</groupId>
<artifactId>CubenexBungee</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>CubenexBungee</name>
<url>http://maven.apache.org</url>

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>com.maxmind.geoip2</groupId>
        <artifactId>geoip2</artifactId>
        <version>2.11.0</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-shade-plugin</artifactId>
     <version>3.1.0</version>
     <executions>
        <execution>
           <phase>package</phase>
           <goals>
              <goal>shade</goal>
           </goals>
           <configuration>
              <artifactSet>
                 <excludes>
                    <exclude>org:*</exclude>
                 </excludes>
                 <includes>
                    <include>com.maxmind.geoip2:geoip2</include>
                    <include>Referenced Libraries:BungeeCord.jar</include>
                 </includes>
              </artifactSet>
           </configuration>
        </execution>
     </executions>
  </plugin>
    </plugins>
</build>

我的 App.java 是:

public class App extends Plugin implements Listener{

public static DatabaseReader database;

@Override
public void onEnable(){
    getProxy().getPluginManager().registerListener(this, this);
    try {
        database = new DatabaseReader.Builder(new File(this.getDataFolder().getPath()+"//countrydatabase.csv")).build();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

@EventHandler
public void login(LoginEvent e){
    CountryResponse response;
    try {
        response = database.country(e.getConnection().getAddress().getAddress());
        System.out.println(e.getConnection().getName()+" comes from "+response.getCountry().getName());
    } catch (IOException e1) {
        e1.printStackTrace();
    } catch (GeoIp2Exception e1) {
        e1.printStackTrace();
    }
}

}

我必须说,我只是刚刚发现了 maven,并没有真正理解它,也没有找到任何我理解的教程。

4

1 回答 1

1

你想要做的是很多人称之为“胖罐子”或“超级罐子”的东西。
你可以使用 maven-shade-plugin 来创建它。
以下是有关它的更多信息:http ://www.mkyong.com/maven/create-a-fat-jar-file-maven-shade-plugin/

于 2018-05-08T14:52:47.080 回答