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

  • SEARCH
  • Home
  • 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 6235467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:40:24+00:00 2026-05-24T10:40:24+00:00

I am working on a big Java project, where Jython scripts are interpreted from

  • 0

I am working on a big Java project, where Jython scripts are interpreted from Java code. For a reason I have not yet figured out, nothing works in the Jython script unless functions, class, variables, are declared all as global.
In an attempt to track down this problem, I have narrowed the issue down to this. Below are :

  • A Jython script script.py which runs well when launched with java -jar jython.jar script.py
  • A Java main class Interpreter.java which uses Jython 2.2 methods to try and interpret the Jython script given above

Python script :

#global aFunction # uncommenting this makes the script work from Java

def main():
    aFunction()


def aFunction():
    print 'aFunction() called'


main()

Java class :

import java.io.File;

import org.python.core.PyException;
import org.python.core.PyStringMap;
import org.python.core.PySystemState;


public class Interpreter {

    public static void main(final String[] args) {
        final PyStringMap localNameSpace = new PyStringMap();
        final PyStringMap globalNameSpace = new PyStringMap();

        final File scriptFile = new File("../../jython/script.py");       
        PySystemState.initialize();

        try {
            org.python.core.__builtin__.execfile(scriptFile.getAbsolutePath(), globalNameSpace, localNameSpace);
        } catch (final PyException pyException) {
            pyException.printStackTrace();
        }

    }

}

Here is the error I get when running the Java class.

Traceback (innermost last):
  File "/opt/coflight/axel/workspace/essais/../../jython/script.py", line 12, in ?
  File "/opt/coflight/axel/workspace/essais/../../jython/script.py", line 5, in main
NameError: aFunction

Any ideas on how to correct the Java class so that the interpreter can run the Jython script ? 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-24T10:40:24+00:00Added an answer on May 24, 2026 at 10:40 am

    I can’t explain the error that you get, but I was able to make it work by using PythonInterpreter:

    org.python.util.PythonInterpreter interp = new org.python.util.PythonInterpreter();
    String scriptname = "script.py"; 
    interp.execfile(scriptname);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have big enough and already working project, written not by a programmer, but
Working on a project at the moment and we have to implement soft deletion
I'm currently working through an assignment that deals with Big-O and running times. I
The team I am working in has embraced scrum in a big way. I
Whilst trying to get our app working in Firefox (I'm a big proponent of
I've been working through a recent Computer Science homework involving recursion and big-O notation.
I'm working on my first Ruby on Rails aplication and it's quite big (at
Working on a project that parses a log of events, and then updates a
I've developed a syntax highlighter in Java for Android and it's working well, but
Working with dates in ruby and rails on windows, I'm having problems with pre-epoch

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.