1

I am calling a Python script from Java by mentioning the Python script name in the ProcessBuilder method within Java code. Code snippet for the same is given below :

String[] commandToProcessDocument = { "python", "C:\\Users\\User_Name\\sample_integration.py"};
ProcessBuilder pb = new ProcessBuilder(commandToProcessDocument);
Process process = pb.start();

And the Python script has the following lines in it :

import pandas as pd

if __name__ == '__main__':

    print("Welcome To Python")

    a = 5
    b = 10

    c = a+b

    print(c)

    df = pd.read_csv("Test_Dataset.csv")
    print("came after reading")

When I execute this python script either in Spyder IDE or in Command Prompt, I am able to see the 3rd print statement which is "came after reading". Attached is the screenshot that shows the Command Line outputenter image description here

But when I try to execute the same python script from Java using Netbeans IDE, the execution doesn't go beyond the pd.read_csv line. It just prints "15" which is the value of sum of 'a' and 'b' variables and keeps on getting executed without ending.

Is there anything I need to do specially for invoking Pandas from java or am i missing anything very basic here ?

JKC
  • 2,498
  • 6
  • 30
  • 56
  • The difference is the "working-directory" from where you execute the script. From cmd it's `C:\Users\%username%` and from java it's something else. Try execute `cd` in cmd and java to see the difference. See also: [How to set working directory with ProcessBuilder](https://stackoverflow.com/q/8405723/8097737) –  Nov 30 '17 at 06:42
  • @devpuh I set working directory by referring the post you just mentioned. It worked like a charm. Thank you so much :) – JKC Nov 30 '17 at 07:02

0 Answers0