views:

2498

answers:

4

I am launching a java jar file which often requires more than the default 64MB max heap size. A 256MB heap size is sufficient for this app though. Is there anyway to specify (in the manifest maybe?) to always use a 256MB max heap size when launching the jar? (More specific details below, if needed.)


This is a command-line app that I've written in Java, which does some image manipulation. On high-res images (about 12 megapixels and above, which is not uncommon) I get an OutOfMemoryError.

Currently I'm launching the app from a jar file, i.e.

java -jar MyApp.jar params...

I can avoid an OutOfMemoryError by specifying 256MB max heap size on the command line, i.e.:

java -Xmx256m -jar MyApp.jar params...

However, I don't want to have to specify this, since I know that 256MB is sufficient even for high-res images. I'd like to have that information saved in the jar file. Is that possible?

+2  A: 

Found an answer on google.

He says no for the JAR file, yes in JavaWeb Start, and that you should do it in your (possibly system-specific) launcher/wrapper script/app.

streetpc
A: 

This isn't great, but you could have it as an executable JAR, and then in it's main, have it execute itself via the command-line as a non-daemon thread with the proper params stored in a properties file or calculated or whatever, then exit the original. You could even have it execute the jar with another "real" entry-point that expects those parameters.

Alex Beardsley
+4  A: 

you could also use a wrapper like launch4j which will make an executable for most OS:es and allows you to specify VM options.

willcodejavaforfood
i like this, and it would get rid of the "java -jar" part of the command line too.
Kip
actually, it isn't quite as great as i thought. i don't have a mac, but i'd like to build a mac executable wrapper for my jar file from my windows machine. it looks like this app doesn't quite do that...
Kip
I use jarbundler for my mac apps
willcodejavaforfood
but you might need a mac to do that
willcodejavaforfood
A: 

YES! You need to write a small starter program - I wrote a small mostly cross platform compatible (paths should be fine) program to do it - see below. This has been tested on Windows/Ubuntu/Mac OS X.

Silent Development Blog article on increasing executable Java jar heap

Ben