使用“classpath:”从 Jython 中的类路径加载资源

发布于 2024-09-13 13:35:05 字数 920 浏览 2 评论 0原文

我有一个相对较大的 Java 应用程序,如果喜欢一点 Python,它会受益匪浅。为此,我一直致力于在 Jython 中启动并运行它。我当前的障碍是让类路径正确。

我采用了两种方法来设置类路径:

  1. 使用 shell 脚本,我构建了一个 jar 列表并执行 java -cp ${CP} -jar jyton.jar ,其中 $CP 是我的应用程序所需的 jar 列表。这似乎不起作用。我无法从这些 jar 中导入任何类,只得到 ImportError: No module named apache
  2. 使用 bootstrap python 脚本,我使用 glob 创建了一个路径列表,并使用 [sys.path.append(path) for path in JAR_LIST] 将它们附加到当前路径。这似乎工作正常;我现在可以从包含的 jar 中导入我需要的任何类。

上面的内容有点令人困惑,因为我找到的大多数信息都转向使用 $CLASSPATH 和 -cp 来添加你的 jars,但我无法让它工作。所以到目前为止的问题是:#2 是使用 Jython 时向类路径添加依赖项的正确方法吗?

我质疑我的方法的主要原因是因为我在充分利用我的应用程序时仍然遇到问题。我的应用程序中的许多位置都使用相对 URL 来尊重资源:classpath:some-ritic-file.xml

some-ritic-file.xml 和我的许多类驻留在同一个 jar 中。我可以从该 jar 导入类,但是任何使用 classpath:some-ritic-file.xml 加载 xml 的尝试都会导致 java.io.FileNotFoundException

任何关于为什么我的类的见解可用,但使用 classpath: 的资源相对路径不太受欢迎。此时我不知所措。

I've got a relatively large Java application that would benefit from a bit of Python love. To that end I've been working on getting it up and running in Jython. My current roadblock is getting the classpath right.

I've taken two approaches to setting the classpath:

  1. Using a shell script, I've built up a list of jars and executed java -cp ${CP} -jar jyton.jar where $CP is a list of the jars needed for my app. This doesn't seem to work. I'm unable to import any classes from these jars, getting only ImportError: No module named apache instead.
  2. Using a bootstrap python script, i've created a list of paths using glob, and appended them to the current path using [sys.path.append(path) for path in JAR_LIST]. This seems to work correctly; I can now import any of the classes I need to from the included jars.

The above is a bit confusing as most information I've been able to find has steered towards using $CLASSPATH and -cp to added your jars, but I can't get that to work. So the question so far: Is #2 the proper way to add dependancies to your classpath when using Jython?

The main reason I question my methods is because I'm still having problems fully utilizing my application. A number of places in my app reverences resources using relative URLs : classpath:some-critical-file.xml

some-critical-file.xml and a number of my classes reside within the same jar. I'm able to import classes from that jar, but any attempts to load my xml with classpath:some-critical-file.xml results in a java.io.FileNotFoundException

Any insight as to why my classes are available but relative paths to resources using classpath: are not would be much appreciate. I'm at a loss at this point.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慵挽 2024-09-20 13:35:05

我最近自己也遇到了一些类路径奇怪的情况。您是否尝试过以下老式方法:

CLASSPATH = ${CLASSPATH}:your.jar

导出类路径

jython your_script.jy

I've run into a little classpath weirdness myself lately. Have you tried the old school approach of:

CLASSPATH = ${CLASSPATH}:your.jar

export CLASSPATH

jython your_script.jy

牵你的手,一向走下去 2024-09-20 13:35:05

如果您使用 java -jar 调用独立 jar,则从 Java 文档 ...

-jar

当您使用此选项时,JAR 文件是所有用户类的源,并且其他用户类路径设置将被忽略。

因此,使用 -jar 时不可能向类路径添加任何内容。请参阅 这个答案 获取解决方案 - 基本上将 jython.jar 添加到类路径(使用 -cp 或 CLASSPATH)并运行直接 org.python.util.jython 类。

If you're invoking the standalone jar using java -jar then from the Java Documentation ...

-jar

When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored.

So it it not possible to add anything to the classpath when using -jar. See this answer for a solution - basically add the jython.jar to the classpath (either using -cp or CLASSPATH) and run the org.python.util.jython class directly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文