Importing a class, please help...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • anon

    #1

    Importing a class, please help...

    Hi,

    Newbie to Python and I have a question please. I am using Windows XP, SPE
    0.8.2.a and Python24. I have done this:

    import sys
    print sys.path

    no problem there, sys imports just fine. I have a folder that I called
    c\JavaProjects\ PythonTesting and it shows up in the output from sys.path
    above. I have dropped a very simple Jar in that folder and I have attempted
    to do an Import on it. This is where things fail. I cannot see the
    contents of my Jar. What am I missing here?

    Would somebody please drop me a hint, please? I would HIGHLY appreciate an
    example; does anybody know of a website or page that would show me how to do
    this? I realize this is a simple question and I appreciate any help
    somebody would give me.

    Regards,
    joe


  • Jorge Godoy

    #2
    Re: Importing a class, please help...

    "anon" <anon@nospam.no spam> writes:
    [color=blue]
    > to do an Import on it. This is where things fail. I cannot see the
    > contents of my Jar. What am I missing here?[/color]

    That JARs are for Java and yo're using Python?


    --
    Jorge Godoy <godoy@ieee.org >

    "Quidquid latine dictum sit, altum sonatur."
    - Qualquer coisa dita em latim soa profundo.
    - Anything said in Latin sounds smart.

    Comment

    • Xavier Morel

      #3
      Re: Importing a class, please help...

      anon wrote:[color=blue]
      > Would somebody please drop me a hint, please?
      >[/color]
      Yeah, the definition of "JAR" is Java ARchive, why the hell would a
      Python script be able to read a JAR in the first place (truth is it is,
      a JAR file is nothing but a renamed ZIP, therefore the zipfile module
      allows you to read it's content) and -- more importantly -- import Java
      classes.

      Notice the difference between *Python* script and *Java* class? That's
      because they're two different languages.
      If you want to use Java classes and modules in a Pythonic context, you
      want Jython (http://www.jython.org/) not Python (http://www.python.org/)

      Comment

      • Jorgen Grahn

        #4
        Re: Importing a class, please help...

        On Sun, 05 Feb 2006 17:16:38 +0100, Xavier Morel <xavier.morel@m asklinn.net> wrote:[color=blue]
        > anon wrote:[color=green]
        >> Would somebody please drop me a hint, please?
        >>[/color]
        > Yeah, the definition of "JAR" is Java ARchive, why the hell would a
        > Python script be able to read a JAR in the first place[/color]

        You are rude to an obvious newbie here ... please keep in mind that today's
        stupid newbies are tomorrow's Python professionals.

        ....[color=blue]
        > If you want to use Java classes and modules in a Pythonic context, you
        > want Jython (http://www.jython.org/) not Python (http://www.python.org/)[/color]

        Maybe he /is/ running Jython and failed to explain that properly? If I
        understand things correctly, jar files are not tied to Java but to the java
        bytecode format -- which Java and Jython share.

        In either case, we'd need more information from the original poster.

        /Jorgen

        --
        // Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
        \X/ snipabacken.dyn dns.org> R'lyeh wgah'nagl fhtagn!

        Comment

        • Magnus Lycka

          #5
          Re: Importing a class, please help...

          Jorgen Grahn wrote:[color=blue][color=green]
          >>Yeah, the definition of "JAR" is Java ARchive, why the hell would a
          >>Python script be able to read a JAR in the first place[/color]
          >
          > You are rude to an obvious newbie here ...[/color]
          Agreed. I don't think it was intended, but we should be
          cautious with our language when we address strangers...
          [color=blue]
          > Maybe he /is/ running Jython and failed to explain that properly?[/color]
          Not likely, since he wrote "python24". I think the newest Jython
          is 2.2a1...

          Comment

          • Xavier Morel

            #6
            Re: Importing a class, please help...

            Jorgen Grahn wrote:[color=blue]
            > You are rude to an obvious newbie here ... please keep in mind that[/color]
            today's[color=blue]
            > stupid newbies are tomorrow's Python professionals.
            >[/color]
            I didn't mean to be rude and apologize if that's the way it came out.
            [color=blue]
            > Maybe he /is/ running Jython and failed to explain that properly?
            >[/color]
            No, he explicitely stated that he's using Python 2.4 (I'm pretty sure
            Jython's latest release is 2.1) and SPE (which didn't handle Jython last
            time I tried it)
            [color=blue]
            > If I
            > understand things correctly, jar files are not tied to Java but to the java
            > bytecode format -- which Java and Jython share.
            >[/color]
            Yes, it's used to store/package compiled java classes (.class bytecode
            files) and various metadata associated to the classes (resources, ...)
            and therefore tied to the bytecode (or the JVM) indeed.

            Comment

            • anon

              #7
              Re: Importing a class, please help...

              Xavier

              Yes I meant Python. I have also been using Jython and I am VERY WELL aware
              what a JAR is. Jython has the ability to call methods stored in classes in
              a JAR. I was only asking if this could be done with Python also.

              You ask "why the hell would a Python script be able to read a JAR..". Well,
              I was simply asking a newbie question. The only way to gather knowladge is
              by asking questions.

              You know, an old college proffesor I had was very knowladgeable and wise.
              He always took the time to answer questions, no matter how 'stupid' they
              would sound. One can make a bigger impression on folks by stepping down a
              couple of steps and give a polite answer. I mean no disrespect, just a
              suggestion when you post answers to folks like me who are just getting
              started with a new language...

              regards


              "Xavier Morel" <xavier.morel@m asklinn.net> wrote in message
              news:ds588l$aii $1@aphrodite.gr ec.isp.9tel.net ...[color=blue]
              > anon wrote:[color=green]
              >> Would somebody please drop me a hint, please?
              >>[/color]
              > Yeah, the definition of "JAR" is Java ARchive, why the hell would a Python
              > script be able to read a JAR in the first place (truth is it is, a JAR
              > file is nothing but a renamed ZIP, therefore the zipfile module allows you
              > to read it's content) and -- more importantly -- import Java classes.
              >
              > Notice the difference between *Python* script and *Java* class? That's
              > because they're two different languages.
              > If you want to use Java classes and modules in a Pythonic context, you
              > want Jython (http://www.jython.org/) not Python (http://www.python.org/)[/color]


              Comment

              • Terry Hancock

                #8
                Re: Importing a class, please help...

                On Sun, 5 Feb 2006 10:41:51 -0500
                "anon" <anon@nospam.no spam> wrote:[color=blue]
                > Newbie to Python and I have a question please. I am using
                > Windows XP, SPE 0.8.2.a and Python24. I have done this:
                >
                > import sys
                > print sys.path
                >
                > no problem there, sys imports just fine. I have a folder
                > that I called c\JavaProjects\ PythonTesting and it shows
                > up in the output from sys.path above. I have dropped a
                > very simple Jar in that folder and I have attempted to do
                > an Import on it. This is where things fail. I cannot see
                > the contents of my Jar. What am I missing here?[/color]

                As others have pointed out, ".jar" is going to be Java
                bytecode, and from that strange looking path (was, I assume
                supposed to be "C:\JavaProject s\PythonTesting "), it looks
                very much like you really wanted to be using Jython (a.k.a
                "Java Python") rather than Python (a.k.a. "C Python").

                Perhaps you have both Jython and Python 2.4 installed and
                you are launching the latter when you meant the former?

                In any case, C Python cannot load Java classes (that's the
                main raison d'etre for Jython).

                --
                Terry Hancock (hancock@Anansi Spaceworks.com)
                Anansi Spaceworks http://www.AnansiSpaceworks.com

                Comment

                • Alex Martelli

                  #9
                  Re: Importing a class, please help...

                  anon <anon@nospam.no spam> wrote:
                  [color=blue]
                  > Xavier
                  >
                  > Yes I meant Python. I have also been using Jython and I am VERY WELL aware
                  > what a JAR is. Jython has the ability to call methods stored in classes in
                  > a JAR. I was only asking if this could be done with Python also.[/color]

                  Classic Python is able to import Python modules from a .zip file; it
                  would be a reasonably easy tweak to allow the extension to be .jar or
                  ..foo or .zap as well as .zip -- but they'd still have to be Python
                  modules. Java classes use a completely different bytecode from Classic
                  Python: if you need to access such JVM bytecode, use Jython instead.


                  Alex

                  Comment

                  Working...