473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

passing data between python -> java? [solved]

9
Hello,
I'm writing an application that takes pen data and tries to recognize the character and therefore predict the word you are writing. A java file will be returning text to stdout that contains the possible characters that the user just wrote.

I would like to be able to use nltk_lite in python to find possible words based on the bigram and return these possibilities to the java GUI to allow the user to choose the correct word.

I plan on the java and python programs to be sending data and possible next words back and forth between each other. Someone told me about pipes(?) and I couldn't find a simple tutorial online.

All i want to do is pass simple strings between the 2 programs. If anyone could recommend a python module or other way to achieve this it would be really helpful,

Thank you for any help :)

ps: I'm using linux if that makes any difference
Oct 25 '06 #1
4 16487
bartonc
6,596 Expert 4TB
You might find something at
aspn.activestate.com/ASPN/Cookbook/Python/
Oct 25 '06 #2
bartonc
6,596 Expert 4TB
I found recipes which use standard library modules that would work if your java GUI can be an XML-RPC server & client.
Oct 25 '06 #3
kudos
127 Expert 100+
I supply you with the "calling python from java part". (Thats the one you need right?)

Here is the python code (i.e. the one that should be called from the program)
Expand|Select|Wrap|Line Numbers
  1. import sys
  2.  
  3. # take the first input argument
  4.  
  5. mystring = sys.argv[1]
  6.  
  7. # reverses the string, but you can do anything you like here..
  8. #
  9.  
  10. print mystring[::-1]
  11.  
It simply reverses a string and output it to stdout. Now we would like to have a java program that get that info.

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.  
  3. class SomeJavaProgram{
  4.  
  5. public static void main(String a[]) throws IOException{
  6.  
  7. Process process = Runtime.getRuntime().exec("python SomePythonProgram.py hello");
  8. BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
  9. System.out.println(reader.readLine());
  10. reader.close();
  11.  
  12. }
  13.  
  14. }
  15.  
Save the python program as "SomePythonProgram.py" and the java program as "SomeJavaProgram.java". Compile the java program, and run it.

Its not really a "pretty way" to do it, but it works. I recall vaugly that there was something called Jython, that (I think) was a python interpreter written in java. Maybe you could use this to embedd python in your java programs? (I have never used Jython)

-kudos



Hello,
I'm writing an application that takes pen data and tries to recognize the character and therefore predict the word you are writing. A java file will be returning text to stdout that contains the possible characters that the user just wrote.

I would like to be able to use nltk_lite in python to find possible words based on the bigram and return these possibilities to the java GUI to allow the user to choose the correct word.

I plan on the java and python programs to be sending data and possible next words back and forth between each other. Someone told me about pipes(?) and I couldn't find a simple tutorial online.

All i want to do is pass simple strings between the 2 programs. If anyone could recommend a python module or other way to achieve this it would be really helpful,

Thank you for any help :)

ps: I'm using linux if that makes any difference
Oct 26 '06 #4
tegdim
9
Thanks kudos,
that's just what I was looking for :)
Thanks again for the fast responses,
tegdim

I supply you with the "calling python from java part". (Thats the one you need right?)

Here is the python code (i.e. the one that should be called from the program)
Expand|Select|Wrap|Line Numbers
  1. import sys
  2.  
  3. # take the first input argument
  4.  
  5. mystring = sys.argv[1]
  6.  
  7. # reverses the string, but you can do anything you like here..
  8. #
  9.  
  10. print mystring[::-1]
  11.  
It simply reverses a string and output it to stdout. Now we would like to have a java program that get that info.

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.  
  3. class SomeJavaProgram{
  4.  
  5. public static void main(String a[]) throws IOException{
  6.  
  7. Process process = Runtime.getRuntime().exec("python SomePythonProgram.py hello");
  8. BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
  9. System.out.println(reader.readLine());
  10. reader.close();
  11.  
  12. }
  13.  
  14. }
  15.  
Save the python program as "SomePythonProgram.py" and the java program as "SomeJavaProgram.java". Compile the java program, and run it.

Its not really a "pretty way" to do it, but it works. I recall vaugly that there was something called Jython, that (I think) was a python interpreter written in java. Maybe you could use this to embedd python in your java programs? (I have never used Jython)

-kudos
Oct 26 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: JW | last post by:
Hi, Below is a description of what I would like to do. Would someone tell me if it is possible and if so, how? I have an array (presumably 'large') that is mallocced in a C function and its...
3
by: Rob Hunter | last post by:
Hi all. I want to run a Python daemon that manages a "to-do" queue. I want it so that the daemon is always running, where its running consists of looking at the queue to see if it has any jobs,...
11
by: Mike M | last post by:
Is it possible? In the parent process, I have a socket that binds, listens and then accepts new connections (which creates new sockets in the process). I want to be able to pass some of these new...
5
by: lugal | last post by:
This might be more appropriate here. I'm new to C++, coming from a background in another languages that allowed a similar solution to work (Python). I wrote the following code in C++ based on the...
20
by: Gregory Piñero | last post by:
Hey guys, would someone mind giving me a quick rundown of how references work in Python when passing arguments into functions? The code below should highlight my specific confusion: <code> ...
5
by: Gordon Airporte | last post by:
I'm wondering if this is might be bad practice. Sometimes when I need to pass around several pieces of datum I will put them in a tuple, then when I need to use them in a receiving function I get...
5
by: Chris Hieronymus | last post by:
Hi, I have a bunch of x-y data contained in an array. I would like to plot the data using an external program (psxy in GMT). The plotting program takes x-y couples as standard input. ...
2
by: goetzie | last post by:
I am using Python 2.4.1 and Numeric 23.8 and running on Windows XP. I am passing a Numeric array of strings (objects) to a C Extension module using the following python code: import Numeric...
2
by: Eric Carlson | last post by:
Hello, I can open, read, and convert data to a numeric (double) array from a binary file using nc = #something given nr = #something given f_o=open('junk.bin','rb')...
2
by: DwBear75 | last post by:
I am contemplating the need for a way to handle high speed data passing between two processes. One process would act as a queue that would 'buffer' data coming from another processes. Seems that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.