Software & AppsOperating SystemLinux

How To Fix “Could not find or load main class” Error in Java/Minecraft Server on Ubuntu

Ubuntu 21

In this article, we will address a common issue faced by many Java/Minecraft server administrators on Ubuntu: the “Could not find or load main class” error. This error often arises due to a simple typographical mistake in the command line, and we will guide you through the process of fixing it.

Quick Answer

To fix the "Could not find or load main class" error in Java/Minecraft Server on Ubuntu, ensure that you are using a minus sign instead of a hyphen in the command line. Replace the hyphen with a minus sign and run the command again. Additionally, make sure to specify the initial and maximum heap size for the Java Virtual Machine (JVM) using the -Xms and -Xmx options respectively. Adjust these values according to your system’s RAM limits.

Understanding the Error

The error “Could not find or load main class –Xms1024M” typically occurs because a hyphen – is used instead of a minus - in the command. Java interprets –Xms1024M as the name of the class or file, rather than an option.

Correcting the Command

To fix this, replace the hyphen with a minus sign and run the command again:

sudo java -Xms1024M -Xmx1024M -jar minecraft_server.1.9.2.jar nogui

In this command, -Xms1024M and -Xmx1024M are options that set the initial and maximum heap size for the Java Virtual Machine (JVM) respectively. The values are set to 1024MB, but you can adjust these according to your system’s RAM limits. The -jar option is used to specify the JAR file that contains the application to be run, which in this case is minecraft_server.1.9.2.jar. The nogui option is used to run the server without a GUI.

Specifying RAM for the Minecraft Server

Not specifying the RAM can cause your Minecraft server to crash if it doesn’t have enough reserved RAM. Adjust the -Xms (initial heap size) and -Xmx (maximum heap size) values according to your system’s RAM limits.

Running the Minecraft Server in the Background

There are several methods to start the Minecraft server via SSH and let it run in the background after disconnecting:

  1. Using screen: Run the command screen followed by your start command. Press Ctrl + A followed by D to detach from the screen session. To resume the session, run screen -r.
  2. Using bg/fg: After starting the Minecraft server, press Ctrl + Z to suspend the process. Then type bg to run it in the background. To bring it back to the foreground, use the fg command.
  3. Using nohup: Prepend your start command with nohup. After starting the server, you can close your SSH session, and the server will continue running. To prevent excessive log file creation, append >/dev/null 2>&1 to the end of the command.
  4. Using initscripts: Initscripts allow your server to auto-start/stop on boot or manual invocation. However, setting up initscripts can be complex and system-specific. Search online for instructions tailored to your system.

Fixing Permission Errors

Regarding the permission errors you encountered, it seems that Minecraft created files with root ownership, preventing access. To fix this, run the command sudo chown -R $USER:$USER . in the Minecraft folder to change ownership to your user.

In conclusion, the “Could not find or load main class” error in Java/Minecraft Server on Ubuntu can be easily fixed by ensuring the correct use of command line options and properly setting up the server. We hope this guide has been helpful in resolving this issue.

What is the difference between a hyphen and a minus sign in the command line?

In the command line, a hyphen (-) is used to indicate command line options or arguments, while a minus sign (–) is not recognized as a valid character and can cause errors.

How do I adjust the RAM settings for my Minecraft server?

To adjust the RAM settings for your Minecraft server, you need to modify the -Xms (initial heap size) and -Xmx (maximum heap size) values in the command. These values determine the amount of RAM allocated to the server. Make sure to set them according to your system’s RAM limits.

How can I run the Minecraft server in the background after disconnecting from SSH?

There are several methods to run the Minecraft server in the background after disconnecting from SSH. You can use tools like screen or nohup to achieve this. Another option is to suspend the process using Ctrl + Z and then run it in the background using the bg command. Each method has its advantages and disadvantages, so choose the one that suits your needs best.

How can I fix permission errors in the Minecraft folder?

If you encounter permission errors in the Minecraft folder, you can fix them by running the command sudo chown -R $USER:$USER . in the Minecraft folder. This command changes the ownership of the files and folders in the Minecraft directory to your user, allowing you to access and modify them.

How can I set up initscripts for my Minecraft server?

Setting up initscripts for your Minecraft server can be complex and system-specific. It involves creating a custom script that specifies how the server should start, stop, and interact with the system’s init system. Detailed instructions for setting up initscripts can be found online, tailored to your specific operating system and version.

Leave a Comment

Your email address will not be published. Required fields are marked *