Issue related to running python script from java

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 870 Views - Last Post: 11 June 2020 - 04:26 AM Rate Topic: -----

#16 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3698
  • View blog
  • Posts: 16,952
  • Joined: 20-September 08

Re: Issue related to running python script from java

Posted 26 May 2020 - 04:51 PM

You're using that class wrongly. All you need is

    public static void main(String[] args) {
        try {
            Process p = Runtime.getRuntime().exec("python num.py");
            IOUtils.outputProcessStreams(p);
        } catch (IOException ie) {
            ie.printStackTrace();

        }

    }


This post has been edited by g00se: 26 May 2020 - 04:52 PM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#17 mfalcon   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 16-August 16

Re: Issue related to running python script from java

Posted 08 June 2020 - 12:15 PM

View PostNormR, on 26 May 2020 - 04:36 PM, said:

When I run this code on my Win10 PC
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleScriptContext;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.concurrent.atomic.AtomicReference;

/**
 *
 * @author Mostafa
 */
public class Java_Python_Test {

    /**
     * @param args the command line arguments
     */
     Process p;
    public static void main(String[] args) throws ScriptException, IOException {
    try{
        String s = null;
        Process p = Runtime.getRuntime().exec("py SimplePython.py");
        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
        BufferedReader err = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        
      
        while((s = err.readLine())!=null)
        {
//            IOUtils.collectProcessText(p);
//            System.out.println(IOUtils.collectProcessText(p));
            System.out.println(s);
             
        }
       
        while((s = in.readLine())!=null)
        {
//            IOUtils.collectProcessText(p);
//            System.out.println(IOUtils.collectProcessText(p));
            System.out.println(s);
             
        }
    }
    catch(IOException ie)
    {
        ie.printStackTrace();
        
        
        
    }
    
    }
    
}


I get the following output:

Quote

Running: java.exe -client Java_Python_Test

1
2
3
4
5
6
7
8
9

0 error(s)


I have windows 8.1. I believe it is easier to change mqtt backend that supports java in easier way, better than going through mixing python and java.

Thanks for all
Was This Post Helpful? 0
  • +
  • -

#18 nicephotog   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 34
  • Joined: 06-June 20

Re: Issue related to running python script from java

Posted 11 June 2020 - 04:26 AM

That's more a Python question.
I don't know if the Python script takes STDIN or any info in args but if you want to see output you need the Python script to output to STDOUT (whetever its called in Python).
You need to look at the Python script code to find what it does for an answer, perhaps it simple creates a file and sends the collected data to the file.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2