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
- I need the BIRT.war that is compatible with Java 17 and Tomcat 10
- Creating global Class holder
- No method found for class java.lang.String in Kafka
- Issue edit a jtable with a pictures
- getting error when trying to launch kotlin jar file that use supabase "java.lang.NoClassDefFoundError"
- Does the && (logical AND) operator have a higher precedence than || (logical OR) operator in Java?
- Mixed color rendering in a JTable
- HTTPS configuration in Spring Boot, server returning timeout
- How to use Layout to create textfields which dont increase in size?
- Function for making the code wait in javafx
- How to create beans of the same class for multiple template parameters in Spring
- How could you print a specific String from an array with the values of an array from a double array on the same line, using iteration to print all?
- org.telegram.telegrambots.meta.exceptions.TelegramApiException: Bot token and username can't be empty
- Accessing Secret Variables in Classic Pipelines through Java app in Azure DevOps
- Postgres && statement Error in Mybatis Mapper?
Related Questions in GROOVY
- No tests found for given includes: [com.bright.TwitterAnalog.AuthenticationControllerSpec.Register user with valid request](--tests filter)
- Groovy - how to read properties from a property file in a jar on the classpath
- How to enter user input in jmeter console (without doing parameterization)
- Post checkmarx scan weblink on the Jenkins Build status page
- Optimal way to replace different parts of a structured string(XML) with random string values of different lengths in a JMeter JSR223 Samper (groovy)
- How to compare two csv files line by line in apachi nifi flow file using groovy script
- Jenkins pipeline map is getting converted to an array but it's too large I think?
- Exception: Unable to start the daemon process. This problem might be caused by incorrect configuration of the daemon
- Passing varible in Groovy script
- Multibranch pipelines not working if it's created by job DSL
- How to fetch more than 50 items when executing nexus rest api get request
- Grails Method is executing asynchronously even when i have not applied any code for asynchronous execution
- Liquibase configuration in Gradle conventions plugin
- Cannot instantiate Groovy class, throwing no signature of getBinding()
- Javascript (asset) missing in browser's sources
Related Questions in CLASSPATH
- Having trouble executing my program from a jar, using Jinput
- java.lang.ClassNotFoundException: org.apache.jsp.WEB_002dINF.jsp.ImportTab_jsp
- Failed to sample data: com.google.protobuf.Descriptors$DescriptorValidationException: ".google.protobuf.Timestamp" is not defined
- Could not resolve all files for configuration ':sqflite:classpath'
- Intellij classpath add path and shorten
- Where to put resources and access it in Java Project (in IntelliJ)? How to read from it then?
- Gradle Multi-Module Build with custom source sets: Missing Classes in Compile Classpath of dependent modules
- Cant connect to sqlite databases in java using java.sql
- What is the recommended way to access file inside test/resource folder from karate features file inside test/java/karate
- How do I run this GitHub java project?
- Trouble Adding ANTLR to Classpath in Java: "package org.antlr.v4.runtime.tree does not exist"
- How can Child jar access a resource present on parent Jar's classpath?
- ClassNotFoundException with Java's -cp on Mac OS X
- Build failed with an exception (2 errors)
- Lambda Spring boot converting html to pdf
Related Questions in CLASSLOADER
- Unable to load Subject segmentation service of com.google.android.gms
- How to use your own Bouncy Castle version in an application deployed on JBoss EAP 7.4
- Loading Jar into classpath which is not defined in pom.xml
- Override classes present in Spring Boot fat jar with classes in external jar present in loader.path
- How to get SystemClassLoader URLs in JDK17 gracefully?
- Maven plugin: common dependency in plugin and project, create dependency class object in plugin from class in project
- Getting 404 when accessing classes annotated with @Path in {war}/WEB-INF/classes in Jetty 9
- Translet class loaded, but unable to create translet instance
- jvm classloading issue (defineClass1)
- How to dynamically load a jar in runtime (Not for reflection)
- TomEE - How does ClassLoader load from 3 webapps
- Spring boot jar loading
- how to load JPA Entities and Repositories in sprint boot app from external folder/jar
- loading constraint violation when resolving method "javax/imageio/metadata/IIOMetadata.getAsTree(Ljava/lang/String;)Lorg/w3c/dom/Node
- Generate custom java code like lombok at compile time
Related Questions in GROOVYCLASSLOADER
- Missing Jenkins functions inside loaded groovy class
- How to use the `addClasspath()` method in GroovyClassLoader
- How to use GroovyClassLoader to load GroovyCompiled classes or how to set delegate in case of loadClass
- How to Store the Compiled Groovy Script in Database and can be fetched when needed?
- GroovyClassLoader call to parseClass is successful, even when code does not compile
- Java : GroovyClassLoader : cannot find symbol for method in Groovy Class
- How do I confirm that I don't have duplicate classes within my ClassLoader structure?
- Running and optimizing Groovy Scripts dynamic execution in Java application
- No suitable ClassLoader found for grab wenn starting from SCM jenkins pipeline
- Groovy class can't locate superclass in the same package
- GroovyCastException while running java code containing new line "\n" with groovy script engine(GroovyClassLoader)
- Groovy: re-compile a class from file and memory leaks
- How can I execute groovy scripts in an isolated classloader?
- Loading groovy classes with conflicting names
- Configure GrrovyClassLoader to honor @CompileStatic annotation
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 # Hahtags
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.