How to execute a python file with few arguments in java?

+5 votes

String command:

python FileName.py <ServerName> userName pswd<b>

Process p = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
    System.out.println(line + "\n");
}

Code is not terminating nor giving the results

Mar 27, 2018 in Java by DareDev
• 6,810 points
11,441 views

3 answers to this question.

+1 vote
Best answer

You can use Java Runtime.exec() to run python script, As an example first create a python script file using shebang and then set it executable.

#!/usr/bin/python
import sys
print ('Number of Arguments:', len(sys.argv), 'arguments.')
print ('Argument List:', str(sys.argv))
print('This is Python Code')
print('Executing Python')
print('From Java')

if you save the above file as script_python and then set the execution permissions using

chmod 777 script_python

Then you can call this script from Java Runtime.exec() like below

import java.io.*;
import java.nio.charset.StandardCharsets;

public class ScriptPython {
       Process mProcess;

public void runScript(){
       Process process;
       try{
             process = Runtime.getRuntime().exec(new String[]{"script_python","arg1","arg2"});
             mProcess = process;
       }catch(Exception e) {
          System.out.println("Exception Raised" + e.toString());
       }
       InputStream stdout = mProcess.getInputStream();
       BufferedReader reader = new BufferedReader(new InputStreamReader(stdout,StandardCharsets.UTF_8));
       String line;
       try{
          while((line = reader.readLine()) != null){
               System.out.println("stdout: "+ line);
          }
       }catch(IOException e){
             System.out.println("Exception in reading output"+ e.toString());
       }
}
}

class Solution {
      public static void main(String[] args){
          ScriptPython scriptPython = new ScriptPython();
          scriptPython.runScript();
      }

}
answered Mar 27, 2018 by DragonLord999
• 8,380 points

edited Nov 6, 2018 by Omkar
0 votes

There are three ways to get this done:

  • runtime approach
  • process approach
  • jython approach

Have a look at this blog for detailed explanation with example.

answered Oct 12, 2018 by Hannah
• 17,150 points
Sorry None of those approaches are usefull
Hey there! Share the code you have used and the error you are getting..

This is the code that worked for me:

yes this solution is working fine
0 votes
how to read pandas dataframe in java
answered Jan 3 by anonymous
Hey! It would be better if you post this as a new question. Because your question is irrelevant to this thread. And also, by posting this as a new question, more people will be able to help you.

Related Questions In Java

0 votes
3 answers

How to read a Text File in Java?

You can use readAllLines and the join method to ...READ MORE

answered Jul 27, 2018 in Java by samarth295
• 2,190 points
130 views
0 votes
3 answers

How to check whether a file exists or not in Java?

Using nio we can check whether file ...READ MORE

answered Aug 14, 2018 in Java by Sushmita
• 6,880 points
295 views
0 votes
2 answers

How to read a text file in Java?

You can use Scanner class to read ...READ MORE

answered Aug 9, 2018 in Java by parth295
• 4,640 points
63 views
0 votes
1 answer

How to run a JAR file in Java?

The command given below will help you ...READ MORE

answered Jun 1, 2018 in Java by parth295
• 4,640 points
1,353 views
0 votes
2 answers

How to left pad a string with zero in java?

String paddedString = org.apache.commons.lang.StringUtils.leftPad("129018", 10, "0") the second ...READ MORE

answered Aug 31, 2018 in Java by Sushmita
• 6,880 points
593 views
0 votes
2 answers

How to move or copy a file in Java

The new JAVA Specification Request 203 -NIO will ...READ MORE

answered Aug 20 in Java by Sirajul
• 41,000 points
47 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 416 views