0

I am trying to compile a class from a String using Janino. The class contains a lambda expression inside a function but it seems to not recognize the reference operator "->" and "::".

I am getting a CompileException The complete stake trace Exception in thread "main" org.codehaus.commons.compiler.CompileException: Line 1, Column 346: Unexpected token ">" in primary

Below is the code I am using,

public class LambdaFromJanino{

      public static void main(String[] args) throws Exception
        {
            String CLASS_NAME = "Foo";
            String codeStr = "import java.util.Arrays;" + 
                        "import java.util.List;"+
                        "import java.util.stream.Collectors;"+
                        "public class " + CLASS_NAME + " {" +
                        "public static void main(String[] args) {" +
                        "System.out.println(\"Hello \" + args[0]);" +
                        "List<String> result = lambdaOut(args);"+
                        //"result.forEach(System.out::println);"+
                        "System.out.println(\"this is result \"+result.get(0));"+
                        "}" +
                        "static List lambdaOut(String[] arr)  {  " +
                        "return  Arrays.stream(arr).map( x -> x.replaceAll(\"[a-zA-Z]\", \"\"))" +
                        ".collect(Collectors.toList()); }; " +
                        "}";
            SimpleCompiler compiler = new SimpleCompiler();
            compiler.cook( codeStr ); // compile the string
            // get the loaded class
            Class<?> cl = compiler.getClassLoader().loadClass(CLASS_NAME);
            // Invoke the "public static main(String[])" method
            Method mainMeth = cl.getMethod("main", new Class[] { String[].class });
            String[] methArgs = new String[] { args[0], args[1], args[2] }; // one input
            mainMeth.invoke(null, new Object[] { methArgs });
        } 
}
6
  • Are you sure you are using Java 8 or above? Commented Aug 20, 2020 at 12:22
  • Yes, I am using Java 8 and this lambda expression works fine when called directly in main method. Commented Aug 20, 2020 at 12:26
  • Issue is not with Lambda expression, it's with assignment of List<String> result. Commented Aug 20, 2020 at 12:28
  • Even if you try like this static it's not working List<String> result= new ArrayList<String>(); Commented Aug 20, 2020 at 12:29
  • @SagarGangwal List<String> result= new ArrayList<String>(); is working fine just need to add "import java.util.ArrayList;" in imports. So if I just return the list in the function lambdaOut without any lambda expression it works fine. Commented Aug 20, 2020 at 12:49

1 Answer 1

0

Try below code, it will help to resolve your problem.

As you are not collection result me lambda expression, here i had attached \"\")).collect(Collectors.toList())\n" to resolve it.

    public static void main(String[] args) throws Exception
                {
                    String CLASS_NAME = "Foo";
                    String codeStr = "import java.util.Arrays;" + 
                                "import java.util.List;"+
                                "import java.util.stream.Collectors;"+
                                "public class " + CLASS_NAME + " {" +
                                "public static void main(String[] args) {" +
                                "System.out.println(\"Hello \" + args[0]);" +
                                "List<String> result = lambdaOut(args);"+
                                //"result.forEach(System.out::println);"+
                                "System.out.println(\"this is result \"+result.get(0));"+
                                "}" +
                                "static List lambdaOut(String[] arr)  {  " +
"return  Arrays.stream(arr).map( x -> x.replaceAll(\"[a-zA-Z]\", \"\")).collect(Collectors.toList())\n" +
                    ".collect(Collectors.toList()); }; \n" +
                    "}";
Sign up to request clarification or add additional context in comments.

1 Comment

It looks redundant.

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.