Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Jython interactive mode

  1. #1
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Jython interactive mode

    Does anyone know how to run Jython with "interactive mode" (or whatever the correct terminology is, this is the terminology in Iron Python)? Basically, interactive mode is similar to running a regular script using the eval() method, but statements which evaluate to a result print out the result.

    So, for example:

    for i in range(5):
        i * i

    In interactive mode, i * i would print out to the script context output stream, so the output would look like:
    0
    1
    4
    9
    16
    In non-interactive mode, nothing would be printed out.

    The higher-level purpose of this is I'm trying to create a Swing "command console" which emulates the standard Python command console, but can be added to Swing applications.

    Here's a short blurb of how I'm creating the Jython script engine and running the test script (note: you will need the Jython library, you can find it here. Java SE6 or newer is also required for the Java scripting classes).

    // runs the script, but interactive mode is not turned on
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("python");
    String command = "for i in range(5):\r\n\ti * i";
    engine.eval(command);

    edit:

    I figured out a work-around to this.

    Instead of using the Java API scripting, I created an InteractiveInterpreter part of the Jython package.

    InteractiveInterpreter engine = new InteractiveInterpreter();
    String command = "for i in range(5):\r\n\ti * i";
    engine.runsource(commands);

    Hopefully next month-ish I will put the whole code in the tips section (still a few things to work out)
    Last edited by helloworld922; July 18th, 2010 at 12:37 AM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Jython interactive mode

    Tricky! Glad you resolved this yourself helloworld because I didn't have much input
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

Similar Threads

  1. Replies: 2
    Last Post: April 21st, 2010, 11:25 AM
  2. need API for Connection Routing in Interactive Diagram Editor
    By pavan arepu in forum AWT / Java Swing
    Replies: 2
    Last Post: April 21st, 2010, 11:19 AM
  3. Works on debug mode but not on run mode
    By alfonsoraul in forum Member Introductions
    Replies: 0
    Last Post: April 14th, 2010, 03:58 PM
  4. Interactive Touchscreen using Java or Java Fx
    By indigovision in forum Paid Java Projects
    Replies: 3
    Last Post: March 16th, 2009, 03:35 PM