【背景】
中的问题:
抱歉打扰一下,请问如果我用jython解析完python后,生成的字节码到了什么地方呢?呃我参照了Using jython的文档没有找到解决办法,在盘中搜索我原.py文件的文件名也没有找到,使用google也没有找到解决办法。。。。我需要用这个文件放到另一台只有JVM的机子上使用。诚盼解答,谢谢 |
即:
想办法,找到jython对于py所生成的二进制class文件。
【解决过程】
1.参考:
去先看看,jython支持哪些参数:
D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python>jython --help usage: jython [option] ... [-c cmd | -m mod | file | -] [arg] ... Options and arguments: -c cmd : program passed in as string (terminates option list) -Dprop=v : Set the property `prop' to value `v' -C codec : Use a different codec when reading from the console. -h : print this help message and exit (also --help) -i : inspect interactively after running script and force prompts, even if stdin does not appear to be a terminal -jar jar : program read from __run__.py in jar file -m mod : run library module as a script (terminates option list) -Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew -S : don't imply 'import site' on initialization -u : unbuffered binary stdout and stderr -v : verbose (trace import statements) -V : print the Python version number and exit (also --version) -W arg : warning control (arg is action:message:category:module:lineno) file : program read from script file - : program read from stdin (default; interactive mode if a tty) arg ... : arguments passed to program in sys.argv[1:] Other environment variables: JYTHONPATH: ';'-separated list of directories prefixed to the default module search path. The result is sys.path.
试试的结果:
D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python>jython -m compileall demoRunPythonUnderWindows.py Listing demoRunPythonUnderWindows.py ... Can't list demoRunPythonUnderWindows.py
很明显还是没有生成class文件。
2.参考:
Compiling Python Source to Real Java Classes
去试试jthonc:
D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python>jythonc --help 'jythonc' is not recognized as an internal or external command, operable program or batch file. D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python>jythonc 'jythonc' is not recognized as an internal or external command, operable program or batch file.
很明显,找不到对应工具。
所以参考原帖去添加环境变量:
不过看到:
The previous section describes how Python classes can be created that subclass from Java classes. |
以为其说是的上一章介绍了,如何生成class文件呢。
结果后来从:
Overview of Jython Documentation
->
Subclassing Java Classes in Jython
发现不是的,其只是jython和python互相调用。
3.再去添加环境变量,但是却遇到:
【已解决】虽已安装了jython后但找不到jythonc.py
4.最后,是用如下代码:
#!/usr/bin/python # -*- coding: utf-8 -*- """ Function: 【已解决】找到Jython编译py文件所生成的二进制的class文件 https://www.crifan.com/try_find_where_the_jython_generated_binary_class_files_for_py_file Author: Crifan Li Version: 2013-11-07 Contact: https://www.crifan.com/about/me """ import os; import compileall; curDir = os.getcwd(); compileall.compile_dir(curDir); # compile py file in current dir #D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python>ls #compilePyToClass.py demoRunPythonUnderWindows.py # #D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python>python compilePyToClass.py #Listing D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python ... #Compiling D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python\compilePyToClass.py ... #Compiling D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python\demoRunPythonUnderWindows.py ... # #D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python>ls #compilePyToClass.py compilePyToClass.pyc demoRunPythonUnderWindows.py demoRunPythonUnderWindows.pyc # #D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python>rm *.pyc # #D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python>jython compilePyToClass.py #Listing D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python ... #Compiling D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python\compilePyToClass.py ... #Compiling D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python\demoRunPythonUnderWindows.py ... # #D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python>ls #compilePyToClass$py.class compilePyToClass.py demoRunPythonUnderWindows$py.class demoRunPythonUnderWindows.py # #D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python>rm *.class # #D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python>compilePyToClass.py #Listing D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python ... #Compiling D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python\compilePyToClass.py ... #Compiling D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python\demoRunPythonUnderWindows.py ... # #D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python>ls #compilePyToClass.py compilePyToClass.pyc demoRunPythonUnderWindows.py demoRunPythonUnderWindows.pyc # #D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python>rm *.pyc # #D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python>
如图:
实现了:
编译当前路径下面的py文件
- 如果用的是python,则生成的是.pyc文件
- 不指定解析器,则调用默认的python
- 如果的使用的是jython,则生成的是.class文件
【总结】
想要通过py文件获得对应的.class文件,则:
1.不能用jythonc了,因为已经被官网取消了。
2.可以使用compileall的方式。
代码为:
#!/usr/bin/python # -*- coding: utf-8 -*- """ Function: 【已解决】找到Jython编译py文件所生成的二进制的class文件 https://www.crifan.com/try_find_where_the_jython_generated_binary_class_files_for_py_file Author: Crifan Li Version: 2013-11-07 Contact: https://www.crifan.com/about/me """ import os; import compileall; curDir = os.getcwd(); compileall.compile_dir(curDir); # compile py file in current dir #D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python>ls #compilePyToClass.py demoRunPythonUnderWindows.py # #D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python>python compilePyToClass.py #Listing D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python ... #Compiling D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python\compilePyToClass.py ... #Compiling D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python\demoRunPythonUnderWindows.py ... # #D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python>ls #compilePyToClass.py compilePyToClass.pyc demoRunPythonUnderWindows.py demoRunPythonUnderWindows.pyc # #D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python>rm *.pyc # #D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python>jython compilePyToClass.py #Listing D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python ... #Compiling D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python\compilePyToClass.py ... #Compiling D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python\demoRunPythonUnderWindows.py ... # #D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python>ls #compilePyToClass$py.class compilePyToClass.py demoRunPythonUnderWindows$py.class demoRunPythonUnderWindows.py # #D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python>rm *.class # #D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python>compilePyToClass.py #Listing D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python ... #Compiling D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python\compilePyToClass.py ... #Compiling D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python\demoRunPythonUnderWindows.py ... # #D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python>ls #compilePyToClass.py compilePyToClass.pyc demoRunPythonUnderWindows.py demoRunPythonUnderWindows.pyc # #D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python>rm *.pyc # #D:\tmp\tmp_dev_root\python\tutorial_summary\how_to_dev_python>
使用方式为:
把上面代码保存为compilePyToClass.py
然后放到你想要编译为.class的py所在路径。
然后运行:
jython compilePyToClass.py
即可将对应的,当前路径下的.py文件,生成对应的.class文件。
注:
1.生成的.class文件的文件名是
原文件名$py.class
的形式。