ninia / jep

Embed Python in Java
Other
1.28k stars 145 forks source link

How to execute .py files using Interactive Console #483

Closed smartboy37597 closed 12 months ago

smartboy37597 commented 12 months ago

Describe the problem I want to execute the.py file using jet.bat. In this .py file I import my own java class. I can achieve the effect in the Interactive-Console document case, i.e

    $ jep
    >>> from java.lang import System
    >>> System.out.println('hello, world')
    hello, world
    >>>

But if I execute the following code, which is my custom package name jptest, and Calculator is the class name, the console says I can't find the jar package.

    $ jep
    >>> from jptest import Calculator
    cannot import name 'Calculator' from 'jptest' (unknown location)
    >>>

I guess jet can't read the location of the jar package where my Calculator is located. I have already packaged the jar package. In which directory should I put the jar package? Maybe there's a better way?

Environment (please complete the following information):

ndjensen commented 12 months ago

Hi, if you open up jep.bat in a text editor, you can see what it's doing. Here is the template used to generate the script: https://github.com/ninia/jep/blob/v4.1.1/src/main/scripts/jep.bat

Like any Java application, you need to get your classes onto the classpath. It looks like the jep script allows you to do that by setting an environment variable CLASSPATH that should point at your jar. Please try that.

smartboy37597 commented 12 months ago

Hi ndjensen,

Thank you very much for your reply. I have checked the implementation of jep.bat according to what you said, and set CLASSPATH to point to my own jar package. Now I can execute my custom class methods in jet.bat.

I also found that the jep command, like the python command, can execute the entire.py file directly from jep xxx.py.

Now my needs have been basically met and the problem has been solved. Thank you again for your reply.