pythonjarexesikulisikuli-script

jar or exe file from Sikuli python script


I just started doing some scripts with Sikuli in Python.

I would like to know if it's possible (and if yes, how) to create a jar or exe file from my python script (file .py and images).

I would like to easily run my program from other machines that do not have sikuli install (e.g., java -jar my_script.jar or my_script.exe) or give the utility to some colleagues, but I don't want that they see the source code.


Solution

  • Use a jar file, as Sikuli is written in Java and uses Jython. You need to do three things in order for this to work.

    Get Sikuli jar:

    First get the sikuli-java.jar file (step-by-step instructions here), which you can get by downloading sikuli-setup.jar, and run it using options 4 (only Java) and 6 (all OS compatible). This should download the sikuli-java.jar file.

    Distribute your script itself as a jar:

    Obfuscated Version:

    You can compile your Python using Jython (full instructions here):

    $JYTHON_HOME/jython $JYTHON_HOME/Lib/compileall.py python_src/
    

    where python_src is the directory containing all your .py files.

    Then you can call these compiled files (which look like myFile$py.class) from Java, using a wrapper class (see full instructions for an example). This wrapper calls a single Python method, so you can just make that method be your main Python method that kicks off the rest of your program.

    Compile the wrapper class:

    javac -cp $JYTHON_HOME/jython.jar TestJ.java
    

    Then jar all your .class files:

    jar cvfe myapp.jar <packageName>.TestJ *.class
    

    where <packageName> is the package for your wrapper TestJ.java class.

    You'll need to add Jython to this jar as well. Just follow the first few steps of the "Python .py files in jar" section below, or download the standalone Jython jar and include it the same way as sikuli-java.jar in the "Combine the jars" section below.

    Python .py files in jar:

    Now you have to create the jar for your script itself. The full instructions are here, but if you want a summary:

    Take the jython.jar file that you get when you install Jython and zip the Jython Lib directory into it, then zip your .py files in, and then add a __run__.py file with your startup logic (this file is treated specially by Jython and will be the file executed when you call the jar with java -jar).

    There is also a more general guide from python.org if you prefer.

    Note:
    The full instructions above assume you have the zip program. If you don't, you can use jar uvf jythonlib.jar Lib instead of zip -r jythonlib.jar Lib (and do similar wherever zip is used). See here for more

    Combine the jars

    You can simply add sikuli-java.jar to the jar containing your scripts using jar as mentioned in the note above. You can also combine the two jar files (also using jar) if you prefer.

    Either way, this should give you a single jar file that can run your Sikuli program across any system with a compatible JVM. Run it using

    java -jar myapp.jar
    

    Congratulations, you're done!

    See the stack overflow guide or the full Python guide if you need to deal with command line arguments, or generally need more detail.