Minecraft and Gnome Shell

Sunday, September 30. 2012

Minecraft and Gnome Shell

If you are running Minecraft on Linux with Gnome 3 then you might have noticed that it doesn't integrate well into Gnome Shell as you can see in these screenshots:

Java class name instead of real application name No Dash menu

So instead of having a nice application name like Minecraft Gnome Shell displays the Java class name. This is not really a problem of Minecraft but more a problem of bad Linux integration of Java applications in general. In Gnome 2 and other desktop environments this application name wasn't really important but since Gnome 3 it is crucial to have a valid application name because the Dash (The dock of Gnome Shell) uses it to connect the running application with its application launcher. And that's another problem: Minecraft doesn't create an application launcher. It only provides a JAR file you have to run somehow. And because this launcher doesn't exist and it can't be connected to the running application because of the bad application name you don't get the usual menu in the Dash to add Minecraft to your favorites.

All this can't be tolerated and must be fixed. In this article I will explain how.

Properly installing Minecraft

If you haven't already done something to improve the Minecraft situation on Linux a little bit then you most likely still have the minecraft.jar in your Downloads folder and you always start Minecraft on the console with the fancy command mentioned on the Minecraft website. This is the first thing we have to clean up.

Minecraft stores all other data in ~/.minecraft so simply move the minecraft.jar file into this folder, too, to give it a proper home:

mkdir -p ~/.minecraft
mv ~/Downloads/minecraft.jar ~/.minecraft/

Now create the launcher script:

mkdir -p ~/bin
echo -e '#!/bin/sh\nexec java -Xmx1024M -Xms512M -jar ~/.minecraft/minecraft.jar "$@"' > ~/bin/minecraft
chmod +x ~/bin/minecraft

If the ~/bin directory wasn't there before then you need to logout and login again so Linux has it included in your PATH environment variable. At least this is the case for Debian-based Linux distributions.

Test the script by pressing Alt+F2 and typing minecraft.

Create application icon

For the next step (Creating the application launcher) we need an icon placed in ~/.local/share/icons (Directory may need to be created first). If you like the low resolution icon which is included in the minecraft.jar then run these commands to extract it:

mkdir -p ~/.local/share/icons
unzip -o -j -d ~/.local/share/icons ~/.minecraft/minecraft.jar net/minecraft/favicon.png
mv ~/.local/share/icons/favicon.png ~/.local/share/icons/minecraft.png

If you prefer a high resolution icon then search for a nice one and store it as ~/.local/share/icons/minecraft.png.

Create application launcher

Now we are ready for the application launcher. Start up your text editor to create one:

mkdir -p ~/.local/share/applications
gedit ~/.local/share/applications/minecraft.desktop

Enter the following content and save the file:

[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
StartupNotify=true
Name=Minecraft
Icon=minecraft.png
Exec=minecraft
Categories=Game

Test the launcher by opening the Gnome Shell activities and typing minecraft. The launcher should now pop up with the proper icon and the launcher should also appear in the Games folder of the applications menu. Click on the launcher to test if it works.

Fixing the application name

Now there is one last thing to do: We need to fix the application name of Minecraft so the launcher can be pinned to the Dash. We need to compile a small Java program for this so first we need a Java compiler:

gksudo apt-get install openjdk-7-jdk

After this create the following Java source code and save it as ~/.minecraft/MinecraftLauncher.java:

import java.awt.Toolkit;
import java.lang.reflect.Field;

public class MinecraftLauncher
{
    /**
     * Sets the application name. There is no API for this (See
     * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6528430) so this
     * method uses reflection to do this. This may fail if the Java
     * implementation is changed but any exception here will be ignored.
     * 
     * The application name is currently only used for X11 desktops and only
     * important for some window managers like Gnome Shell.
     * 
     * @param appName
     *            The application name to set.
     */
    public static void setAppName(final String appName)
    {
        final Toolkit toolkit = Toolkit.getDefaultToolkit();
        final Class<?> cls = toolkit.getClass();

        try
        {
            // When X11 toolkit is used then set the awtAppClassName field
            if (cls.getName().equals("sun.awt.X11.XToolkit"))
            {
                final Field field = cls.getDeclaredField("awtAppClassName");
                field.setAccessible(true);
                field.set(toolkit, appName);
            }
        }
        catch (final Exception e)
        {
            // If this fails for whatever reason then we silently ignore it 
            // because a running game is more important than a correct 
            // application name
        }
    }
    
    public static void main(String[] args) throws Exception
    {
        setAppName("Minecraft");
        net.minecraft.MinecraftLauncher.main(args);
    }
}

Compile the Java program with these commands:

cd ~/.minecraft
javac -cp minecraft.jar MinecraftLauncher.java

When compilation was successful then you'll now have a MinecraftLauncher.class file in the Minecraft folder. Last step is changing the launcher script to use this launcher class instead of the original one. So open ~/bin/minecraft in your favourite editor and change it so it looks like this:

#!/bin/sh
cd ~/.minecraft
exec java -Xmx1024M -Xms512M -cp .:minecraft.jar MinecraftLauncher "$@"

When you now start Minecraft then you'll have a correct application name and the game can also be pinned to the Dash:

Proper application name Proper Dash menu

Posted in Gaming | Comment (1)
navhaxs at 2013-06-29 09:08
Thanks for the great post - Minecraft and Gnome 3? Awesome! Also, just wondering why you didn't just simply post the compiled java code to fix the app title?

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.