简体   繁体   English

在jar文件中搜索文件(包括将在jar文件中找到的Jython类文件)

[英]Searching for files in a jar file (Including Jython class files in a jar file to be found)

Following Using Jython Within Java Applications , I could integrate Java and Jython (Java methods can call python scripts). 在Java Applications中使用Jython之后,我可以集成Java和Jython(Java方法可以调用python脚本)。 The python code is located inside py directory. python代码位于py目录中。 The Jython uses sys.path.append("py") command to find the Jython scripts. Jython使用sys.path.append("py")命令查找Jython脚本。

Then, I tried to integrate all the files into a single jar file. 然后,我尝试将所有文​​件集成到一个jar文件中。

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. 对于第一步,我可以生成一个jar文件,并将py目录与jar文件一起复制,并且工作正常。 I used IntelliJ IDEA for generating a jar file. 我使用IntelliJ IDEA生成jar文件。

│   └── 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. 下一步,我尝试将jar目录中的py目录复制,我也使用了IntelliJ。

I checked that the jar file has the py directory in the jar file. 我检查了jar文件在jar文件中是否具有py目录。 You see that the py directory is located at the root of jar file. 您会看到py目录位于jar文件的根目录。

在此处输入图片说明

However, when I executed the jar file, I got an error saying the jython module is missing. 但是,当我执行jar文件时,出现错误,提示jython模块丢失。

> 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. 我假设py目录可以存储在jar文件中,就像在jar文件之外找到它一样。 What's wrong with this assumption? 这个假设有什么问题?

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. Jython类文件位于jar文件中,而Jython的搜索路径并不关心它们是否位于jar文件中。 All we need is to locate where the Jython classes are, and let Jython know about it. 我们需要做的就是找到Jython类的位置,并让Jython知道。

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. 从本文的提示( 如何获取正在运行的JAR文件的路径? )中,可以找到jar文件所在的路径。 The class files can be found in the location + "py" directory. 可以在location +“ py”目录中找到类文件。

For development purposes, the Jython source code in the source directory should also be specified ("src/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"});

After this modification, the Jython could find the classes correctly. 进行此修改后,Jython可以正确找到类。

This is the setupPath method for setting up Jython search path. 这是用于设置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]));
    }
}

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

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