使用Jython将我的Python脚本作为JAR文件分发吗? [英] Distributing my Python scripts as JAR files with Jython?

查看:86
本文介绍了使用Jython将我的Python脚本作为JAR文件分发吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成为Python程序员近两年了,我习惯于编写一些小的脚本来自动化我在办公室要做的一些重复性任务.现在,显然我的同事注意到了这一点,他们也想要这些脚本.

I have been a Python programmer for almost two years, and I am used to writing small scripts to automate some repetitive tasks I had to do at the office. Now, apparently my colleagues noticed this, and they want those scripts too.

其中有些装有Mac,有些装有Windows.我在Windows上制作的.我调查了使用py2exe甚至py2app来制作我的脚本的本机的可能性,但是他们从未满足于我...

Some of them have Macs, some Windows; I made these on windows. I investigated the possibility of using py2exe or even py2app to make natives of my script, but they never satisfied me...

我知道他们所有人的系统上都有JVM,所以我可以使用Jython之类的东西给他们一个脚本的可执行JAR文件吗?

I came to know that all of them have JVM on their systems, so can I give them one single executable JAR file of my script using something like Jython may be?

这是多么可行...我的意思是,我不知道如何为Jython编写脚本,写这些脚本时我也不在意……它将带来什么样的问题?

How feasible is this... I mean, I had no idea how to write scripts for Jython, neither did I care about it when I wrote them... what kind of problems will it give?

推荐答案

本文在Jython的Wiki上详细介绍了在jar中分发Python文件的最新最佳技术:

The best current techniques for distributing your Python files in a jar are detailed in this article on Jython's wiki: http://wiki.python.org/jython/JythonFaq/DistributingJythonScripts

对于您的情况,我认为您希望获取安装Jython时获得的jython.jar文件并将Jython Lib目录压缩到其中,然后将.py文件压缩到其中,然后添加__run__.py具有启动逻辑的文件(Jython对该文件进行了特殊处理,当您使用"java -jar"调用jar时将执行该文件.)

For your case, I think you would want to 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开发人员)需要提供一个很好的工具来自动执行这些任务,但是现在这是最好的方法.下面,我将在上面文章的底部复制配方(略作修改以适合您的问题描述),以使您对解决方案有所了解.

This process is definitely more complicated then in ought to be, and so we (the Jython developers) need to come up with a nice tool that will automate these tasks, but for now these are the best methods. Below I'm copying the recipe at the bottom of the above article (modified slightly to fit your problem description) to give you a sense of the solution.

创建基本jar:

$ cd $JYTHON_HOME
$ cp jython.jar jythonlib.jar
$ zip -r jythonlib.jar Lib

将其他模块添加到jar中:

Add other modules to the jar:

$ cd $MY_APP_DIRECTORY
$ cp $JYTHON_HOME/jythonlib.jar myapp.jar
$ zip myapp.jar Lib/showobjs.py
# Add path to additional jar file.
$ jar ufm myapp.jar othermanifest.mf

添加__run__.py模块:

# Copy or rename your start-up script, removing the "__name__  == '__main__'" check.
$ cp mymainscript.py __run__.py
# Add your start-up script (__run__.py) to the jar.
$ zip myapp.jar __run__.py
# Add path to main jar to the CLASSPATH environment variable.
$ export CLASSPATH=/path/to/my/app/myapp.jar:$CLASSPATH

在MS Windows上,最后一行设置CLASSPATH环境变量,如下所示:

On MS Windows, that last line, setting the CLASSPATH environment variable, would look something like this:

set CLASSPATH=C:\path\to\my\app\myapp.jar;%CLASSPATH%

或者再次在MS Windows上,使用控制面板"和系统"属性来设置CLASSPATH环境变量.

Or, again on MS Windows, use the Control Panel and the System properties to set the CLASSPATH environment variable.

运行应用程序:

$ java -jar myapp.jar mymainscript.py arg1 arg2

或者,如果您已将启动脚本添加到jar中,请使用以下一种方法:

Or, if you have added your start-up script to the jar, use one of the following:

$ java org.python.util.jython -jar myapp.jar arg1 arg2
$ java -cp myapp.jar org.python.util.jython -jar myapp.jar arg1 arg2
$ java -jar myapp.jar -jar myapp.jar arg1 arg2

double -jar有点烦人,因此,如果要避免这种情况并获得更令人愉悦的话:

The double -jar is kind of annoying, so if you want to avoid that and get the more pleasing:

$ java -jar myapp.jar arg1

您将需要做更多的工作,直到我们在将来的Jython中得到类似的东西为止[更新:JarRunner是Jython 2.5.1的一部分].这是一些Java代码,它们会自动查找__run__.py并运行它.请注意,这是我在这堂课上的第一次尝试.让我知道是否需要改进!

You'll have to do a bit more work until we get something like this into a future Jython [Update: JarRunner is part of Jython 2.5.1]. Here is some Java code that looks for the __run__.py automatically, and runs it. Note that this is my first try at this class. Let me know if it needs improvement!

package org.python.util;

import org.python.core.imp;
import org.python.core.PySystemState;

public class JarRunner {

    public static void run(String[] args) {
        final String runner = "__run__";
        String[] argv = new String[args.length + 1];
        argv[0] = runner;
        System.arraycopy(args, 0, argv, 1, args.length);
        PySystemState.initialize(PySystemState.getBaseProperties(), null, argv);
        imp.load(runner);
    }

    public static void main(String[] args) {
        run(args);
    }
}

我将此代码放入org.python.util包中,因为如果我们决定将其包含在将来的Jython中,它将放在这里.要进行编译,您需要将jython.jar(或myapp.jar)放入类路径中,如下所示:

I put this code into the org.python.util package, since that's where it would go if we decide to include it in a future Jython. To compile it, you'll need to put jython.jar (or your myapp.jar) into the classpath like:

$ javac -classpath myapp.jar org/python/util/JarRunner.java

然后,您需要将JarRunner.class添加到您的jar中(类文件将需要在org/python/util/JarRunner.class中),在"org"目录中调用jar将使整个路径进入您的罐子.

Then you'll need to add JarRunner.class to your jar (the class file will need to be in org/python/util/JarRunner.class) calling jar on the "org" directory will get the whole path into your jar.

$ jar uf org

将此添加到将用于更新清单的文件中,一个好名字是manifest.txt:

Add this to a file that you will use to update the manifest, a good name is manifest.txt:

Main-Class: org.python.util.JarRunner

然后更新jar的清单:

Then update the jar's manifest:

$ jar ufm myapp.jar manifest.txt

现在,您应该可以像这样运行您的应用了:

Now you should be able to run your app like this:

$ java -jar myapp.jar

这篇关于使用Jython将我的Python脚本作为JAR文件分发吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆