How do I compile my Python program to a .jar with Jython?

Translate Java into Jython

  • Help me translate this Java into Jython(Python)... I usually horse around in Python but my job is making me use some use some Java libraries thus my resorting to Jython. I am trying to use the an API to access the Enterprise version of SourceForge. VA Software ships a JAR file that wraps their SOAP API. Here is an example code fragment (in Java) that does performs a login and then receives a session key: import com.vasoftware.sf.soap43.webservices.ClientSoapStubFactory; import com.vasoftware.sf.soap43.webservices.sfmain.ISourceForgeSoap; import com.vasoftware.sf.soap43.webservices.sfmain.ProjectSoapList; import com.vasoftware.sf.soap43.webservices.sfmain.ProjectSoapRow; import java.rmi.RemoteException; import java.io.IOException; /** * The SoapRunner class demonstrates how the SOAP APIs can be used * to list, create or edit projects, trackers, artifacts, task groups, * tasks etc. */ public class SoapRunner { /** SourceForge Soap interface handle */ private ISourceForgeSoap mSfSoap; /** Login user name */ private String mLoginUserName; /** Session id */ private String mSessionId; /** Soap server url */ private String mServerUrl; /** * Class consturctor. */ public SoapRunner(String serverUrl) { mServerUrl = serverUrl; mSfSoap = (ISourceForgeSoap) ClientSoapStubFactory.getSoapStub(ISourceForgeSoap.class, serverUrl); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ } That last line is giving me grief.. My Java skills are weak.. and I can't figure out how that would map to Jython syntax. Help! PS.. don't ask me to access the SourceForge API via CPython. SOAP under CPython is brain dead.. :)

  • Answer:

    I can explain the Java, I'll leave to you the transliteration into Jython. Piece by piece: mSfSoap = The declaration above tells us that mSfSoap is a Java reference variable (in C terms, a pointer) of type ISourceForgeSoap ; this reference will be assigned the result of the expression to its right mSfSoap = (ISourceForgeSoap) The parenthesized type name is a cast ClientSoapStubFactory ClientSoapStubFactory is a class imported in the first line of the example code .getSoapStub( is a (static) method of that class getSoapStub(ISourceForgeSoap.class, serverUrl); that method is called with two arguments, the ISourceForgeSoap.class (see below) and the url, a String ISourceForgeSoap.class In Java, all types (both primitives and classes derived from Object) have a "static member" of type Class named class, that is that instance's type's Class. If you had an actual instance, this would be the same Class instance you'd get by calling instance.getClass().

tucsongal at Ask.Metafilter.Com Visit the source

Was this solution helpful to you?

Other answers

Erm. So. The static method takes the Class instance and the url, and returns an object of dynamic type ISourceForgeSoap, which is assigned to the member variable mSfSoap. While the dynamic or runtime type is ISourceForgeSoap, the static* or compile time type is some base type, so it's necessary to cast the returned reference. * I'm using a different meaning of static here than I was above, in case that's not clear. More questions? Ask.

orthogonality

Well.. reading your explanation gets me closer. It seems like you have to go through hoops in Java to do stuff Python does auto-magically.. But I am still not sure how to 'port' the code still.. I'm going to keep thinking about it....

tucsongal

Looking at the JPython FAQ, it seems that ISourceForgeSoap.class is represented by a Python instance of the Python type JavaClass.

orthogonality

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.