उपयोगकर्ता 13619 9 1> मुझे आपका बुरा विकल्प पसंद है लेकिन मैं इसे तब तक टिप्पणी नहीं कर सकता क्योंकि मैं अभी भी एक नौसिखिया हूं। वैसे भी, मैंने सोचा कि यह थोड़ा संवर्धन योग्य है क्योंकि मुझे लगता है कि इसमें कठोर और stdout पुनर्निर्देशन की कमी है।
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 ;
}
और StreamGobbler स्रोत (शायद इंटरनेट पर कहीं से लिया गया ईमानदार होना और एक छोटे से संशोधित, मुझे याद नहीं):
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() ;
}
}
}
जावा जार -Xmx1024M A.jar – Mark