relaunch Jar With More Memory - Java File Path IO

Java examples for File Path IO:Jar File

Description

relaunch Jar With More Memory

Demo Code


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.UIManager;

public class Main{
    public static void main(String[] argv) throws Exception{
        relaunchWithMoreMemory();//from  w  w  w  .j  a  va 2  s .  c o  m
    }
    public static void relaunchWithMoreMemory() throws IOException {
        Runtime runtime = Runtime.getRuntime();
        String jarName;

        try {
            jarName = getJarName();

            int grantedGigaBytes = 50;

            if (runtime.maxMemory() < 5 * Math.pow(10, 9)) {
                runtime.exec("java -Xmx" + grantedGigaBytes + "G -jar \""
                        + jarName + "\"");

                System.exit(0);
            }
        } catch (FileNotFoundException noJar) {

        }
    }
    public static String getJarName() throws FileNotFoundException {
        Class<PointerSearcherGui> theClass = PointerSearcherGui.class;

        String path = theClass.getResource(
                theClass.getSimpleName() + ".class").getFile();
        if (path.startsWith("/")) {
            throw new FileNotFoundException("This is not a jar file: \n"
                    + path);
        }

        path = ClassLoader.getSystemClassLoader().getResource(path)
                .getFile();

        return new File(path.substring(0, path.lastIndexOf('!'))).getName()
                .replaceAll("%20", " ");
    }
}

Related Tutorials