1

Below is the given code for calling Python from Java

public static void main(String args[]) {
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new test1( ).setVisible(true);
        }
    });
    try {
        PythonInterpreter.initialize(System.getProperties(), System.getProperties(), new String[0]);
        PythonInterpreter interp = new PythonInterpreter();
        interp.set("firstName", args[0]);
        interp.set("lastName", args[1]);
        interp.execfile("‪C:\\Users\\priyank\\Desktop\\pythontest.py");
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

I am getting the following exception:

java.lang.ArrayIndexOutOfBoundsException: 0" error.. 

Why do I get this error?

3 Answers 3

0

A pipe can be a better solution. Have a look at this thread:

java: how to both read and write to & from process thru pipe (stdin/stdout) .

Sign up to request clarification or add additional context in comments.

2 Comments

I did not understand this example, sorry I am new to Java and Python
What did you not understand?
0

It looks like you do not pass any arguments, when you start your application. The args-array is empty (size = 0), but then you are trying to access the first element (index 0), which does not exist.

1 Comment

I have added two string to it, still i am getting the same error, please help me rewriting it, and if you can please tell me how to pass parameters to python from java.
0

Change the values to

interp.set("firstName", args[1]);
interp.set("lastName", args[2]);

it will work, as the first value is the python script file name but we are not adding that, please check that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.