在jar文件中搜索文件(在要找到的jar文件中包含Jython类文件)(Searching for files in a jar file (Including Jython class files

编程入门 行业动态 更新时间:2024-10-28 02:23:43
在jar文件中搜索文件(在要找到的jar文件中包含Jython类文件)(Searching for files in a jar file (Including Jython class files in a jar file to be found))

在Java应用程序中使用Jython之后,我可以集成Java和Jython(Java方法可以调用python脚本)。 python代码位于py目录中。 Jython使用sys.path.append("py")命令来查找Jython脚本。

然后,我尝试将所有文​​件集成到一个jar文件中。

对于前一步,我可以生成一个jar文件,并将py目录与jar文件一起复制,并且工作正常。 我使用IntelliJ IDEA生成jar文件。

│   └── py │   ├── Context$py.class │   ├── Context.py │   ├── Host$py.class │   └── Host.py ├── aggregationPython.jar <-- Generated jar file

为了下一步,我尝试复制jar文件中的py目录,我也使用了IntelliJ。

我检查了jar文件在jar文件中有py目录。 您会看到py目录位于jar文件的根目录下。

但是,当我执行jar文件时,我得到一个错误,说jython模块丢失了。

> java -jar aggregationPython.jar Exception in thread "main" ImportError: No module named Host

可能有什么问题? 我假设py目录可以存储在jar文件中,就像它在jar文件外面找到一样。 这个假设有什么问题?

Following Using Jython Within Java Applications, I could integrate Java and Jython (Java methods can call python scripts). The python code is located inside py directory. The Jython uses sys.path.append("py") command to find the Jython scripts.

Then, I tried to integrate all the files into a single jar file.

For the pre step, I could generate one jar file, and copied the py directory along side with the jar file, and it works fine. I used IntelliJ IDEA for generating a jar file.

│   └── py │   ├── Context$py.class │   ├── Context.py │   ├── Host$py.class │   └── Host.py ├── aggregationPython.jar <-- Generated jar file

For the next step, I tried to copy the py directory inside the jar file, I also used IntelliJ for that.

I checked that the jar file has the py directory in the jar file. You see that the py directory is located at the root of jar file.

However, when I executed the jar file, I got an error saying the jython module is missing.

> java -jar aggregationPython.jar Exception in thread "main" ImportError: No module named Host

What might be wrong? I assumed the py directory can be stored in a jar file to be found just like it is found outside the jar file. What's wrong with this assumption?

最满意答案

Jython类文件位于jar文件中,Jython的搜索路径并不关心它们是否在jar文件中。 我们所需要的只是找到Jython类的位置,让Jython知道它。

从这篇文章的提示( 如何获取正在运行的JAR文件的路径? ),可以找到jar文件所在的路径。 类文件可以在位置+“py”目录中找到。

出于开发目的,还应指定源目录中的Jython源代码(“src / py”)。

在此处输入图像描述

String runningDir = Simulate.class.getProtectionDomain().getCodeSource().getLocation().getPath(); String jarPointer = "py"; String joinedPath = new File(runningDir, jarPointer).toString(); String pythonSrcPath = "/Users/smcho/code/PycharmProjects/aggregator/src"; JythonObjectFactory.setupPath(new String[]{joinedPath, "src/py"});

在此修改之后,Jython可以正确地找到类。

这是用于设置Jython搜索路径的setupPath方法。

public static void setupPath(String[] paths) { PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec("import sys;"); for (int i = 0; i < paths.length; i++) { interpreter.exec(String.format("sys.path.append(\"%s\")", paths[i])); } }

The Jython Class files are in the jar file, and the search path for Jython doesn't care if they are in jar file or not. All we need is to locate where the Jython classes are, and let Jython know about it.

From the hint of this post(How to get the path of a running JAR file?), one can find the path where the jar file is located. The class files can be found in the location + "py" directory.

For development purposes, the Jython source code in the source directory should also be specified ("src/py").

enter image description here

String runningDir = Simulate.class.getProtectionDomain().getCodeSource().getLocation().getPath(); String jarPointer = "py"; String joinedPath = new File(runningDir, jarPointer).toString(); String pythonSrcPath = "/Users/smcho/code/PycharmProjects/aggregator/src"; JythonObjectFactory.setupPath(new String[]{joinedPath, "src/py"});

After this modification, the Jython could find the classes correctly.

This is the setupPath method for setting up Jython search path.

public static void setupPath(String[] paths) { PythonInterpreter interpreter = new PythonInterpreter(); interpreter.exec("import sys;"); for (int i = 0; i < paths.length; i++) { interpreter.exec(String.format("sys.path.append(\"%s\")", paths[i])); } }

更多推荐

本文发布于:2023-08-03 10:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1388254.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   要找   Jython   jar   Searching

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!