How to Compile and Deploy a Java Class at Runtime

Can a Java class add a method to itself at runtime?

It's not simple. Once a class is loaded by a classloader, there is no way to change the methods of loaded classes. When a class is requested, a classloader will load it and link it. And there is no way (with Java) to change the linked code or to add/remove methods.

The only trick that comes to my mind is playing with classloaders. If we delete a custom classloader, then the classes loaded by that classloader should be deleted or inaccessible too. The idea that comes to my mind is to

  1. implement one custom classloader
  2. load the dynamic class with that custom classloader
  3. if we have an updated version of this class,
  4. remove the custom classloader and
  5. load the new version of this class with a new instance of the custom classloader

I leave that as food for thought, can't prove, if this leads to a solution or if we have pitfalls.

As a simple answer to the question: No, we can't change a loaded class like we can change the content of fields with reflection. (we can't add or remove fields too).

How to create a java progam to compile and run a list of java programs

On Runtime.exec

Though perhaps not the most ideal solution, you an execute a shell command as a separate Process using Runtime.getRuntime().exec(someCommand). There are also overloads that takes parameters as a String[].

This is not an easy solution. Managing a concurrent Process and preventing a deadlock etc is not trivial.

Related questions

  • Is java Runtime.exec(String[]) platform independent?
  • What is the purpose of Process class in Java?
  • How can I compile and deploy a java class at runtime?
  • Compiling a class using Java code using process
  • Java: Executing a Java application in a separate process
  • Running a program from within Java code..

On draining Process streams

Generally you can't just waitFor() a Process to terminate; you must also drain its I/O streams to prevent deadlock.

From the API:

Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.

Related questions

  • Draining Standard Error in Java

On the Java 6 Compiler API

One option to compiling a Java source code within Java is to use the Java 6 Compiler API. This requires a JDK to be installed (not just a JRE).

See also

  • interface JavaCompiler from package javax.tools
    • external tutorial article

Related questions

  • Null Pointer Exception while using Java Compiler API

Changing the java class file in web app requires building the complete project again and deploy it

due to J2EE container implementaion, in tomcat,

JSP -- use a stand-alone ClassLoader;

Normal Servelet and other class -- AppClassloader

when a jsp changed, the new compiled Servlet(different from user defined Servlet) class will be loaded by a new stand-alone ClassLoader.



Related Topics



Leave a reply



Submit