简体   繁体   中英

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 (eg, 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.

There are many ways to convert a .py to .exe and excetra

While I believe it is possible to change the extension, the process would be much easier and smoother if you use an extension to python, like Py2exe the download to which can be found with a simple google search (I have also included a link to their site below). The program will allow you to convert the program into a .exe and will run without needing a python installation.

Here is a list of reputable and good options: http://www.freehackers.org/Packaging_a_python_program

I would personally recommend Py2exe ( http://www.py2exe.org/ ) and the tutorial to Py2exe can be found here ( http://www.py2exe.org/index.cgi/Tutorial )

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM