Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding new Java class at runtime

Tags:

java

I have a functionality that I wish to provide to a customer for a software mockup that we are preparing - and I want to know if it's

  1. possible
  2. intelligent (a.k.a. not stupid)
  3. the best thing

I want the customer to be able to write a java class that implements my Computable interface and stick it in some predetermined folder. This folder will contain the .java files rather than .class files. Then, at runtime, I want my program to search that folder and extract all of the Computables from that folder and store them in a map from the name of the Computable to the Computable object. The Computable should only have a default constructor and the it interface will only have one method called compute which maps an array of Object to an Object.

like image 793
JnBrymn Avatar asked Jul 22 '10 14:07

JnBrymn


People also ask

How does the Java Runtime Environment find classes?

How the Java Runtime Finds Classes. The JVM searches for and loads classes in this order: Bootstrap classes, which are classes that comprise the Java platform, including the classes in rt. jar and several other important JAR files.

Can we create a class at runtime in Java?

Dynamic Class creation enables you to create a Java class on the fly at runtime, from source code created from a string. Dynamic class creation can be used in extremely low latency applications to improve performance.

What is getClass () getName () in Java?

Java Class getName() Method The getName() method of java Class class is used to get the name of the entity, and that entity can be class, interface, array, enum, method, etc. of the class object. Element Type.


4 Answers

The Java Compiler API introduced in Java SE 6 should give you what you need.

like image 56
djna Avatar answered Oct 12 '22 02:10

djna


You may find Google Reflections useful to find classes implementing/extending a certain interface/superclass in the classpath. It's then as straightforward as

Reflections reflections = new Reflections("my.project.prefix");
Set<Class<? extends SomeClassOrInterface>> subTypes = reflections.getSubTypesOf(SomeClassOrInterface.class);

Then, to test if it indeed has a no-arg default constructor, just check for each if Class#newInstance() doesn't throw any exception.

like image 32
BalusC Avatar answered Oct 12 '22 03:10

BalusC


There are several suggestions provided as answers to this question.

Here too On-the-fly, in-memory java code compilation for Java 5 and Java 6

like image 29
David J. Liszewski Avatar answered Oct 12 '22 01:10

David J. Liszewski


If it's easy enough to compile at runtime that would be fine.

You can use javax.tools to do the compilation as needed. Create dynamic applications with javax.tools may help, too. It's also possible to do it in memory.

One caveat: using the compiler creates a dependency on the JDK; the JRE alone is insufficient.

like image 1
trashgod Avatar answered Oct 12 '22 02:10

trashgod