To host a Discord4J bot 24/7 you run it on a machine that stays on: a Linux VPS with a systemd service that starts it again when it stops, or a bot plan on PlayNHost Discord bot hosting (from US$1.99 a month), where you upload one runnable jar, paste the token and press Start. In our test a Discord4J 3.3.3 bot used about 126 MB of memory on Java 21 and got Discord's answer 2.6 seconds after it was launched. Discord4J is a library, not a program, so you build your bot into a jar on your own computer first. The steps on PlayNHost:
- On the Discord bot hosting page, click Create Server on a plan. In the order window, answer What is your bot for? with Discord and pick Java.
- On your computer, build the bot with the
pom.xmlbelow:mvn packagewritestarget/bot.jar, with Discord4J inside. - Open your server in the panel. The Set up your bot card on Overview opens at Connect to Discord. Its first step, What runs, should say This bot runs Java Bot.; click that step to check it, then Continue.
- At Connect to Discord, paste your token into Paste your bot token, click Connect, then Yes, continue.
- At Permissions, tick My code uses this only for the privileged intents your code asks for (the bot below needs none), click Add to a Discord server, then Continue.
- At Your code, click Upload files and upload
bot.jarto the main folder. - On the Bot page, pane Code, check that Entry file is
bot.jarand Runtime is Java 21 (recommended), or Java 17 or 25. If you change Entry file, click Save next to it; a new Runtime is saved as soon as you pick it. - Click Start bot and watch Console. When your bot prints
Logged in as ..., the card says Your bot is online in Discord.
Creating the bot and its token is explained in How to create a Discord bot and get its token, and the Java bot type in general in How to host a Java Discord bot. This page is about Discord4J itself: what it uses on a server and what to know before you host it.
What we measured
| Library | Version | Memory at rest | Launch to Discord's answer | Jar size |
|---|---|---|---|---|
| Discord4J | 3.3.3 | 126 MB | 2.6 s | 37 MB |
| JDA | 6.7.0 | 91 MB | 1.5 s | 19 MB |
| Java 21 alone, no library | 21.0.12.1 | 38 MB | none | 0.0 MB |
How: on our own node, in a throwaway container set up like a 1 GB bot plan (the Java 21 image and the exact java command of our Java bot type), each bot logged in with a fake token, which Discord refused; memory is the resident memory 15 seconds after that answer, start time runs from launch to the answer, and each figure is the median of runs 2 to 4.
These are floors: no servers, no cached members, no open gateway session yet. Your own code and the number of servers your bot is in add to them, and the start script lets the Java heap grow up to 80 percent of the plan (see below). Compare the numbers with the plans on Discord bot hosting and choose by what your bot does.
A minimal Discord4J bot
It registers a /ping slash command, answers it with Pong!, reads the token from DISCORD_TOKEN (the name the panel gives it) and prints a ready line. It needs no privileged intent. Two files:
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>bot</artifactId>
<version>1.0</version>
<properties>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.discord4j</groupId>
<artifactId>discord4j-core</artifactId>
<version>3.3.3</version>
</dependency>
</dependencies>
<build>
<finalName>bot</finalName>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.7.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>Bot</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
src/main/java/Bot.java
import discord4j.core.DiscordClient;
import discord4j.core.event.domain.interaction.ChatInputInteractionEvent;
import discord4j.core.event.domain.lifecycle.ReadyEvent;
import discord4j.discordjson.json.ApplicationCommandRequest;
import discord4j.rest.http.client.ClientException;
import reactor.core.publisher.Mono;
public class Bot {
public static void main(String[] args) {
String token = System.getenv("DISCORD_TOKEN");
if (token == null || token.isBlank()) {
System.out.println("No DISCORD_TOKEN found. Paste your bot token on the Bot page, then restart.");
System.exit(1);
}
ApplicationCommandRequest ping = ApplicationCommandRequest.builder()
.name("ping")
.description("Replies with Pong!")
.build();
try {
DiscordClient.create(token).withGateway(gateway -> {
// Creating a command that already exists updates it, so this is safe on every start.
Mono<Void> register = gateway.getRestClient().getApplicationId()
.flatMap(appId -> gateway.getRestClient().getApplicationService()
.createGlobalApplicationCommand(appId, ping))
.then();
Mono<Void> ready = gateway.on(ReadyEvent.class, event -> Mono.fromRunnable(() ->
System.out.println("Logged in as " + event.getSelf().getUsername())))
.then();
Mono<Void> commands = gateway.on(ChatInputInteractionEvent.class, event -> {
if (event.getCommandName().equals("ping")) {
return event.reply("Pong!");
}
return Mono.empty();
}).then();
return register.and(ready).and(commands);
}).block();
} catch (RuntimeException e) {
if (ClientException.isStatusCode(401).test(e)) {
System.out.println("Discord did not accept the bot token. Copy it again (Developer Portal, Bot, Reset Token), paste it on the Bot page, then restart.");
} else {
e.printStackTrace();
}
System.exit(1);
}
}
}
Build it with Maven 3.9 and a JDK 17 or newer:
mvn package
java -jar target/bot.jar
The second line is a test on your computer: without a token it prints No DISCORD_TOKEN found. and stops, which proves the jar starts and finds its main class. Upload target/bot.jar. The maven-assembly-plugin's jar-with-dependencies puts Discord4J and everything it needs into that one jar; Gradle users get the same with the Shadow plugin. maven.compiler.release 17 makes a jar that runs on all three Java versions the panel offers.
The line Logged in as ... is what turns the chip on Overview to Online in Discord. Invite the bot with Add to a Discord server before you try /ping: How to invite your Discord bot to a server.
If the token is wrong
In our test, with a fake token, Discord4J's login failed with this error:
ClientException: GET /gateway/bot returned 401 Unauthorized with response {code=0, message=401: Unauthorized}
The bot above catches a refused token (HTTP 401) and prints Discord did not accept the bot token. Copy it again (Developer Portal, Bot, Reset Token), paste it on the Bot page, then restart. instead of a stack trace, then stops with exit code 1. When we ran it with a fake token, three SLF4J: warning lines came first, starting with SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".; they are about logging, not the token (see Logs below). To fix it, open the Discord Developer Portal, your application, Bot, and click Reset Token; Discord shows the new token once. On our Bot page, pane Discord, click Replace token, paste it, click Save token, then Restart now. When Discord refuses a check of the saved token, the pane shows Discord did not accept it.
What to know about Discord4J on a server
- Versions: Discord4J 3.3.3 came out on 19 Aug 2026. The 3.3 line is the current one and 3.2 gets maintenance only. Discord4J 3.3.3 is compiled for Java 8, so any Runtime the panel offers (Java 17, 21 or 25) runs it; build with a
releaseno newer than the runtime you pick. - One jar, nothing installed on the server. The Java bot type has no Git source and no Packages field: it starts
java -jaron your Entry file, so everything your bot uses must be inside the jar. Upload a new jar to update the bot. - Memory limit: the start script runs Java with
-XX:MaxRAMPercentage=80.0and the G1 garbage collector, so the heap can use up to 80 percent of your plan's memory and the rest of the process has room. If the heap fills up, Java printsjava.lang.OutOfMemoryErrorand the panel shows Your bot ran out of memory. - Intents: Discord4J connects with every non-privileged intent unless you choose others. The
!pingexample in Discord4J's README reads message text, which needs the privileged Message Content intent: switch it on in the Developer Portal, request it in code withDiscordClient.create(token).gateway().setEnabledIntents(IntentSet.nonPrivileged().or(IntentSet.of(Intent.MESSAGE_CONTENT))), and tick My code uses this for it on our Permissions step. Without it, Discord sends the bot empty message text, except in direct messages and messages that mention the bot. A slash command, as above, needs none of this. See Fix "Used disallowed intents" on your Discord bot. - Logs: Discord4J logs through Reactor, which uses SLF4J when SLF4J is in the jar, and Discord4J brings SLF4J with it. With no logging library behind it, as in the
pom.xmlabove, SLF4J prints three warning lines at start, the last oneSLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details., and then throws Discord4J's own log lines away, errors included. YourSystem.outlines still show in Console. To see Discord4J's lines, add a logging library; Discord4J's docs recommend Logback. - Keep
main()waiting. Discord4J is reactive: its login is aMonothat connects when something subscribes to it. The examples in its README endmain()withgateway.onDisconnect().block()or withblock()onwithGateway(...), which wait while the bot is connected. The bot above useswithGateway(...).block(). - Registering commands on every start is fine. Discord treats creating a command with a name your application already uses as an update of that command, so the bot above does not pile up copies of
/ping. - Crashes: when the bot exits, as the bot above does on a refused token, the panel starts it again right away. If it stops again within a minute of that, it stays stopped and Console says why.
Host it yourself on a Linux VPS
A VPS needs only a Java runtime, because you build the jar on your own computer. On Debian 13 or Ubuntu 24.04 install Java 21; Debian 12 ships Java 17, which also runs the jar above:
sudo apt update
sudo apt install openjdk-21-jre-headless
java -version
Make a user for the bot and copy the jar to the server (for example with scp), then into its folder:
sudo useradd --system --create-home --home-dir /opt/mybot mybot
sudo cp bot.jar /opt/mybot/
sudo chown -R mybot: /opt/mybot
Keep the token out of your code, in a file only root can read:
sudo sh -c 'echo "DISCORD_TOKEN=your-token" > /etc/mybot.env'
sudo chmod 600 /etc/mybot.env
Create /etc/systemd/system/mybot.service:
[Unit]
Description=Discord4J bot
Wants=network-online.target
After=network-online.target
[Service]
User=mybot
WorkingDirectory=/opt/mybot
EnvironmentFile=/etc/mybot.env
ExecStart=/usr/bin/java -XX:MaxRAMPercentage=80.0 -jar /opt/mybot/bot.jar
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
Start it now and at every boot, then follow its output:
sudo systemctl daemon-reload
sudo systemctl enable --now mybot
sudo journalctl -u mybot -f
Restart=always starts the bot again 5 seconds after it exits or crashes, but not after you stop it with systemctl stop. On a VPS, -XX:MaxRAMPercentage is a share of the whole machine's memory, so lower it if other programs run there too. To update, copy the new jar over the old one and run sudo systemctl restart mybot.
Self-hosting or a bot plan
On a VPS you manage yourself:
- Operating system updates and security, and Java upgrades.
- Copying each new jar, the service file, the token file and its permissions.
- Reading
journalctlto find out why the bot stopped. - Backups of anything your bot stores.
In return a VPS runs anything you install on it.
On a PlayNHost bot plan the panel does this for you:
- Java 17, 21 or 25 from a picker, with the heap limit and garbage collector set.
- The token, checked with Discord before it is saved, stored encrypted and given to your code as
DISCORD_TOKEN. - An invite link with the permissions you pick, and a Privileged intents card that checks which ones are switched on in your Developer Portal.
- Starting the bot again after a crash, and a banner above Console when it runs out of memory.
- Upload from the browser, and backups from the panel.
Questions people ask
My Discord4J bot is online but !ping does nothing. Why?
Because the message text is empty. Discord4J requests only non-privileged intents by default, and reading message text in servers needs Message Content, a privileged intent. Turn it on in the Developer Portal and request it with setEnabledIntents as shown above, or use a slash command like /ping in the bot on this page, which needs no privileged intent.
Discord4J or JDA on PlayNHost?
Both run on the same Java bot type, from the same kind of jar. The table above has the memory we measured for each. The real difference is the style: Discord4J is built on Reactor, so you chain Mono and Flux, while JDA uses listeners and callbacks. Choose the one whose code you find easier to read. JDA steps: How to host a Java Discord bot.
Which Java version should I pick for Discord4J?
Java 21, the recommended one, unless your own code needs 25. Discord4J itself works from Java 8 up, so the limit is the release you build with: a jar built for a newer Java than the server runs stops with UnsupportedClassVersionError. Pick a newer Runtime, or build with a lower release.
Why is my Discord4J jar so large?
Because it holds everything: Discord4J brings Reactor, Netty, Jackson and its own voice modules, and the jar-with-dependencies build packs them all in. The jar in our test was 37 MB. That is disk space on your plan; the memory the running bot used is in the table above.
Ready to run your Discord4J bot around the clock? See the plans on Discord bot hosting.



