Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8635055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:52:44+00:00 2026-06-12T09:52:44+00:00

Is it possible to define some custom precompiled function with MVEL and use one

  • 0

Is it possible to define some custom precompiled function with MVEL and use one in another compiled expressions?
If I try the following:

MVEL.compileExpression("function hello() { System.out.println(\"Hello!\"); hello(); return x * y;");

I get Exception in thread "main" [Error: duplicate function: hello] when try to execure it second time.

It looks like I should declare the function not at the expression itself, but some kind of context. I played with ParsedContext for a while, but always got Exception in thread "main" [Error: unable to access property (null parent): hello].

Internet is full of tutorials that cover different MVEL language usage topics, but such embedding topics a very poorly documented. Could you please give me some clue what I’m doing wrong?

P.S. I don’t want to execute some function from Java. I need to define them dynamically with MVEL.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:52:45+00:00Added an answer on June 12, 2026 at 9:52 am

    This is a bit trickier than some common use-cases, although it is possible.

    Fundamentally, what you need to do is create a script which defines your functions, like so:

    VariableResolverFactory functionFactory = new MapVariableResolverFactory();
    MVEL.eval("def foo() { System.out.println("foo"); }; def bar() { System.out.println("bar") };", functionFactory);
    

    Then, what you do is back-chain this factory to any expression you run:

    VariableResolverFactory myVarFactory = new MapVariableResolverFactory();
    myVarFactory.setNextFactory(functionFactory);
    
    Serializable s = MVEL.compileExpression("foo(); bar();");
    
    MVEL.executeExpression(s, myVarFactory);
    

    Functions are recorded as references on the variable table, so you can use regular variable factory back-chaining to share them in MVEL.

    Note: You shouldn’t re-share ‘myVarFactory’. You want to create a new one every time. You only want to recycle ‘functionFactory’ and back-chain it to the per-run factories as shown above. Otherwise you’ll end up sharing local var state between executions — and that’s not thread-safe

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to define some sort of capitalize() or toLowerCase() function when creating
Is it possible to use some thing like this in Objective-C: #define number_of_items 10
Is it possible to define a custom attribute for a raphael element? e.g. r.circle(25,50,10).attr({fill:'#b71e16',
Does anyone know if it possible to define the equivalent of a java custom
I would like to be able to define and use a custom type in
Possible Duplicate: Define a method that has many (or infinite) arguments I have the
Possible Duplicate: Is it possible to define enumalpha? Is there any equivalent of Java
Is it possible to define a binding between an exchange (direct or topic) and
Is it possible to define an interface (e.g. MyClass Implements MyInterface) whose method/property definitions
It's possible to define an alias in C# like this using kvp = System.Collections.Generic.KeyValuePair<string,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.