Let's say I've compiled a Groovy script using Groovyc, which has generated one or more .class files in the file system. From a Java application, how do I add those classes to the classpath dynamically in order to load them and call their methods? The goal is to pre-compile Groovy scripts and store them into the database, so evaluation can be performed from compiled versions of the scripts.
Loading classes not present in the classpath
4.9k Views Asked by Tiago Fernandez At
2
There are 2 best solutions below
1
Alexander Pogrebnyak
On
You need to write your own classloader.
This javadoc link has an example of how you can define a custom one.
Related Questions in JAVA
- Add image to JCheckBoxMenuItem
- How to access invisible Unordered List element with Selenium WebDriver using Java
- Inheritance in Java, apparent type vs actual type
- Java catch the ball Game
- Access objects variable & method by name
- GridBagLayout is displaying JTextField and JTextArea as short, vertical lines
- Perform a task each interval
- Compound classes stored in an array are not accessible in selenium java
- How to avoid concurrent access to a resource?
- Why does processing goes slower on implementing try catch block in java?
- Redirect inside java interceptor
- Push toolbar content below statusbar
- Animation in Java on top of JPanel
- JPA - How to query with a LIKE operator in combination with an AttributeConverter
- Java Assign a Value to an array cell
Related Questions in GROOVY
- spring-integration-dsl-groovy-http return null when i use httpGet method
- groovy xml namespace definition used in attribute value lost after XmlParse/serialize
- jenkins with groovy postbuild .Not able to execute anything in groovy script field
- How can I set the the expected Exception type for a catch statement with a parameter I've passed into a method?
- How to add quotes into sql where clause in Groovy script?
- integrating groovy with api
- java.util.ConcurrentModificationException on cloneEntity
- jenkins (or groovy) using pom.xml from previous execution
- How to use multiple classes in multiple files in scripts?
- How to work around Groovy's XmlSlurper refusing to parse HTML due to DOCTYPE and DTD restrictions?
- Workaround for lack of generators/yield keyword in Groovy
- Groovy's @CompileStatic and map constructors
- Java syntax to Groovy syntax
- Groovy TimeDuration Argument Types
- Elasticsearch : _score always 0 in Groovy script
Related Questions in CLASSPATH
- maven pom.xml dependencies order vs classpath/build path order
- Derby, Java: Trouble with "CREATE_TYPE" statement
- Can I use \ and / to be directory delimiter in CLASSPATH of .bat
- Java FrameDemo Error: Could not find or load main class
- How to set resources directory in eclipse dynamic web project?
- Absolute filesystem path for an IClasspathEntry
- Scala classpath not finding default package class in local directory
- NoClassDefFoundError with Kryo
- adobe pro cc (AS3) not recognizing box2d classpath
- Hadoop Job class not found
- when writing spock test in groovy java mixed project unable to resolve class
- Importtsv command gives : Container exited with a non-zero exit code 1 error
- WEKA API LibSVM ClassPath not found
- Non user specific path operations?
- Getting a List of files from a class path entry
Related Questions in CLASSLOADER
- Get full path of a package situated in source folder from junit
- ClassLoader: Treating a class as a resource - safe or implementation detail?
- Eclipse/Java can't start any project
- Can java string literals be garbage collected?. If Yes, how to prove it?
- Java override class to Load project Class instead Jar class
- Javamail ClassCastException when sending multipart messages
- How to use PowerMock with Arquillian?
- How do I install a servlet in a tomcat container and have it loaded into each web app's context?
- How do I print the current classpath for a Jenkins plugin?
- UrlClassLoader scope
- The autoloader expected class tobe defined in file
- ClassNotFoundException when load class with Class.forName
- Rome 0.9 does not work correctly when module classloader order : parent last
- Confusing ClassNotFoundException when instantiating JAXBContext with packageName and Classloader
- ClassNotFoundException on android blank activity
Related Questions in GROOVYCLASSLOADER
- lookupScriptFiles "true" in the loadClass() method
- How can I evaluate my own Groovy script from Java?
- GroovyClassLoader parseClass security
- Replace classpath of a running Groovy Script (Jenkins Pipeline script)
- GroovyCastException while running java code containing new line "\n" with groovy script engine(GroovyClassLoader)
- How can I execute groovy scripts in an isolated classloader?
- How compile Groovy source but not from filesystem
- Can't unloaded Groovy classes - PermGen Erros
- How to cast an object in class loaded in classloader
- Groovy: re-compile a class from file and memory leaks
- Run groovy script from within gradle
- How to use GroovyClassLoader to load GroovyCompiled classes or how to set delegate in case of loadClass
- Java : GroovyClassLoader : cannot find symbol for method in Groovy Class
- GroovyClassLoader call to parseClass is successful, even when code does not compile
- How to Store the Compiled Groovy Script in Database and can be fetched when needed?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
You can create an instance of URLClassLoader to load new classes from a directory:
Line 1 creates the
URLto the directory where the .class files reside.Line 2 creates a new
URLClassLoaderinstance. First argument is an array of URLs to be used as the source. You can specify multiple directory URLs within the array. Second argument is the classloader that will become the parent of this new classloader. We pass the classloader of the class executing the above code as this argument.The classes loaded by a child classloader can access the classes loaded by the parent classloader.