I have implemented a JavaScript function inside the Mongodb server using this example.
It works fine when I use mongo shell, but I want to run it from inside a Java
Since MongoDB 4.2 the eval command is removed (it was deprecated in 3.0). There is no more way to eval JavaScript scripts into MongoDB from the Java Driver.
eval
See https://docs.mongodb.com/manual/reference/method/db.eval/