Importing a class, please help...

A

anon

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
 
J

Jorge Godoy

anon said:
to do an Import on it. This is where things fail. I cannot see the
contents of my Jar. What am I missing here?

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


--
Jorge Godoy <[email protected]>

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

Xavier Morel

anon said:
Would somebody please drop me a hint, please?
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/)
 
J

Jorgen Grahn

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

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

....
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/)

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
 
X

Xavier Morel

Jorgen said:
> You are rude to an obvious newbie here ... please keep in mind that today's
> stupid newbies are tomorrow's Python professionals.
>
I didn't mean to be rude and apologize if that's the way it came out.
Maybe he /is/ running Jython and failed to explain that properly?
>
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)
If I
understand things correctly, jar files are not tied to Java but to the java
bytecode format -- which Java and Jython share.
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.
 
M

Magnus Lycka

Jorgen said:
You are rude to an obvious newbie here ...
Agreed. I don't think it was intended, but we should be
cautious with our language when we address strangers...
Maybe he /is/ running Jython and failed to explain that properly?
Not likely, since he wrote "python24". I think the newest Jython
is 2.2a1...
 
A

anon

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
 
A

Alex Martelli

anon said:
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.

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
 
T

Terry Hancock

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?

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:\JavaProjects\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 ([email protected])
Anansi Spaceworks http://www.AnansiSpaceworks.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,283
Messages
2,571,405
Members
48,098
Latest member
inno vation

Latest Threads

Top