I recently received an email asking about how to pass arguments when launching a processing sketch from the command line. I hadn’t ever done such a thing, but after some poking came up with an easy, though not exactly robust, solution. This example sets the value of the r, g, and b variables and uses them to set the background color.

The main method just clones the array of arguments into a “global” variable of the sketch. That variable needs to be declared static so it can be accessed by the main method, which is also static. Also, make sure that the string in PApplet.main(new String[] { "passing_arguments" }); matches the name of your sketch. Next, just parse the array of arguments like you would anything else inside setup. It’s a good idea to make sure that you have any arguments at all (by checking for != null) and that you’ve got the number of arguments you expect.

Next you export the sketch (File > Export) then you can execute the .jar file from the command line using: java -jar passing_arguments.jar 255 255 1 where passing_arguments.jar is the name of the jar file, and 255 255 1 are the values you want to use for the r, g, and b variables.

If you need something more robust, or need to use named switches, check out this article on using the Apache Commons CLI Library.

4 Comments

  1. jan says:

    Thank you very much. I was looking for this for a long time.

    Nice!
    jan from Santander (Spain)

  2. toxi says:

    Hey, there’s also an old Processing hack about this here:

    http://processing.org/hacks/hacks:dynamicsize?s=param

    And I also highly recommend the args4j library by kohsuke, although it’s only suitable for working with Processing outside the PDE (e.g. in Eclipse):

    https://args4j.dev.java.net/

  3. chandler says:

    oh very nice! could be worth moving that param hack into processing proper.

  4. samuel says:

    my problem is solved, love you so much!

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*