Question

I have no idea if I can just run this code and it will work but here:

public void actionPerformed(ActionEvent e) {
        try
        {
        Runtime rt = Runtime.getRuntime();
        Process p = rt.exec("c:\\Users\\Toby\\AppData\\Roaming\\.minecraft\\minecraft.exe");
        }

        catch(Exception a)
        {

        }
    }

This is linked to a button, and it launches the minecraft launcher. If I want to give it to my friends, what do I have to do so that it doesn't look for the user 'Toby', but instead looks for their home folder? Sorry if its confusing!

Was it helpful?

Solution

I think you're looking for the user.home property. There's a list of properties available here: http://www.mindspring.com/~mgrand/java-system-properties.htm

So, your code would be changed to:

Process p = rt.exec(System.getProperty("user.home") + \\AppData\\Roaming\\.minecraft\\minecraft.exe");

OTHER TIPS

If you want to keep consistency with other version of Windows, I would suggest using the System.getenv("APPDATA") instead of adding to System.getProperty("user.home") because Roaming folder is not present in older version of Windows

So your code would change to something like this:

Process p = rt.exec(System.getenv("APPDATA") + ".minecraft\\minecraft.exe")

Try System.getProperty("user.home")

String mhome = System.getProperty("user.home"))+"";

    public void actionPerformed(ActionEvent e) {
            try
            {
            Runtime rt = Runtime.getRuntime();
            Process p = rt.exec(mhome+"\\AppData\\Roaming\\.minecraft\\minecraft.exe");
            }

            catch(Exception a)
            {

            }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top