Dear Rajat,
Letr try o do a Pythonic Javaneese patchwork.
Step 1 - Compile a valid Java
class. Enclosed a sample source for a HelloWorld using Java Swing.
import javax.swing.*;
public class HelloWorld
{
public static void main(String
args[])
{
}
public void showIt()
{
JFrame frame = new JFrame("Hello");
JLabel label = new JLabel("Hello,
Welcome to Pythonic Javaneese");
frame.getContentPane().add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
C:\Jython\myprojects\testing>javac HelloWorld.java
C:\Jython\myprojects\testing>dir HelloWorld.*
Volume in drive C has no label.
Volume Serial Number is 5CC0-9DEE
Directory of C:\Jython\myprojects\testing
11/04/2008 05:05 PM
748 HelloWorld.class
11/04/2008 05:00 PM
427 HelloWorld.java
2
File(s) 1,175 bytes
0
Dir(s) 4,345,040,896 bytes free
Step 2 - Prepare a Jython script
using a Python execution model. The script is stored in the same folder
as the Java class code (olamundo.py)
import sys
import HelloWorld
def runJavaClass():
ola = HelloWorld()
ola.showIt()
def main():
runJavaClass()
if __name__ == "__main__":
main()
Step 3 - Run the Jython script
to execute the Java class
jython olamundo.py
Step 4 - Check the screen
output (this
is a Java Swing display)
Hope this sample helps. Have fun.
Regards,
Claude
Claude Falbriard
Developer
AMS Hortolândia / SP - Brazil
phone: +55 13 9762 4094
cell: +55 13 8117 3316
e-mail: claudef <at> br.ibm.com
| dudeja.rajat <at> gmail.com
11/04/2008 01:04 PM
|
|
To
|
claudef <at> br.ibm.com
|
|
cc
|
|
|
Subject
|
Re: [Jython-users] Hello to everyone.
And my first basic question - How to use a Java class in Jython? |
|
On Tue, Nov 4, 2008 at 3:02 PM, <
dudeja.rajat <at> gmail.com>
wrote:
On Tue, Nov 4, 2008 at 2:39 PM, <
claudef <at> br.ibm.com>
wrote:
Dear Rajat,
The easieat way to use a Java class is through the Jython interpreter.
Below a sample code that illustrate some of the concepts.
Step 1 - Import the class pointing to the java package name. To avoid
eventual name conflicts, rename a Java class name during the import.
A sample:
from java.util
import Calendar as JavaCalendar
from java.text import SimpleDateFormat
Step 2 - Use the Java class right from the shelf (no instance creation
is required, no "new")
mytoday
= JavaCalendar.getInstance()
sdf = SimpleDateFormat("MM/dd/yyyy")
Step 3 - Use the Java class methods the same way as you use to do under
Python
timev = JavaCalendar.getTime(mytoday)
stringdate = sdf.format(timev)
print ("The new age of Java & Python starts
at date = " +stringdate)
C:\Jython\myprojects\testing>jython classtest1.py
The new age of Java & Python starts at date = 11/04/2008
Have fun with Java classes and Jython.
Regards,
Claude
Dear Claude,
Thanks for the detailed description on using the java class.
I have written a sample Hello World class (HelloWorld.class) in Java. Now,
I want to import this class in Jython using the statement:
import HelloWorld
Jython is unable to find this class and gives me the following error:
ImportError: no module named HelloWorld
Sorry that was a typo.
Please suggest should I change some enviroment variable or there is anything
else?
--
Regrads,
Rajat