JnBrymn
JnBrymn

Reputation: 25353

Finding new Java class at runtime

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.

Upvotes: 8

Views: 376

Answers (8)

BalusC
BalusC

Reputation: 1108577

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.

Upvotes: 3

djna
djna

Reputation: 55897

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

Upvotes: 9

Peter Lawrey
Peter Lawrey

Reputation: 533442

You could use BeanShell. This library is small and doesn't require the JDK. It is used in a number of IDE and web servers. The latest version appears to have the support you need loading .java files from the class path. (Still in beta)

Upvotes: 0

trashgod
trashgod

Reputation: 205775

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.

Upvotes: 1

Carl Smotricz
Carl Smotricz

Reputation: 67750

It's easy enough to iterate through the list of files in a folder. Someone mentioned that it's possible to call the Java compiler from Java (if you re-distribute the JDK, which I think is a point whose legality needs checking!!) That's much of the battle.

You seem to have a fixed model in your mind where only files fulfilling a certain interface are extracted from the folder. I think this is where your method needs to give a little. The sensible way (IMO) to do this would be to compile all files in that folder, and then with their classes stashed away somewhere, you can load and reflect them and then determine which of them "do" the interface and which don't. Those that don't will have been needlessly loaded into your JVM, but unless it's intentionally very space-wasteful, code you don't execute can't harm your program.

Having determined which ones do the computable thing, you can then store those classes (or instances thereof) in a Collection and do whatever you like with them. You simply ignore the other ones.

Upvotes: 0

Dave L.
Dave L.

Reputation: 11228

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

Upvotes: 2

Robert Munteanu
Robert Munteanu

Reputation: 68268

I think this would be simpler if you allowed your customer to type in a code declaration using something like Groovy, which is Java-ish enough, and easy to execute at runtime from a String value.

Upvotes: 0

foret
foret

Reputation: 728

take a look: Find Java classes implementing an interface

Upvotes: 0

Related Questions