简体   繁体   中英

Minecraft Forge Gradle adds external dependencies ,but does not build the dependencies to the jar

This is my Gradle file

buildscript {
    repositories {
        maven { url = 'https://files.minecraftforge.net/maven' }
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath group: 'net.minecraftforge.gradle', name: 'ForgeGradle', version: '3.+', changing: true
        classpath files('libs/json-lib-2.4-jdk15.jar')
    }
}
tasks.withType(JavaCompile) {
    options.encoding = "UTF-8"
}
apply plugin: 'net.minecraftforge.gradle'
group = 'siongsng.rpmtwupdatemod'
version = '1.0.0'
archivesBaseName = 'rpmtw_update_mod'
sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = '1.8'
minecraft {
    mappings channel: 'snapshot', version: '20210309-1.16.5'
    runs {
        client {
            workingDirectory project.file('run')
            property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP'
            property 'forge.logging.console.level', 'debug'
            mods {
                rpmtwupdatemod {
                    source sourceSets.main
                }
            }
        }
        server {
            workingDirectory project.file('run')
            property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP'
            property 'forge.logging.console.level', 'debug'
            mods {
                rpmtwupdatemod {
                    source sourceSets.main
                }
            }
        }
        data {
            workingDirectory project.file('run')
            property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP'
            property 'forge.logging.console.level', 'debug'
            args '--mod', 'rpmtwupdatemod', '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/')
            mods {
                rpmtwupdatemod {
                    source sourceSets.main
                }
            }
        }
    }
}
sourceSets.main.resources { srcDir 'src/generated/resources' }
dependencies {
    implementation files('libs/json-lib-2.4-jdk15.jar')
    minecraft 'net.minecraftforge:forge:1.16.5-36.1.1'
}
jar {
    manifest {
        attributes([
                "Specification-Title"     : "rpmtwupdatemod",
                "Specification-Vendor"    : "SiongSng",
                "Specification-Version"   : "1",
                "Implementation-Title"    : project.name,
                "Implementation-Version"  : version,
                "Implementation-Vendor"   : "SiongSng",
                "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ")
        ])
    }
}
jar.finalizedBy('reobfJar')

However, when I use Gradle Build, the following error occurs when the output jar is run in Minecraft

java.lang.NoClassDefFoundError: net/sf/json/JSONObject
    at siongsng.rpmtwupdatemod.json.get(json.java:29) ~[rpmtw_update_mod:1.0.0]
    at siongsng.rpmtwupdatemod.json.ver(json.java:38) ~[rpmtw_update_mod:1.0.0]
    at siongsng.rpmtwupdatemod.RpmtwUpdateMod.<init>(RpmtwUpdateMod.java:27) ~[rpmtw_update_mod:1.0.0]
    at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:?]
    at jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:64) ~[?:?]
    at jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:?]
    at java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500) ~[?:?]
    at java.lang.reflect.ReflectAccess.newInstance(ReflectAccess.java:128) ~[?:?]
    at jdk.internal.reflect.ReflectionFactory.newInstance(ReflectionFactory.java:350) ~[?:?]
    at java.lang.Class.newInstance(Class.java:645) ~[?:?]
    at net.minecraftforge.fml.javafmlmod.FMLModContainer.constructMod(FMLModContainer.java:81) ~[forge:36.1]
    at net.minecraftforge.fml.ModContainer.lambda$buildTransitionHandler$4(ModContainer.java:120) ~[forge:?]
    at java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1800) [?:?]
    at java.util.concurrent.CompletableFuture$AsyncRun.exec(CompletableFuture.java:1792) [?:?]
    at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290) [?:?]
    at java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1016) [?:?]
    at java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1665) [?:?]
    at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1598) [?:?]
    at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:183) [?:?]
Caused by: java.lang.ClassNotFoundException: net.sf.json.JSONObject
    at java.lang.ClassLoader.findClass(ClassLoader.java:719) ~[?:?]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:589) ~[?:?]
    at cpw.mods.modlauncher.TransformingClassLoader.loadClass(TransformingClassLoader.java:106) ~[modlauncher-8.0.9.jar:?]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:522) ~[?:?]
    ... 19 more

This is the code in json.java

package siongsng.rpmtwupdatemod;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import net.sf.json.JSONObject;

public class json {
    public static JSONObject get() {
        StringBuilder json = new StringBuilder();
        try {
            URL urlObject = new URL("https://api.github.com/repos/SiongSng/ResourcePack-Mod-zh_tw/releases/latest");
            URLConnection uc = urlObject.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
            String inputLine = null;
            while ( (inputLine = in.readLine()) != null) {
                json.append(inputLine);
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return JSONObject.fromObject(json.toString());
    }

    public static Object loadJson() {
        JSONObject aaa = get();
        return aaa.getJSONArray("assets").getJSONObject(0).get("browser_download_url");
    }

    public static Object ver() {
        JSONObject aaa = get();
        return aaa.get("tag_name");
    }
}

I guess it is because there is no dependency (json-lib) in this jar, but I have set it in the Build configuration file, I would like to ask what causes it Here is the complete code for this mod ( https://github.com/SiongSng/RPMTW-Update-Mod/tree/master/1.16-forge )

try using implementation group: 'net.sf.json-lib', name: 'json-lib', version: '2.4' this you're dependencies

https://mvnrepository.com/artifact/net.sf.json-lib/json-lib/2.4

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