Java
globals Java
Global utilities for loading classes, constructing instances, boxing primitives, and bridging callbacks between Java and the script thread.
Table of contents
Methods
- boxBoolean
- boxByte
- boxChar
- boxDouble
- boxFloat
- boxInt
- boxLong
- boxShort
- create
- createSync
- defineClass
- findClass
- findClassOrNull
- loadDex
- loadJar
- setDefaultThreadMode
- setThreadMode
- wrap
Methods
boxBoolean
boxBoolean(value: boolean): any
Boxes value as a java.lang.Boolean. Use this when calling overloaded Java methods so the call resolves to the boolean / java.lang.Boolean overload instead of another primitive overload.
Example
"nodejs";
require('rhino').install();
const $java = $autojs.java;
const values = new android.content.ContentValues();
// public void put(String key, Boolean value)
values.put("key", $java.boxBoolean(true));Parameters
value:boolean— value to box.
Returns
any
boxByte
boxByte(value: number): any
Boxes value as java.lang.Byte for disambiguating overloaded methods (byte vs java.lang.Byte).
Example
"nodejs";
require('rhino').install();
const $java = $autojs.java;
const values = new android.content.ContentValues();
values.put("key", $java.boxByte(1));Parameters
value:number— byte-sized value.
Returns
any
boxChar
boxChar(value: number): any
Boxes a UTF-16 code unit as java.lang.Character (or the runtime’s char wrapper) so char/Character overloads can be selected reliably.
Example
"nodejs";
require('rhino').install();
const $java = $autojs.java;
const values = new android.content.ContentValues();
values.put("key", $java.boxChar(65));Parameters
value:number— char code.
Returns
any
boxDouble
boxDouble(value: number): any
Boxes value as java.lang.Double.
Example
"nodejs";
require('rhino').install();
const $java = $autojs.java;
const values = new android.content.ContentValues();
values.put("key", $java.boxDouble(1));Parameters
value:number
Returns
any
boxFloat
boxFloat(value: number): any
Boxes value as java.lang.Float.
Example
"nodejs";
require('rhino').install();
const $java = $autojs.java;
const values = new android.content.ContentValues();
values.put("key", $java.boxFloat(1));Parameters
value:number
Returns
any
boxInt
boxInt(value: number): any
Boxes value as java.lang.Integer.
Example
"nodejs";
require('rhino').install();
const $java = $autojs.java;
const values = new android.content.ContentValues();
values.put("key", $java.boxInt(1));Parameters
value:number
Returns
any
boxLong
boxLong(value: number): any
Boxes value as java.lang.Long.
Example
"nodejs";
require('rhino').install();
const $java = $autojs.java;
const values = new android.content.ContentValues();
values.put("key", $java.boxLong(1));Parameters
value:number
Returns
any
boxShort
boxShort(value: number): any
Boxes value as java.lang.Short.
Example
"nodejs";
require('rhino').install();
const $java = $autojs.java;
const values = new android.content.ContentValues();
values.put("key", $java.boxShort(1));Parameters
value:number
Returns
any
create
create(constructor, args, threadMode?): Promise<any>
Creates a Java instance on the chosen ThreadMode and returns a Promise. Many Android UI types must be constructed on the UI thread ('ui').
Example
"nodejs";
const $java = $autojs.java;
const View = $java.findClass('android.view.View');
const context = $autojs.androidContext;
async function main() {
const view = await $java.create(View, [context], 'ui');
console.log(view);
}
main();Parameters
| Name | Type | Default | Description |
|---|---|---|---|
constructor | any | — | Java class constructor function. Throws TypeError if not a Java constructor. |
args | any[] | — | Arguments passed to the Java constructor. |
threadMode? | ThreadMode | 'current' | Thread used to run the constructor. |
Returns
Promise<any>
createSync
createSync(constructor, args, threadMode?): any
Same as create but blocks until the object exists and returns it directly (not a Promise). Prefer create unless you know blocking is safe on your thread.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
constructor | any | — | Java class constructor function. Throws TypeError if invalid. |
args | any[] | — | Constructor arguments. |
threadMode? | ThreadMode | 'current' | Thread for construction. |
Returns
any
defineClass
defineClass(jsClass, options?): Promise<C>
Generates a Java class from a JavaScript class that extends a Java type and/or implements Java interfaces—useful for abstract base classes or overriding methods such as WebViewClient.shouldOverrideUrlLoading.
Implementation builds DEX on demand (can be slow the first time; subsequent calls may reuse cached DEX). By default the cache file lives under .codecache in the current working directory; configure paths via DefineClassOptions.
If you only need a single Java interface, you can often use new SomeInterface({ ... }) instead of defineClass.
Example
"nodejs";
require('rhino').install();
const $java = $autojs.java;
async function main() {
const MyWebViewClient = await $java.defineClass(
class MyWebViewClient extends android.webkit.WebViewClient {
shouldOverrideUrlLoading(webview, url) {
if (typeof(url) === 'string') {
console.log(url);
}
return false;
}
}
);
const client = new MyWebViewClient();
}
main().catch(console.error);Parameters
| Name | Type | Description |
|---|---|---|
jsClass | C | JavaScript class inheriting Java types / implementing interfaces. |
options? | DefineClassOptions | Package name, interfaces, DEX cache path, etc. |
Returns
Promise<C>
findClass
findClass(name: string): any
Loads the class named name and returns its Java-side constructor (a JavaScript-callable function). You can new it for instances or call static methods on it.
Inner classes use $ in the name, e.g. android.app.AlertDialog$Builder.
Throws ClassNotFoundError if the class cannot be loaded.
Example
"nodejs";
const $java = $autojs.java;
const Integer = $java.findClass('java.lang.Integer');
const int = new Integer(255);
console.log(Integer.toHexString(int));Parameters
name:string— fully qualified Java class name.
Returns
any — the Java class / constructor function.
findClassOrNull
findClassOrNull(name: string): any
Same as findClass, but returns null if the class is missing instead of throwing.
See also: findClass
Parameters
name:string
Returns
any — Java class or null.
loadDex
loadDex(dexFile: string): Promise<void>
Loads a .dex file asynchronously. Classes from that DEX become available to JavaScript after the promise settles.
Always await completion before constructing types from that DEX.
Example
"nodejs";
const $java = $autojs.java;
require('rhino').install();
async function main() {
await $java.loadDex('/sdcard/mydex.dex');
console.log(new com.example.MyClass());
}
main().catch(console.error);Parameters
dexFile:string— absolute path to the.dexfile.
Returns
Promise<void>
loadJar
loadJar(jarFile: string): Promise<void>
Loads a .jar similarly to loadDex: the runtime typically converts the JAR to DEX internally, so unsupported bytecode or API mismatches can fail. When possible, pre-convert to DEX for faster, more reliable loads.
Example
"nodejs";
const $java = $autojs.java;
require('rhino').install();
async function main() {
await $java.loadJar('/sdcard/myjar.jar');
console.log(new com.example.MyClass());
}
main().catch(console.error);Parameters
jarFile:string— path to the.jarfile.
Returns
Promise<void>
setDefaultThreadMode
setDefaultThreadMode(clazz: any, threadMode: ThreadMode): any
Sets the default ThreadMode for new instances of clazz. Existing instances are unchanged.
Example
"nodejs";
const $java = $autojs.java;
const View = $java.findClass('android.view.View');
$java.setDefaultThreadMode(View, 'ui');Parameters
| Name | Type | Description |
|---|---|---|
clazz | any | Java class object returned by findClass. |
threadMode | ThreadMode | Default thread for future instances. |
Returns
any
setThreadMode
setThreadMode(obj: any, threadMode: ThreadMode): any
Pins obj to a thread. Subsequent method calls marshal to that thread; if not on the current thread, marshaled calls resolve to Promises.
Example
"nodejs";
const $java = $autojs.java;
const TextView = $java.findClass('android.widget.TextView');
(async () => {
const textView = await $java.create(TextView, [$autojs.androidContext], 'ui');
$java.setThreadMode(textView, 'ui');
await textView.setText('Hello World');
})();Parameters
| Name | Type | Description |
|---|---|---|
obj | any | Java object wrapper. |
threadMode | ThreadMode | Thread to execute calls on. |
Returns
any
wrap
wrap<T>(obj: T, sync?: boolean): T
Wraps a JavaScript object (function or interface-shaped object) so that when Java invokes it from another thread, execution jumps back to the script thread. If sync === true, Java blocks until the script thread returns a value.
Typical use: Android listeners invoked on non-script threads.
Example
"nodejs";
const $java = $autojs.java;
const Button = $java.findClass('android.widget.Button');
(async () => {
const button = await $java.create(Button, [$autojs.androidContext], 'ui');
$java.setThreadMode(button, 'ui');
button.setOnClickListener($java.wrap(() => {
console.log('click');
}));
button.setOnLongClickListener($java.wrap({
onLongClick: () => {
console.log('long click');
return true;
},
}, true));
})();Type parameters
| Name |
|---|
T |
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
obj | T | — | Listener function or object with interface methods. |
sync? | boolean | false | When true, block the Java caller until the script completes. |
Returns
T
