0
import org.python.util.PythonInterpreter;
public class JythonTest {
public static void main(String[] args) {
    PythonInterpreter interp = new PythonInterpreter();
    interp.exec("if 2 > 1:");
    interp.exec("   print('in if statement!'");
}
}

I need to be able to execute Python code from a Java program, so decided to try out Jython, but I'm unfamiliar with it. I tried executing the above code, but got the error: "Exception in thread "main" SyntaxError: ("mismatched input '' expecting INDENT", ('', 1, 9, 'if 2 > 1:\n'))". Any ideas what this means or how I can otherwise execute an if statement using the PythonInterpreter?

thefourtheye
  • 233,700
  • 52
  • 457
  • 497
Louis Slater
  • 17
  • 1
  • 4

2 Answers2

1

Conditionals must be entered as a single string and you have an extra parenthesis:

import org.python.util.PythonInterpreter;

public class JythonTest {
    public static void main(String[] args) {
        PythonInterpreter interp = new PythonInterpreter();
        interp.exec("if 2 > 1: print 'in if statement!'");
    }
}
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
0

Rather than executing a script line by line with strings you can invoke the interpreter to run a file. All you have to do is provide a file path to your python file, in this example place script.py in the src folder.

script.py

if 2 > 1:
    print 'in if statement'

JythonTest.java

import org.python.util.PythonInterpreter;

public class JythonTest {
    public static void main(String[] args) {
        PythonInterpreter interp = new PythonInterpreter();
        interp.execfile("src/script.py");
    }
}
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
  • Why would you add a new answer instead of editing it into your existing one? – Makoto Feb 01 '15 at 17:31
  • Because they're two separate ideas. Why do you think stack exchange would add such a feature? – Malik Brahimi Feb 01 '15 at 17:34
  • If I do this, will I be able to input to the program whilst it is running e.g for a python program like this: `name=raw_input() print('Hi '+name)` If so, how? – Louis Slater Feb 01 '15 at 17:37
  • Just tried the following code: [link](http://pastebin.com/xUwfsRcw) And got the error: Exception in thread "main" SyntaxError: ("no viable alternative at input '='", ('test1.py', 1, 10, 'print(name=raw_input()\n'))". – Louis Slater Feb 01 '15 at 17:50
  • That's not even proper python. You have two statement in a print statement. – Malik Brahimi Feb 01 '15 at 18:05
  • Woops sorry just put 'print' in the wrong place. Should be: `out.write("name=raw_input()\nprint('Hi '+name)");` When I run that, nothing happens. Think it just gets stuck waiting for the input. Any ideas? – Louis Slater Feb 01 '15 at 18:10
  • Yes, why write the python programmatically? Why don't you just type it yourself and place it in the folder as I described. – Malik Brahimi Feb 01 '15 at 18:19
  • Did you put the python file in the source folder and call `interp.execfile("src/script.py")`? – Malik Brahimi Feb 01 '15 at 18:31
  • See one of my previous answers [here](http://stackoverflow.com/questions/28120716/access-a-python-file-in-a-jython-jar-from-eclipse/28120725#28120725). – Malik Brahimi Feb 01 '15 at 18:50
  • That doesn't help with this I'm afraid. I just need a way to give arguments to the PythonInterpreter. I've just seen that PythonInterpreter has a setOut() method, which looks like it could do the trick, but I'm not sure how to use it. Do you know how I can? Thanks for all your help btw! – Louis Slater Feb 01 '15 at 19:03
  • @MalikBrahimi Please see my last question^ – Louis Slater Feb 02 '15 at 18:23
  • I'm sorry Louis. I have no idea about the `setOut` method, I'm just as unfamiliar with Jython as you are. – Malik Brahimi Feb 02 '15 at 18:51