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

Read System.out.println From Java Using popen ?

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
2 2620
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: somebody | last post by:
I have several web servers with SSL certificates installed. My plan is to write a java program which will grab the certs via ftp, then read (parse) the certificate information. Ultimately what I...
1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
0
by: Stuart Miller | last post by:
When using Java to read in an XML file, I am having problems getting the XML file to be parsed against the specified DTD. Background: I have an existing Java application that allows users to...
1
by: Geethu03 | last post by:
Hi In my project i want to convert the input file to the .doc format. I convert the text file into the .doc file by using the below code import java.io.*; public class test1 { public...
0
by: lovecarole | last post by:
hi, i am the student who should write a program about reading wav file and do the DFT. actually i don't know how to read data of the wav song and save it into the array... if i want to read...
8
by: Jromero | last post by:
My code Another class = new Student; /* public static void copyLines() throws IOException { BufferedReader inputStream = null; PrintWriter outputStream = null; ...
0
by: svpriyan | last post by:
Hai Implemented a code that will read the 100s of files from a directory. Each time when i read a file , i will take that file and check the content of the file( i put them in to a List )and ...
0
by: svpriyan | last post by:
hai, I could not understand how to debug the error i have now. target 1- read the files from directory // i able ot do this part 2- for each file for each file read the content &...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.