Ash Hagi
Ash Hagi

Reputation: 1

How to make Java compiler ignore the absent interfaces implemented by a dependency class?

I'm modding Minecraft and I imported a mod jar as dependency. However, the compiler prompted error since the class from the dependency mod I invoked method of implemented interfaces from other mods.

In my code, I invoked a method of a class from the dependency jar. There are some simplified code that demonstrate what the code is like.

// From my mod
public class MachineIgniterListener {
    
    public void onPlayerInteract(PlayerInteractEvent event) {
        BaseMetaTileEntity tileEntity = getMachineInEventIfItIs(event);
        if (tileEntity == null) return;

        tileEntity.doEnergyExplosion(); 
    }

}

where BaseMetaTileEntity comes from the dependency mod, and it implements several interfaces from other mods

// From dependency mod
public class BaseMetaTileEntity extends CommonMetaTileEntity 
        implements IActionHost, IAlignmentProvider, IConstructableProvider... {

    public void doEnergyExplosion() {
        // do some explosion
    }
}

IActionHost, IAlignmentProvider, and other interfaces are from other mods which I haven't include in the classpath, resulting in the compiler complaining

        tileEntity.doEnergyExplosion();
                  ^
   class file for appeng.api.networking.security.IActionHost not found

I think, in terms of JVM bytecode, the code does not require knowing what interfaces the class implements to compile (which I might be wrong about it), so I'm expecting the code compiles without the depended jars added to the classpath. However, the compiler requires all the jars to be present. Since I'm coding in legacy versions of Minecraft, finding some mod that it requires can be very tidious.

I used Gradle to include the jar file in my project. Since I'm new to Gradle, I tried different kinds of dependency configurations, and none of them solved the problem.

dependencies {
    // compile files("dependency_mod.jar")
    // implementation files("dependency_mod.jar")
    // api files("dependency_mod.jar")
    // runtimeOnly files("dependency_mod.jar") I can't even refer to the classes with this one
    // Yes I tried lol

    compileOnly files("dependency_mod.jar")
}

I wonder if there are ways to make the code compile without adding all these jars to the dependencies. Am I doing wrong with the Gradle settings? Or there's no other ways around than adding the jars?

Also, if this is relevant, I'm using IntelliJ with Gradle 6.9.1, Java 8, and FalsePattern / ExampleMod1.7.10 modding toolchain template

See also: A question described that resulted in expected behavior - Java Compiler transistive type checking behaviour

Upvotes: 0

Views: 123

Answers (1)

Skod
Skod

Reputation: 469

I wonder if there are ways to make the code compile without adding all these jars to the dependencies.

Short answer: Probably not, you have to add all the dependencies.

In your Gradle file you define the libs that your mod uses in order to compile your app. What Gradle will do then is to connect all of those classes together and build your classpath. Gradle is able to find (and download) all the missing dependencies from repositories, such as maven's. The packages there can declare other dependencies themselves and so on.

What Gradle (and the build tools general) is good for is to traverse those complicated dependency trees and fetch the jars that each projects needs/declares.

In your dependencies though you defined a local jar dependency. That means that you are saying to Gradle:

  • Don't look at a remote repository to get the jar, but I have it ready for you in a local directory

But your local directory is not a repository and it doesn't tell to Gradle which other jars it needs and where to go from there.

Now it depends on the jar that you downloaded if your project will compile. If the jar doesn't contain its dependencies, then you have to find those additional jars yourself, download them and add them to Gradle manually so it will connect the dots and build the classpath.

But check if there's a possibility that there's a dependency_mod.jar out there that contains all of its dependencies in the jar itself - that's generally called a fat or uber jar

Upvotes: 1

Related Questions