• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Python Script gets called from a Java class

 
Ranch Hand
Posts: 1397
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

when I run this class calling the Python script it doesn't run and I get no error.


Thanks in advance,
 
Bartender
Posts: 9584
13
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are two output streams from a process: the "normal" output stream and the "error" output stream.  see here for some general background.
 
Angus Ferguson
Ranch Hand
Posts: 1397
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like the line below is not executing the script when debugging I get a kind of Null. I have attached an image when debugging.



InputStream.PNG
[Thumbnail for InputStream.PNG]
 
Rancher
Posts: 4271
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
exec produces a Process.
A Process has an input stream (accessed via the getInputStream method) and an error stream (accessed via the getErrorStream method).
You need to be reading from both.
 
Joe Ess
Bartender
Posts: 9584
13
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Angus Ferguson wrote: I get a kind of Null.



That appears to be an implementation detail of ProcessBuilder (see the part about process attributes and how streams are handled).
What you should probably do is handle the error stream as well as check the process exit value.
 
Angus Ferguson
Ranch Hand
Posts: 1397
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just changed the complete code adding the error handler commented in the forum.



I get these values;





 
Joe Ess
Bartender
Posts: 9584
13
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you invoke "sudo", the OS will prompt you for your credentials, so this process will not complete (unless you send the credentials to the process via an OutputStream)
Your original code didn't catch the exception, which is crucial for solving this issue.  If you remove "sudo" what exception do you get?
 
Angus Ferguson
Ranch Hand
Posts: 1397
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If you remove "sudo" what exception do you get?



Exactly the same stdout
 
Angus Ferguson
Ranch Hand
Posts: 1397
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When debugging it doesn't launch exception in fact I think that it doesn't execute the Python script...

streamProcess.PNG
[Thumbnail for streamProcess.PNG]
streamProcess1.PNG
[Thumbnail for streamProcess1.PNG]
streamProcess2.PNG
[Thumbnail for streamProcess2.PNG]
 
Joe Ess
Bartender
Posts: 9584
13
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Angus Ferguson wrote:When debugging it doesn't launch exception in fact I think that it doesn't execute the Python script...



Did you recompile after removing "sudo"?
Note the values for process (below).  You wouldn't have a PID and hasExited would not be true if the process has not executed.
However, note that exitcode is 2.  A non-zero exit code indicates a command-line error.  Is your script executable? Do you have a shebang in your Python file?  Try using the full path to the script in the argument to exec.
process.png
[Thumbnail for process.png]
 
Angus Ferguson
Ranch Hand
Posts: 1397
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Did you recompile after removing "sudo"?



Yes

Is your script executable? Do you have a shebang in your Python file?



I just added it.


Try using the full path to the script in the argument to exec.





Same app behaviour...  
 
Joe Ess
Bartender
Posts: 9584
13
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Angus Ferguson wrote:



Are you on Unix?  Because that's the wrong path separator for Unix.
 
Joe Ess
Bartender
Posts: 9584
13
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, make sure to print BOTH output streams.  I think you are only printing out the contents of the error stream.
 
Angus Ferguson
Ranch Hand
Posts: 1397
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed them for Unix.


Also, make sure to print BOTH output streams.  I think you are only printing out the contents of the error stream.





It is also curious that when I remove "python" in the exec method it doesnt find the .py file.




 
Joe Ess
Bartender
Posts: 9584
13
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


It is also curious that when I remove "python" in the exec method it doesnt find the .py file.



You are using a relative path.  You need to add a slash before "org" to make it absolute.  Also, there's no need to put two slashes.
 
Angus Ferguson
Ranch Hand
Posts: 1397
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You need to add a slash before "org" to make it absolute.



Added and still the same behavior...
 
Joe Ess
Bartender
Posts: 9584
13
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you share your current code?
 
Dave Tolls
Rancher
Posts: 4271
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This code is really hard to follow.
You get the error stream into a BufferedReader called stdError.
You then get the regular stream into a BufferedReader called reader.
You then do a while loop, reading lines from the reader (we know there are none from earlier), however you try to print some random bit of the stdError as well.

You need to do 1 loop with the reader, and one with the stdError.  Not try and mix them.
 
Angus Ferguson
Ranch Hand
Posts: 1397
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, here it goes...

The Python one starts like this:



The current Java file is:



 
Joe Ess
Bartender
Posts: 9584
13
Mac OS X Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Dave has some good points.  I don't think this code is helpful:



All that is going to print out is a class name and an address.  What you want to do is print the contents of that stream.
As Dave points out, that code is troubling:


Note that read operations block until data is available or the stream is terminated.  If the Python process hangs or is waiting for input, your Java program stops working.
I am getting inconsistent results including "python" in the command line.  Try it both ways.
There's a really good article on this subject here.  Look at the examples and they should help.
 
Angus Ferguson
Ranch Hand
Posts: 1397
3
Netbeans IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, it is fixed the path to the data was the problem.
 
I miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!