Kyle Wright
Kyle Wright

Reputation: 520

Eclipse with Jython doesn't understand Java Imports

I have set up Eclipse to use Jython as documented here:

http://www.jython.org/jythonbook/en/1.0/JythonIDE.html (Under Minimal Configuration)

I am following this tutorial as best I can, but for some reason the IDE does not understand Java imports. The line javax.swing import JFrame, JLabel underlines JFrame and JLabel as unresolved.

Code in entirety:

# -*- coding: utf-8 -*-
import sys
from optparse import OptionParser

greetings = dict(en=u'Hello %s!',
                 es=u'Hola %s!',
                 fr=u'Bonjour %s!',
                 pt=u'Al %s!')

uis = {}
def register_ui(ui_name):
    def decorator(f):
        uis[ui_name] = f
        return f
    return decorator

def message(ui, msg):
    if ui in uis:
        uis[ui](msg)
    else:
        raise ValueError("No greeter named %s" % ui)

def list_uis():
    return uis.keys()

@register_ui('console')
def print_message(msg):
    print msg

@register_ui('window')
def show_message_as_window(msg):
    from javax.swing import JFrame, JLabel
    frame = JFrame(msg,
                   defaultCloseOperation=JFrame.EXIT_ON_CLOSE,
                   size=(100, 100),
                   visible=True)
    frame.contentPane.add(JLabel(msg))

if __name__ == "__main__":
    parser = OptionParser()
    parser.add_option('--ui', dest='ui', default='console',
                      help="Sets the UI to use to greet the user. One of: %s" %
                      ", ".join("'%s'" % ui for ui in list_uis()))
    parser.add_option('--lang', dest='lang', default='en',
                      help="Sets the language to use")
    options, args = parser.parse_args(sys.argv)
    if len(args) < 2:
        print "Sorry, I can't greet you if you don't say your name"
        sys.exit(1)

    if options.lang not in greetings:
        print "Sorry, I don't speak '%s'" % options.lang
        sys.exit(1)

    msg = greetings[options.lang] % args[1]

    try:
        message(options.ui, msg)
    except ValueError, e:
        print "Invalid UI name\n"
        print "Valid UIs:\n\n" + "\n".join(' * ' + ui for ui in list_uis())
        sys.exit(1)

When I run it I selected Jython. So I don't understand why Eclipse doesn't understand. Do I need to include Jython JAR files in every Jython project...?

Thanks in advance.

Upvotes: 0

Views: 525

Answers (1)

John Landells
John Landells

Reputation: 112

Have you created a new PyDev project for this? Without that, Eclipse won't be able to find your full Jython installation, which could explain the underlinings. In my environment (Eclipse Kepler, PyDev, and Jython 2.5.2) it works correctly.

Upvotes: 1

Related Questions