-2

I have a Java program which required -Xmx1g memory. I am using Netbeans IDE for application developing. I can set this parameter from the IDE,

Right click on the project --> select 'set configuration' --> select 'customize' --> set 'VM option' to -Xmx1g

After setting Xmx value, programe works without any error.

But when I try to run the .jar file from the command prompt, it gives me OutOfMemory error. So I want to set this value in my Java code (e.g.: somewhere in the main method) so I can run the .jar file from the command prompt.

How do I do that?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Roshanck
  • 2,220
  • 8
  • 41
  • 56
  • just use the command line parameters, thts all the configuration settings are doing in the background. – Jon Taylor Aug 12 '12 at 11:42
  • @JonTaylor can he call command line from java code and pass to it what ever ?! – shareef Aug 12 '12 at 11:43
  • no you add the parameters to the command which runs your jar file. – Jon Taylor Aug 12 '12 at 11:45
  • @shareef can you please tell me how to do this 'call command line from java code and pass to it' – Roshanck Aug 12 '12 at 11:47
  • @Jon what is the command line parameter? How do I use it in command line? – Roshanck Aug 12 '12 at 11:47
  • its exactly the same commands you use in the configuration, instead of just calling java -jar with the jar file you do this but add in the parameters you added in the IDE in configuration settings. – Jon Taylor Aug 12 '12 at 11:48
  • http://stackoverflow.com/questions/2848346/how-do-i-set-maximal-jvm-memory-xmx-for-a-jar-file – dacwe Aug 12 '12 at 11:59
  • If the app. has a GUI, launch it using [Java Web Start](http://stackoverflow.com/tags/java-web-start/info). Memory requirements can be set in the launch file. – Andrew Thompson Aug 12 '12 at 21:14

2 Answers2

2

As far as i know, you can't do that in your java code as the jvm is already created when main get executed. But you still can use xmx switch from command line eg java -xmx1024m -jar (check the syntax, i did not).

0

Here's the actual syntax, and I have tested it too.

java -jar -Xmx1024m [jar_file] [args]

San
  • 5,051
  • 3
  • 16
  • 12