简体   繁体   中英

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

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);
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM