By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,957 Members | 2,017 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,957 IT Pros & Developers. It's quick & easy.

Read System.out.println From Java Using popen ?

P: n/a
Hello All,

I'm relatively new to Python programming but have been working on this
problem for a little bit now ... I initially began writing UNIX scripts
in Python and thought it was the greatest because I could do the
following very easily :

pswwaux = os.popen( "ps wwaux | grep /usr/sbin/httpd" )
for line in pswwaux.readlines() :
print line.strip()

This made writing wrapper scripts for restarting servers ( and handling
all the errors that may occur on a restart ... ) a snap ... now I'm
trying to make a call to a Java service that utilizes
System.out.println to say whether the action was performed, or if there
were errors or any other piece of information ... I'm having issues
reading what comes out of System.out.println, and I'm beginning to
wonder if my Java service is being called at all and if so, is it even
possible to capture System.out.println ? Here's the gist of my call to
the Java service:

result = os.popen( "%s/bin/java com.foo.service.JavaService %s" % (
JAVA_HOME, FILE_TO_CREATE ) )

print
result.readlines()

The result.readlines() always results in this output: [] Furthurmore,
the file that is supposed to be created is not created ... If I replace
the result = os.popen with a print, and then copy what the script
prints out and run that ( straight up on the command line ), the file
is created and the expected output is spit out ...

It should be noted that I'm trying to loop over a list of files and
create a Thread for each action since the Java service can take a bit
of time to run :

for id in ids :
thread = threading.Thread( target=export, args=( [ id ] ) )
thread.start()

So I guess my questions are:

1) Am I executing the Java service the correct way
2) Am I capturing the System.out.println from the Java service the
correct way

Any help would be greatly appreciated

Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 15 Jun 2005 12:45:56 -0700, "Your Friend"
<my**************@gmail.com> declaimed the following in
comp.lang.python:

wonder if my Java service is being called at all and if so, is it even
Does this service /exit/... It may be that you won't have data
to read unless the service completes, and only then is the output
released to you.
-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 19 '05 #2

P: n/a
Your Friend wrote:
result = os.popen( "%s/bin/java com.foo.service.JavaService %s" % (
JAVA_HOME, FILE_TO_CREATE ) )

print
result.readlines()

I find that subprocess.Popen works _better_ for this kind of thing.

import os
from subprocess import Popen

outfile = open("out.txt","w")
errfile = open("err.txt","w")
pid = Popen([os.environ["JAVA_HOME"]+/bin/java,
"-classpath",
".",
"MyJavaExecutable"],
stdout=outfile,
stderr=errfile)
..
..

/rune
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.