Default JVM arguments for a particular jar?

Quick question: Is there a way to make my executable jar file run with certain jvm arguments by default? I don’t mean by tweaking my current vm, I mean having the arguments be there by default for a specific jar. That way, I could maybe run the concurrent mark and sweep algorithm for my game jar, and not some other jar. Haven’t been able to find anything in a google search. I know Minecraft lets you do this, but Minecraft first opens a launcher before firing up the jvm, and the launcher is a native executable.

Specifically, I want to pass -XX:+UseConcMarkSweepGC by default.

You can’t. :emo:

If making an exe is not your thing, create a batch file instead.
(and on Windows, turn that into an exe with bat-to-exe)

If you really want a double-clickable jar (bad idea, but whatyagonnado), you can spawn a new java process with the proper VM args.

Shameless self-promotion: I just released a tool called SvgExe that supports exactly this.

Edit: Here is the JGO announcement thread.

This does what I want, as it turns out.

package main;
import java.io.IOException;
import javax.swing.JOptionPane;


/**
 * Class that fires an external jar
 * @author William Andrew Cahill
 */
public class Main2
{
	/**
	 * Main method that fires an external jar
	 * @param args Derp :)
	 */
	public static void main(String[] args)
	{
		try
		{
			Runtime.getRuntime().exec("java -jar -XX:+UseConcMarkSweepGC jarlie.jar");
		}
		catch (IOException e)
		{
			JOptionPane.showMessageDialog(null, "Oh noes!");
		}
	}
}

Edit: I run a jar that runs another jar, in case this isn’t obvious. No batch files for me!

That’ll work, until the user renames the jar! :stuck_out_tongue:

Users are weirdos. Why would they want to do that? Actually, I just thought of something… Are the commands I typed windows specific?

I don’t think anything you have above is Windows-specific.

And there is a way to get the name of the jar being executed, if that’s the way you want to go.

Oh! You’re talking about programming users. This was just an example! No need to do anything too fancy. Just replace “jarlie.jar” with something more… suitable…

No I meant your end users, you know how misbehaved they can be!

This is what I use:

File jarFile = new File(SomeClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());
String fullJarPath = jarFile.getAbsolutePath();

Any game will break when users start renaming files. Don’t waste time on it.

Sure, if they rename internal files you’re out of luck. But I’m talking about renaming a single runnable file, which is probably pretty common.

Here’s an example:



import java.io.IOException;
import java.net.URISyntaxException;

public class Main
{
    public static void doMain()
    {
        // Run the application.
        System.out.println("Hello World");
    }
    
    public static void main(String[] args)
    {
        for(String s : args)
        {
            if(s.equals("run"))
            {
                doMain();
                return;
            }
        }
        
        try
        {
            ProcessBuilder pb = new ProcessBuilder();
            pb.command("java", "-jar", Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath(), "run");
            pb.inheritIO();
            pb.start();
            System.exit(0);
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        catch(URISyntaxException e)
        {
            e.printStackTrace();
        }
    }
}

Simply add any commands you want to the ProcessBuilder, move your current main function to doMain(), and you’re set.

Edit: Remember in your IDE to add ‘run’ to the application arguments.

I’ve never heard of anyone doing that, and I’ve been surrounded by idiots for years.

Cas :slight_smile:

I think I’m probably being unclear. I think what the OP is talking about is creating a single runnable .jar file that users can download. I can see a situation where, for example, a new version of the jar is released, and the user downloads it to the same place as the old one. The OS will probably append a (2) or something to the jar, or the jar will contain _v1.2.3 in the name. The user might think that’s ugly and just want to have Game.jar somewhere, so he renames it. I’m not talking about getting into the internal files of the game, just the main shortcut that the user is actually going to use. It seems like a common enough case to worry about, especially since it’s a one line fix!

Although this is all happening in theoretical land and the OP’s code was just an example anyway, so I’m probably just being pedantic.

Relying on double-clicking jars is not wise anyway; I seem to recall it’s been turned off by default (somebody know for sure?) because it’s a bit of a security issue, bypassing all the OS security features for running downloadable executables.

Cas :slight_smile:

Eh, I was just responding to the OP, which I think was talking about runnable jars. Double-clicking jars still works for me on the latest Windows 8. Do you know what OS double-clicking has been deactivated for? If that’s true, that’s a pretty big deal…

I’m mostly just writing this code for myself. I’m not so noble as to write this code to make someone ELSE’s life easier! What kind of guy do you think I am? xD Yes, this is for my own purposes in the event that I want to make a particular game that runs with certain JVM arguments. In my opinion, there should be a file in the jar that lets you specify this instead of doing this crazy indirection. Even worse is having the user fire up a jar from the command line. No everyday user should have to do that anymore!

The code I provided does all the crazy indirection needed, and should run fine from double-clicking.