在python模块中导入java类

发布于 2024-12-10 08:02:42 字数 239 浏览 4 评论 0原文

我在 Java 中创建了一个包,并在其中创建了一个 publis 类。

package GX1164;
public class Test
{
    public int add(int a)
    {
    return (a+a);
    }
}

现在我必须在 python 模块中导入这个包和类来调用函数“add”。 但不知怎的,我无法导入测试类。我不断收到错误“未定义的模块测试”

I have created one package in Java and one publis class inside it.

package GX1164;
public class Test
{
    public int add(int a)
    {
    return (a+a);
    }
}

Now I have to import this package and class in python module to call the function "add".
But somehow I am not able to import the class Test. I keep getting the error "Undefined module Test"

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

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

发布评论

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

评论(2

紙鸢 2024-12-17 08:02:42

将您的 java 类构建到 .jar 文件中,然后执行以下操作
将 jar 附加到 sys.path 与设置类路径相同。

import sys
sys.path.append("GX1164.jar")

import GX1164

test = GX1146.Test()
print test.add(42)

Build your java class into a .jar file then do the following
Appending the jar to sys.path is the same as setting a class path.

import sys
sys.path.append("GX1164.jar")

import GX1164

test = GX1146.Test()
print test.add(42)
·深蓝 2024-12-17 08:02:42

好吧,您的模块名为 GX1164,类名为 Test

import GX1164
test = GX1164.Test()
print test.add(42)

您还需要确保以某种方式设置 CLASSPATH这将允许 Jython 找到 Test.class。

Well, your module is called GX1164 and the class is called Test:

import GX1164
test = GX1164.Test()
print test.add(42)

You also need to make sure that the CLASSPATH is set up in a manner that will allow Jython to find Test.class.

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