Correct way to (re)launch a Java application with hardware-dependent VM parameters?
EDIT I don't want to use Java Web Start
I've got a Java application that I'd like to run with different VM parameters depending on the amount of memory the system it is launched on has.
For example if the ma开发者_JS百科chine has 1 GB of memory or less I'd like to pass "-Xmx200m" and "-Xmx400m" if it has 2 GB and "-Xmx800m" if it has 8 GB (these are just examples).
Is there a portable way to do this?
I've tried having a first tiny Java app (hence portable) that determines the amount of memory available and then launches a new Java app but I don't think this is very clean.
As of now I've written Bash shell scripts that invoke the Java app with the correct parameters depending on the config but it only works on Linux on OS X.
What is the correct way to solve this?
Would application packager package ;) help ?
Are you sure you want to do this? In general, Java application need a certain amount of memory, not more, not less.
If you have a machine with little memory and you specify a small number at -Xmx you risk to run out of memory.
If you have a machine with lots of memory and you specify a large number the application won't use all the memory but only the amount required by the objects in use.
So, in general, you don't lose memory by specifying large numbers at -Xmx.
(Exceptions are server applications which have a large amount of object 'throughput' and tend to waste memory.)
What's your gripe with your current approach, exactly? It's completely portable (unlike various scripting solutions... why is a script "cleaner" than 100%-portable bytecode?), and it'll adapt perfectly if the hardware changes after installation (so it's better than capturing the hardware details as part of your installation script).
If you can explain the actual issues it may be easier to come up with a solution to those specific problems.
Here's an alternative that you might like better, actually: copy what Photoshop, GIMP, etc. do and make "resource usage" or "performance" part of the preferences for your app. You'll have to prompt them restart your app when/if they change this setting (to relaunch your app using the modified start procedure, since you can't change the memory limits for a running app... where you write the new flags is deployment-method dependent), but that's not such a disaster for a setting they'll likely change exactly once.
You could also check their hardware on first run, and if your default installed setting is way off from their setup, prompt them to change the setting then.
Since http://ant.apache.org :
- is a java program
- has built-in forking/launch mechanism
- has scripting basics
- is embeddable within another program
- has command line parsing and other basic stuff that deals with environment/config issues.
I suggest that you
- Create a build.xml (or create the targets/tasks programatically)
- and have the your initial program be ant running the build.xml script
To define the various launch configurations I would go with JNLP. xmx can be specified there.
Unfortunately this still leaves you the task of finding out how much memory the system has, for which your tiny Java app sounds best.
In the same line as your existing solution, I use scripts to launch my applications but instead of Bash I use Python. Python ships on most Linux distros as well as OSX. The only difficulty is Windows, but you can suggest Python as a prerequisite similar to getting the JVM installed.
For any 'configuration task' I would suggest to use izpack, really powerfull and free packager. You might also take a look at eclipse, where they have implemented restarting a java application.
Using a bash script instead of a JavaApplicationStub in the application bundle allows for changing the parameters. However, this breaks the handleOpenFile() functionality: the event signal never arrives at the handleOpenFile ApplicationEvent event. See http://lists.apple.com/archives/java-dev/2010/Jun/msg00009.html
精彩评论