ninia / jep

Embed Python in Java

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to execute .py files using Interactive Console

smartboy37597 opened this issue · comments

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):

  • OS Platform, Distribution, and Version: Windows 10
  • Python Distribution and Version: 3.8.3
  • Java Distribution and Version: 1.8.0_261
  • Jep Version: 4.1.1
  • Python packages used (e.g. numpy, pandas, tensorflow): no

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.

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.