4

I am trying to create a jar file that will execute my game with just a click. My game exceeds java's default allocated heap memory so I have to run my code with -Xmx1000m. I've been researching online and unfortunately there doesn't seem to be a way to tell a jar file to fun my code with more than the default memory. Instead I created another class that will use runtime to compile my code from within another main method and created a jar file using this:

import java.io.*;
public class RuntimeExec{
public static void main(String[] args){
    try
    {
    Process process = Runtime.getRuntime().exec("java -Xmx1000m Controller");
    process.waitFor();
    int exitCode = process.exitValue();
    if(exitCode == 0) { /* success*/ }
    else { /*failed*/ }
    }
    catch (Exception e)
    {e.printStackTrace();}
}

}

This works however I think it only works because it runs my existing class in the folder and not the one I stored in the jar. Is there a way so that the jar will run a class within in or combine two different jars that will allow me to get around the memory heap problem?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433

2 Answers2

0

There are many ways of doing it:

  1. As you mentioned, by having another Jar file which triggers your game file
  2. As @Perception mentioned, have a batch file which will start your game. But be careful, if downloaded from say Net, the user will have to set permissions for the script to be runnable
  3. Build an installer. On Mac, using the Oracle Java App bundler for Java 7, Apple App bundler for Java 6 build the .app file. You still cant redistribute it as the necessary permissions wont be set. Build a dmg for the app file. This can be used for distribution. A similar installer for Windows

The third technique would be the best, as you can then package the dependencies well, set all JVM arguments etc

Jatin
  • 31,116
  • 15
  • 98
  • 163
0

The entire solution to providing an easy install for users is to deploy the app. using Java Web Start. It can set RAM for an app., and also install a desktop shortcut to launch it. JWS is much more robust than a (poorly implemented) call to exec.


However if JWS is not for some reason suitable for this app., see IWantToBeBig for a hack that will cause a Jar to have enough memory (similar to how you use exec above, but slightly more robust in using ProcessBuilder to relaunch the app. that does not have enough memory).

Organizing the desktop shortcut to allow the user to launch it with a click, is left as an exercise for the reader.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433