Maven 项目中的运行时 java.lang.NoClassDefFoundError 即使在阴影 jar 中

问题描述 投票:0回答:2

我正在编写一个工具来帮助开发 Minecraft 服务器的 Paper 插件。它使用 Maven 引入相关依赖项,我正在使用一个插件来生成一个阴影 jar。我还使用插件来生成可执行 jar。

我的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>org.example</groupId>
    <artifactId>CustomItemGenerationTool</artifactId>
    <version>1.0-SNAPSHOT</version>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <shadedArtifactAttached>true</shadedArtifactAttached>
                            <transformers>
                                <transformer implementation=
                                                     "org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>Main</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>papermc-repo</id>
            <url>https://repo.papermc.io/repository/maven-public/</url>
        </repository>
        <repository>
            <id>sonatype</id>
            <url>https://oss.sonatype.org/content/groups/public/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>io.papermc.paper</groupId>
            <artifactId>paper-api</artifactId>
            <version>1.18.2-R0.1-SNAPSHOT</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

</project>

脚本以防相关:(是的,它很乱,它只是一个快速而肮脏的工具来节省我的时间)

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

public class Main {
    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.println("Starting....");
        ItemStack item;
        Material material;
        int quantity;
        List<Component> lore;

        // Material?
        System.out.print("What material is the item? ");
        material = Material.getMaterial(scanner.nextLine());

        // Quantity?
        System.out.print("How many are in the stack? (1-64): ");
        quantity = scanner.nextInt();

        //

        item = new ItemStack(material, quantity);

        //

        // Get item name as component
        System.out.println("Item Name:");
        item.getItemMeta().displayName(getComponent());

        // Enchants?
        Enchantment enchant;
        int level;

        System.out.print("Is the item enchanted? ");
        if (scanner.nextLine().equalsIgnoreCase("y")) {
            while (!(scanner.nextLine().equalsIgnoreCase("n"))) {
                System.out.print("What Enchants are on the item? ");
                enchant = Enchantment.getByKey(new NamespacedKey(NamespacedKey.MINECRAFT, scanner.nextLine()));
                System.out.print("What level is that enchant? ");
                level = scanner.nextInt();
                item.addUnsafeEnchantment(enchant, level);

                System.out.println("Another enchant? (y/n)");
            }
        }

        // Lore as component
        System.out.print("Does the item have lore? ");
        if (scanner.nextLine().equalsIgnoreCase("y")) {
            if (item.lore() == null) { lore = new ArrayList<>(0); } else { lore = item.lore(); }

            while (!(scanner.nextLine().equalsIgnoreCase("n"))) {
                lore.add(getComponent());

                System.out.println("Another line? (y/n)");
            }

            item.lore(lore);
        }

        // ItemFlags?
        while (!(scanner.nextLine().equalsIgnoreCase("n"))) {
            System.out.print("Enter flag name: (HIDE_ATTRIBUTES | HIDE_DESTROYS | HIDE_DYE | HIDE_ENCHANTS | HIDE_PLACED_ON | HIDE_POTION_EFFECTS | HIDE_UNBREAKABLE): ");
            item.addItemFlags(ItemFlag.valueOf(scanner.nextLine()));

            System.out.println("Another flag? (y/n)");
        }

        // CustomModelData?
        System.out.print("Does the item have custom model data? (y/n): ");
        if (scanner.nextLine().equalsIgnoreCase("y")) {
            item.getItemMeta().setCustomModelData(scanner.nextInt());
        }

        // Unbreakable?
        System.out.print("Is the item unbreakable? (Y/N) ");
        if (scanner.nextLine().equalsIgnoreCase("y")) {
            item.getItemMeta().setUnbreakable(true);
        } else {
            item.getItemMeta().setUnbreakable(false);
        }
    }

    private static Component getComponent() {
        Component component;
        String name;
        int[] rgb = new int[3];
        HashMap<TextDecoration, TextDecoration.State> decorations = new HashMap<>();
        decorations.put(TextDecoration.BOLD, TextDecoration.State.FALSE);
        decorations.put(TextDecoration.ITALIC, TextDecoration.State.FALSE);
        decorations.put(TextDecoration.STRIKETHROUGH, TextDecoration.State.FALSE);
        decorations.put(TextDecoration.OBFUSCATED, TextDecoration.State.FALSE);
        decorations.put(TextDecoration.UNDERLINED, TextDecoration.State.FALSE);

        System.out.print("Component Name: ");
        name = scanner.nextLine();

        System.out.println("Component Colour? (y/n):");
        if (scanner.nextLine().equalsIgnoreCase("y")) {
            System.out.print("R (0-255): ");
            rgb[0] = scanner.nextInt();

            System.out.print("G (0-255): ");
            rgb[1] = scanner.nextInt();

            System.out.print("B (0-255): ");
            rgb[2] = scanner.nextInt();
        }

        System.out.println("Component Decoration? (y/n):");
        if (!(scanner.nextLine().equalsIgnoreCase("y"))) {
            while (scanner.nextLine().equalsIgnoreCase("n")) {
                System.out.print("Enter decoration name: (BOLD | ITALIC | OBFUSCATED | STRIKETHROUGH | UNDERLINED): ");
                decorations.put(TextDecoration.valueOf(scanner.nextLine()), TextDecoration.State.TRUE);

                System.out.println("Another decoration? (y/n)");
            }
        }

        component = Component.text(
                name
        ).color(
                TextColor.color(
                        rgb[0],
                        rgb[1],
                        rgb[2]
                )
        ).decorations(
                decorations
        );

        return component;
    }
}

产生的堆栈跟踪:

PS C:\Users\gamin\Desktop\natserver\CustomItemGenerationTool> java -jar .\target\CustomItemGenerationTool-1.0-SNAPSHOT-shaded.jar
Starting....
What material is the item? STICK
Exception in thread "main" java.lang.NoClassDefFoundError: org/bukkit/Material
        at Main.main(Main.java:27)
Caused by: java.lang.ClassNotFoundException: org.bukkit.Material
        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:521)
        ... 1 more
java maven classnotfoundexception bukkit
2个回答
2
投票

您错过了添加

org.bukkit
的依赖项。

添加

<!-- https://mvnrepository.com/artifact/org.bukkit/bukkit -->
<dependency>
    <groupId>org.bukkit</groupId>
    <artifactId>bukkit</artifactId>
    <version>1.7.9-R0.2</version>
</dependency>

到你的pom.xml


0
投票

尽管如此,OP可能很久以前就已经继续他们的生活了......

对于像我一样的可怜的灵魂,他连续10个小时用完了他的理智和SSD,修复了这个被上帝遗弃的问题... 检查你是否在本地服务器中安装了纸张,这可能不是解决方案。 ..我不知道...坦白说,我太沮丧了,无法阅读这个问题...

检查一下!请!为了人类的进步!

© www.soinside.com 2019 - 2024. All rights reserved.