简体   繁体   English

Sikuli python脚本中的jar或exe文件

[英]jar or exe file from Sikuli python script

I just started doing some scripts with Sikuli in Python. 我刚刚开始用Python用Sikuli做一些脚本。

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). 我想知道是否有可能(如果是,如何)从我的python脚本(文件.py和images)创建jar或exe文件。

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. 我想从没有安装sikuli的其他计算机上轻松运行我的程序(例如java -jar my_script.jarmy_script.exe )或将该实用程序提供给某些同事,但我不希望他们看到源代码码。

There are many ways to convert a .py to .exe and excetra 有多种方法可以将.py转换为.exe和exetra

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). 虽然我相信可以更改扩展名,但如果您使用python扩展名,则过程会更加轻松,顺畅,例如Py2exe可以通过简单的Google搜索找到下载的文件(我也提供了指向它们的链接网站)。 The program will allow you to convert the program into a .exe and will run without needing a python installation. 该程序将允许您将程序转换为.exe,并且无需安装python就可以运行。

Here is a list of reputable and good options: http://www.freehackers.org/Packaging_a_python_program 这是一个信誉良好的选择列表: 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 ) 我个人会推荐Py2exe( http://www.py2exe.org/ ),有关Py2exe的教程可以在此处找到( http://www.py2exe.org/index.cgi/Tutorial

Use a jar file, as Sikuli is written in Java and uses Jython . 使用jar文件,就像Sikuli用Java编写并使用Jython一样 You need to do three things in order for this to work. 您需要做三件事才能使它起作用。

Get Sikuli jar: 获取Sikuli罐子:

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). 首先获取sikuli-java.jar文件( 此处是分步说明 ),您可以通过下载sikuli-setup.jar来获取,然后使用选项4(仅Java)和6(与所有OS兼容)运行它。 This should download the sikuli-java.jar file. 这应该下载sikuli-java.jar文件。

Distribute your script itself as a jar: 以jar形式分发脚本本身:

Obfuscated Version: 混淆版本:

You can compile your Python using Jython ( full instructions here ): 您可以使用Jython编译Python( 此处有完整说明 ):

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

where python_src is the directory containing all your .py files. 其中python_src是包含所有.py文件的目录。

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). 然后,您可以使用包装程序类从Java调用这些编译文件(看起来像myFile$py.class )(有关示例,请参见完整说明)。 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. 该包装器调用了一个Python方法,因此您可以将该方法作为开始其他程序的主要Python方法。

Compile the wrapper class: 编译包装器类:

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

Then jar all your .class files : 然后jar所有.class文件

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

where <packageName> is the package for your wrapper TestJ.java class. 其中, <packageName>是包装器TestJ.java类的软件包。

You'll need to add Jython to this jar as well. 您还需要将Jython添加到此jar中。 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. 只需按照下面“ jar中的Python .py文件”部分的前几个步骤进行操作,或下载独立的Jython jar,并在下面的“合并jars”部分sikuli-java.jar其包含为sikuli-java.jar

Python .py files in jar: jar中的Python .py文件:

Now you have to create the jar for your script itself. 现在,您必须为脚本本身创建jar。 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 ). 以安装Jython时获得的jython.jar文件并将Jython Lib目录压缩到其中,然后压缩.py文件,然后添加具有启动逻辑的__run__.py文件(Jython对该文件进行了特殊处理)并且将是您使用java -jar调用jar时执行的文件。

There is also a more general guide from python.org if you prefer. 如果愿意,还可以从python.org获得更通用的指南

Note: 注意:
The full instructions above assume you have the zip program. 上面的完整说明假定您具有zip程序。 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). 如果不这样做,则可以使用jar uvf jythonlib.jar Lib代替zip -r jythonlib.jar Lib (在使用zip任何地方都可以执行类似操作)。 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 . 您可以使用jar来简单地将sikuli-java.jar添加到包含脚本的jar上面的注释所述 You can also combine the two jar files (also using jar ) if you prefer. 如果愿意,还可以组合两个jar文件 (也可以使用jar )。

Either way, this should give you a single jar file that can run your Sikuli program across any system with a compatible JVM. 无论哪种方式,这都应该为您提供一个jar文件,该文件可以在具有兼容JVM的任何系统上运行Sikuli程序。 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. 如果您需要处理命令行参数,或者通常需要更多详细信息,请参见堆栈溢出指南完整的Python指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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