0

I am passing string values to a python script from java. In my python script i am using the parameters but i could not able to read the 2 values.Plz help

Java Code:

String filePath = "E:\\Project_ivin\\test.py";  
ProcessBuilder pb = new ProcessBuilder().command("python", "-u", filePath, ""+issueId+""+comments);        
Process p = pb.start(); 

python script:

sys.argv[1] # issueid

sys.argv[2]  # comments
3
  • I am able to read 1 argument but if i try to read second one i could not able to read it..Pl help Commented May 1, 2020 at 11:27
  • how your are accessing value in python ?? Commented May 1, 2020 at 11:32
  • A blank must be between: issueId+" "+comments Commented May 1, 2020 at 11:34

1 Answer 1

0

Seems comma is missing, else use 3rd argument.

Java:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Main {
  public static void main(String[] args) throws IOException, InterruptedException {
    String[] list = { "python", "-u", "in.py", "issueId", "comments" };
    ProcessBuilder pb = new ProcessBuilder().command(list);
    Process p = pb.start();
    System.out.println("" + pb.command());
    p.waitFor();
    BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
    String line;
    StringBuilder sb = new StringBuilder();
    while ((line = br.readLine()) != null)
      sb.append(line);
    System.out.println(sb.toString());
  }
}

Python:

# print(sys.argv[0]) // this will be file name

print(sys.argv[1])
print(sys.argv[2])
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for you reply. I am getting the arguments but when i assign those arguments to some variable i am getting error Ex: issueId=sys.argv[1] and comment=sys.argv[2]
what is the error ??

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.