集册 Java实例教程 用更多内存重新启动Jar

用更多内存重新启动Jar

欢马劈雪     最近更新时间:2020-01-02 10:19:05

458
重新启动具有更多内存的Jar
/*时代Java公众号 - N o w J a  v a . c o m 提 供*/

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();

    }

    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)) {/*n  o  w  j  a  v  a . c o m*/

                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"

                    + 
展开阅读全文