• 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
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Running python in Java

 
Ranch Hand
Posts: 74
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have some code written in python that use libraries such as pandas, numpy, sci-kit learn.
I'm wondering if there is a way to run this code in Java?

I looked into using Jython but those libraries can't be used in it as they are also written in C.

Would ProcessBuilder or Appache Commons Exec allow me to run the files? I found an article using these but it was for a very simple python program.

Any ideas would be great, thanks!
 
Bartender
Posts: 242
27
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interfacing Java with other stuff is always a pain.

The first thing I would try is running a simple Shell command from Java in order to run a command like "python myScript.py".
I've written this code in the past to run a shell command in java. I've used this to run C++ apps from a Java program.
 
Sheriff
Posts: 22057
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although that code tries to read from both the output stream and the error stream, it's still prone to deadlock. If the process outputs a lot to its error stream, and the error stream buffers become full, your code is still trying to read from the output stream. However, there's nothing coming to that because the process is blocked on its error stream, which you're not consuming yet. It also won't end because the process doesn't end.

There are two solutions:
1) Do the reads in two threads, one for the output and one for the input. This is what When Runtime.exec() won't teaches us. Be careful about interleaving the two though.
2) Use ProcessBuilder and call redirectErrorStream(true). You'll then only need to read from the process' output stream.

I prefer the latter, because it lets the JVM and/or OS deal with merging the two streams.
 
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic