How to set the -Xmx when start running a jar file?

后端 未结 5 1923
青春惊慌失措
青春惊慌失措 2020-12-13 12:36

As we know that we can set -Xmx1024M in window->preferences->java->installed jres->edit->default vm arguments in eclipse

5条回答
  •  自闭症患者
    2020-12-13 13:30

    user1361991 > I like your evil option but I cannot comment it since I am still a newbie here. Anyway, I thought it deserved a little enhancement since I find it lacks the stderr and stdout redirection.

    String currentPath= MyClass.class
        .getProtectionDomain()
        .getCodeSource().getLocation()
        .toURI().getPath()
        .replace('/', File.separatorChar).substring(1) ;
    if ( args.length == 0 && Runtime.getRuntime().maxMemory()<512*1024*1024) {
        Process p= Runtime.getRuntime().exec("java -jar -Xmx512M " + currentPath + " restart") ;
        new StreamGobbler(p.getInputStream()).start() ;
        new StreamGobbler(p.getErrorStream()).start() ;
        p.waitFor() ;
        return ;
    }
    

    and the StreamGobbler source (probably retrieved from somewhere on the Internet to be honest and modified a little, I can't remember):

    public class StreamGobbler
        extends Thread
    {
        public StreamGobbler( InputStream is )
        {
            this(is, System.out) ;
        }
    
        public StreamGobbler( InputStream is, PrintStream ps )
        {
            this.is= is ;
            this.ps= ps ;
        }
        private final InputStream is ;
        private final PrintStream ps ;
    
        @Override
        public void run()
        {
            try {
                InputStreamReader isr= new InputStreamReader(is) ;
                BufferedReader br= new BufferedReader(isr) ;
                for ( String line ; (line= br.readLine()) != null ; ) {
                    ps.println(line) ;
                }
           }
           catch ( IOException ioe ) {
               ioe.printStackTrace() ;
           }
        }
    }
    

提交回复
热议问题