Anvi
Anvi

Reputation: 2741

Java Runtime.getRuntime().exec Executing only first line

I want to execute a python script from java. The code is getting in to the python file but only executes first line of the file.

following is the code:

Process p = Runtime.getRuntime().exec("python "+dir+"/pyfiles/testfile.py");
        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
        value = in.readLine();

after the first line nothing is executed. what is the solution?

'dir' value is getting from

final String dir = System.getProperty("user.dir");

link to python file:

https://drive.google.com/file/d/1tvkFTM_Oo5gTS7FyzeNgoeY5DLitFQjD/view?usp=sharing

Upvotes: 0

Views: 235

Answers (2)

Anvi
Anvi

Reputation: 2741

it worked fine when I passed like this:

String cmd = "python2.7 "+dir+"/pyfiles/getGitFiles.py "+ownerVal+" "+repoVal+" "+folderVal+" "+branchVal+" "+Values.accessToken;
System.out.println(cmd);
Process p = Runtime.getRuntime().exec(cmd);

passing the arguments inside the 'exec' itself is causing the problem.

Upvotes: 0

kAliert
kAliert

Reputation: 768

The problem seems, that you are only reading the first line of your BufferedReader. So change your code as follows:

Process p = Runtime.getRuntime().exec("python "+dir+"/pyfiles/testfile.py");
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));

while ((line = in.readLine()) != null)
{
     System.out.println(line);
}

Upvotes: 1

Related Questions