473,322 Members | 1,431 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,322 software developers and data experts.

Executing Python interpreter from Java

Hello,
I'm trying to write some Java code that will launch a python
interpreter shell and pipe input/output back and forth from the
interpreter's i/o streams and the Java program. The code posted below
seems to work just fine under Mac OS X; however, under Windows XP
(Java 1.6 and Python 2.5 installed) the program hangs when I give a
command to import the Tkinter (Python GUI library) and create a top-
level frame window.

Specifically, run the program as:
% java PythonShell
..... (loads Python and displays welcome message)
>>from Tkinter import *
t = Tk()
After this point a frame window pops up but anything you type further
doesn't evoke any further response from the shell. There is a little
hiccup that occurs when the Tkinter library is actually loaded (on
both the Mac or Windows OS) and the window in which the java program
is running loses the focus-- I guess that has something to do with the
way the Tk windowing library is loading. I know on the Java
documentation for Process it says that it may not work correctly with
native windowing libraries on some OSs. If this is what is happening
here, does anyone have any suggestions?

(Btw, I know about Jython and I do not want to use that for a whole
lot of reasons.)

Thanks a whole lot!

nadeem
import java.io.*;
import java.util.*;

public class PythonShell {

// these are out here so the inner class can access them...
static BufferedReader in = null;
static boolean ok = true;

public static void main(String[] args) throws InterruptedException {
ProcessBuilder pb = new ProcessBuilder("python", "-i"); // force
interactive
pb.redirectErrorStream(true); // combine stdout and stderr

Process p = null;
PrintStream out = null;
boolean started = false;
try {
p = pb.start();
started = true;
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
out = new PrintStream(p.getOutputStream(), true);
}
catch (IOException exc) {
exc.printStackTrace();
if (started) p.destroy();
return;
}

final Scanner userIn = new Scanner(System.in);
final Thread mainthread = Thread.currentThread();

class ReaderThread implements Runnable {
public void run() {
try {
while (true) {
int c = in.read();
if (c == -1) {
ok = false;
break;
}
System.out.print((char)c);
}
}
catch (IOException exc) {
ok = false;
}

// try to interrupt the other thread
userIn.close();
try {
System.in.close();
} catch (IOException exc) {}
mainthread.interrupt();
}
}

Thread rt = new Thread(new ReaderThread());
rt.start();

while (ok) {
try {
String input = userIn.nextLine();
out.println(input);
System.out.println(ok);
}
catch (NoSuchElementException exc) {
ok = false;
}
}

p.destroy();
p.waitFor();
}
}
/*
Input:

from Tkinter import *
t = Tk()
print 'hi'

*/

May 21 '07 #1
0 1608

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

Similar topics

36
by: Tim Churches | last post by:
If a compiled Python extension module B includes code from some other software A which is licensed only under the GPL, do other Python programmes, C, which import module B also need to be licensed...
0
by: vincent Salaun | last post by:
hi all, here's my problem : I've embedded a python interpreter in our java application (based on the NetBeans palteforrm) using the Jython API : http://www.jython.org/docs/javadoc/index.html...
58
by: Svein Ove Aas | last post by:
Is anyone working on a python-to-native compiler? I'd be interested in taking a look. Come to think of it, is anyone working on a sexpr-enabled version of Python, or anything similar? I really...
53
by: Krystian | last post by:
Hi are there any future perspectives for Python to be as fast as java? i would like to use Python as a language for writing games. best regards krystian
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.