Search code examples
javaintegrationprocessbuilder

Cannot resolve method 'resolvePythonScriptPath'


I'm following this tutorial about integrating Java and Python.

And I keep getting Cannot resolve method 'resolvePythonScriptPath' in 'TestIntegration' what import am I missing for this method resolvePythonScriptPath() ?

This is my code:

import org.junit.Test;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.SimpleScriptContext;
import java.io.FileReader;
import java.io.StringWriter;

import static org.junit.Assert.*;

public class TestIntegration {
    @Test
    public void givenPythonScriptEngineIsAvailable_whenScriptInvoked_thenOutputDisplayed() throws Exception {
        StringWriter writer = new StringWriter();
        ScriptContext context = new SimpleScriptContext();
        context.setWriter(writer);

        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("python");
        engine.eval(new FileReader(resolvePythonScriptPath("hello.py")), context);
        assertEquals("Should contain script output: ", "Hello Baeldung Readers!!", writer.toString().trim());
    }
}

Solution

  • You should write your own method. it could be something like:

    private String resolvePythonScriptPath(String path){
       File file = new File(path);
       return file.getAbsolutePath();
    }