从java代码设置JRE变量

时间:2012-06-27 18:09:06

标签: java

我们需要手动设置-Xmax和-Xmin环境变量,但我想从java代码设置这些变量。有没有办法用java代码设置这些变量。感谢。

2 个答案:

答案 0 :(得分:3)

您是否需要从正在运行的Java进程中设置这些设置?我不相信这是可能的。但是,您可以从调用Java程序在子进程上设置它们,例如通过ProcessBuilder

ProcessBuilder pb = new ProcessBuilder("myCommand", "-Xmax", value);
pb.directory(new File("dir"));
Process p = pb.start();

答案 1 :(得分:0)

我得到了这个问题的解决方案: -

     public static void main(String[] args) {
     Properties prop = new Properties();
     try {
        String path=System.getenv("APPDATA");
          path=path.substring(0,path.lastIndexOf('\\')+1)+"LocalLow\\Sun\\Java\\Deployment\\deployment.properties";
            System.err.println(path);

            prop.load(new FileInputStream(path));
            String jreIndex = null; 
            Enumeration enuKeys = prop.keys();
            while (enuKeys.hasMoreElements()) {
                String key = (String) enuKeys.nextElement();
                String value = prop.getProperty(key);
                if(value.contains("1.6.0_26")){
                    String[] st2 = key.split("\\.");  
                    if(st2.length==5){
                        jreIndex = st2[3];
                    }
                }
            }
            //SystemSettings SysConfigsettings = new SystemSettings();
            if(jreIndex != null){
            //  if (SysConfigsettings.getValue(SystemSettings.JRE_HEAP_SIZE))
              {
                prop.setProperty("deployment.javaws.jre."+jreIndex+".args", "-Xmx1024m");
              }
            }
            prop.store(new FileOutputStream(path), "UpdatedHeapSize");
            System.out.println("First : "+prop.getProperty("deployment.javaws.jre.0.args"));
    } catch (Exception e) {
        e.printStackTrace();
    }

}