Tuesday, October 25, 2005

a simple jvm launcher

ever wanted to have more memory available with jvm, but didn't want to write platform dependent shell script or .bat files? here is a sampler, it spawns a new vm with more memory from the class file, which can be packed as a jar (you need jdk 1.5 for this code):

import javax.swing.*;

public class Hello {
public static void main(String [] args) {
if (args.length == 0) {
try {
System.out.println("I now have : " + Runtime.getRuntime().maxMemory());
Process p = new ProcessBuilder("java", "-Xmx256m", "Hello", "memory").start();
} catch(Exception e) {
e.printStackTrace();
}
} else {
JFrame f = new JFrame("Hi! I have more memory!: " + Runtime.getRuntime().maxMemory());
f.setVisible(true);
} // end if
}
}

No comments: