Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3871122
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T21:47:36+00:00 2026-05-19T21:47:36+00:00

I am trying to implement Python code I’ve written in Java and I want

  • 0

I am trying to implement Python code I’ve written in Java and I want to use Jython. There are a number of websites that provide information about how to build Jython object factories, but all of the examples use Python classes. For example:

http://jythonpodcast.hostjava.net/jythonbook/en/1.0/JythonAndJavaIntegration.html#using-jython-within-java-applications

However, I am not using classes in my code. That is, I’ve got a number of files that each contain several function definitions and but no classes. Therefore, I wanted to know if anyone could point me to a resource or provide information about if it is possible to use Jython to bridge the gap between Java and Python without having classes.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T21:47:37+00:00Added an answer on May 19, 2026 at 9:47 pm

    If I understand what you’re asking correctly (I take it you want to invoke a Python function from Java code), I think that is possible.

    This is completely untested and a bit speculative, but it looks like Jython provides a Java object model that will allow you to do what you want. The important thing to understand here is that Python stores functions as attributes of a dictionary for each module (Python file). Thus, in pure Python, one can get and use the function os.path.join() doing something like:

    import os
    getattr(getattr(os, 'path'), 'join').__call__('.', 'directory')
    

    Not the most Pythonic syntax around, but I think it helps clarify how one can use the PyObject interface to load an invoke a Python function from a Python module.

    To do this from Java it seems that you would need to follow a pattern similar to List 10-5 in the section of the Jython book you linked to. There, in the first constructor, is an example of how to import a Python class (also stored as an attribute of a Python module). Modifying this somewhat, you’d end up with something like:

    class PythonExecutor {
        public static void main(String...args) {
            String moduleName = "...";
            String functionName = "...";
    
            PyObject importer = state.getBuiltins().__getitem__(Py.newString("__import__"));
    
            // module = __import__(moduleName)
            PyObject module = importer.__call__(Py.newString(moduleName));
    
            // function = getattr(module, functionName)
            PyObject function = module.__getattr__(functionName);
    
            // function.__call__()
            function.__call__();
        }
    }
    

    Of course, you can already see from the above snippet some of the disadvantages Java has compared to dynamic languages. This is particularly acute if your Python function requires any sort of parameters, as any Java objects would need to be marshalled into PyObjects. Again, the listing mentioned above has several examples using the java2py() method of the Py class to convert a Java object into a Python object. Similarly, if your Python function returns some sort of result, you will likely need to convert it into a Java object using the __tojava__() function on PyObject.

    To handle all of these details, it may be worth your time to construct a Java adapter for the Python modules you will need to access from your Java code, similar to the way the JythonObjectFactory provides several methods which convert Java objects into Python objects, invoke the __init__() method on a Python class, and return the result as a Java object.

    Hopefully, that’s enough to point you in the right direction on this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement dynamic reloading objects in Python, that reflect code changes live.
I am trying to learn Ruby, and want to implement the Python algorithms from
I'm new Python and trying to implement code in a more Pythonic and efficient
I am trying to implement the python logging handler TimedRotatingFileHandler . When it rolls
I am trying to implement string unescaping with Python regex and backreferences, and it
i am trying to implement RSA in python(i am new to python) for my
I'm learning Python and i've been trying to implement a Singleton-type class as a
I'm trying to implement file upload functionality in the iPhone app. Server code is
All I am currently trying implement something along the lines of dim l_stuff as
trying to implement a dialog-box style behaviour using a separate div section with all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.