Skip to content

Java API ​

The java global table allows interacting directly with Java classes and objects from your scripts.

NOTE

When interacting with Minecraft internals, all classes, methods, and fields use Mojang mappings. This means you must use the official, unobfuscated names as they appear in the Minecraft source code.

Core Functions ​

java.import(class_name) ​

Loads a Java class by its full package name. Returns a Lua representation of the class.

lua
local Math = java.import("java.lang.Math")
print(Math:abs(-1))

With the Lua API definitions installed, literal class names resolve to the imported class's static members. Java methods use : for both instance and static calls:

lua
local Minecraft = java.import("net.minecraft.client.Minecraft")
local InteractionHand = java.import("net.minecraft.world.InteractionHand")

if mc.player then
    mc.player:swing(InteractionHand.MAIN_HAND)
end

local minecraft = Minecraft:getInstance()
local Math = java.import("java.lang.Math")
local positive = Math:abs(-1)

Types outside the supported import packages and method-level generic variables currently fall back to any, class-level generics preserve collection element types and generic inheritance.

java.new(class_obj, ...) ​

Creates a new instance of a Java class using its class object.

lua
local ArrayList = java.import("java.util.ArrayList")
local list = java.new(ArrayList)
list:add("Hello")

java.newInstance(class_name, ...) ​

Creates a new instance of a Java class using its full class name.

lua
local list = java.newInstance("java.util.ArrayList")
list:add("World")

java.proxy(interface_name, table) ​

Creates a Java proxy for an interface, using a Lua table for method implementations.

lua
local my_runnable = java.proxy("java.lang.Runnable", {
    run = function()
        print("Running in Java thread!")
    end
})

Utility Functions ​

java.is_instance(obj, class_or_name) ​

Checks if a Java object is an instance of a specific class or class name. The Lua API definitions include this function for autocomplete, with an imported Java class or string as its second parameter and a boolean return value.

lua
local Player = java.import("net.minecraft.world.entity.player.Player")

if java.is_instance(entity, Player) then
    print("This is a player!")
end

if java.is_instance(entity, "java.lang.String") then
    print("This is a string!")
end

is_instance(obj, class_or_name) (deprecated) ​

Use java.is_instance(obj, class_or_name) instead. The global function remains available as an alias for backwards compatibility and accepts the same arguments.