0

The problem

I was attempting to pass a python script (embedded as a String in a Java file, NOT as it's own script file), to the cpython interpreter from Java. I called runtime.exec("python -c \"print('Hello World')\""), which succeeds in Windows, but not Linux.

This only occurs in Java. Executing python -c "print('Hello Word')" succeeds in the cli for both Windows and Linux.

I suspect it has something to do with either how Java's Runtime class handles runtime.exec, or how bash handles the piping or whatnot. Anyone have any ideas about why this happens / how to get it to work?

Windows env:

  • 10.0.16299 N/A Build 16299
  • Python 3.6.4
  • Java oracle jdk 9.0.4

Linux env:

  • Raspbian 8 (jessie)
  • Python 3.4.2
  • Java oracle jdk 1.8.0_161 (arm32)

Sample java:

import java.io.IOException;

public class Test {

    public static void main(String[] args) throws IOException {
        Runtime runtime = Runtime.getRuntime();

        String py = "open('test', 'w').write('HAI')";

        // If linux, sub for "python3 -c ..."
        runtime.exec("python -c \"" + py + "\"");
    }
}

I changed it to write to a file in case stdin or stderr were being redirected funnily.


What I am trying to achieve

This isn't part of the original question, but I figured you might want to know why I would be doing this.

I've got an Adafruit DHT22 attached to the pi at gpio 24. The libraries for this are only written in Python (the ones not for arduino anyway). Jython is out of the question since the sensor needs microsecond timing to read, and the python libraries use external C libraries as well. We already have 99% of the code written in Java; we just need a way to get the T&H data from the dht22 to the Java application.

Jepp would be perfect for this, except I couldn't get the pip install to work on Windows (Visual C++ 14 required, I've tried everything I could google), and I kept getting ClassNotFoundException when calling Jep jep = new Jep(false); in Linux

I have a solution that will work, which is to write the script to a file, then call runtime.exec("python script.py"). This will work; however, since the Java application will be running as a jar file, I would have to manually write the file from the code (rather than pre-written script.py in the java source). I was hoping for a more elegant solution, but if push comes to shove, it will have to do.

Again, this is not relevant to the original question; it's just the "why would you need to do that"

Lightfire228
  • 546
  • 1
  • 5
  • 17
  • For writing the script to a file, you can have the file in the compiled jar and copy it using `YourClass.class.getResourceAsStream(file)` – phflack Mar 15 '18 at 16:45
  • For having one command that works on Windows and Linux, it may be a bit more difficult given one will use cmd and the other bash. You may need to [detect which platform you're running on](https://stackoverflow.com/q/228477/5475891) and then execute a command based on that – phflack Mar 15 '18 at 16:47

0 Answers0