Writing and embedding new Java class files at run time

Is it possible to somehow “import” a new Java class into a running program and use it?

Can a program create a new file of the type ".java" and then include it in the project files and reference it without restarting the program?

Below is an example of what I mean:

import java.io.*; public class Program { File JClass = new File("JClass.java"); public static BufferedWriter out = null; public static void main(String[] args) { try { out = new BufferedWriter(new FileWriter("JClass.java")); out.write("public abstract class JClass {"); out.newLine(); out.newLine(); out.write(" public void printSomething(String a) {"); out.newLine(); out.write(" System.out.println(a);"); out.newLine(); out.write(" }"); out.newLine(); out.write("}"); out.close(); } catch (IOException e) { System.exit(-1); } //Somehow import JClass.java as a class here JClass.printSomething("Yay! It worked!"); } } 

The resulting JClass.java file:

 public abstract class JClass { public void printSomething(String a) { System.out.println(a); } } 

Similarly, is it possible to create a copy of one of the source files of the project, edit the code in the file and then somehow force the changes in the current program?

At the moment, I do not care about the practical application. I just study different ideas related to programming. I also understand that this is a potential for all kinds of disasters. Editing executable code and including classes on the fly (which, I believe, will not be checked for errors, because other classes are used when building a project) can have very unpredictable results. I just want to talk to this idea.

However, if anyone has USEFUL warnings or things to look for, I would appreciate it. Otherwise, I would be grateful if people did not respond to this: "This is a bad idea" or "there are simpler and more effective ways to solve problems." I am NOT trying to solve the problem with this. I am only studying this idea.

So is this possible?

+7
source share
4 answers

Javassist will allow you to modify existing classes and create new classes at runtime. http://www.csg.is.titech.ac.jp/~chiba/javassist/

The Javassist tutorial has a section for defining a new class from scratch. Check out the API to learn how to add new methods, etc. Check out CtNewMethod.make in the Javassist API.

Javassist is what JBoss uses to implement aspect-oriented programming.

You can also check out the EATS (tool method would be interesting), which Javassist uses to add new code to existing methods at runtime. Eats is not in release, but works: o

JPDA provides some mechanisms for modifying classes that are already loaded and running in the JVM.

+5
source

You should look at loading the fly class using ClassLoader

http://download.oracle.com/javase/1.5.0/docs/api/java/lang/ClassLoader.html

There are also mechanisms you can use to compile classes, but that probably won't be useful to you.

+2
source

You really want a compiler - take a look at java.lang.Compiler:

http://download.oracle.com/javase/6/docs/api/java/lang/Compiler.html

You can load another class this way by compiling a new .class file.

This does not allow you to modify the loaded class. To do this, you also need java.lang.instrument.ClassFileTransformer, which must be specified on the command line at run time.

To answer your other questions:

  • If the file is compiled from .java, it will pass the same checks as one compiled elsewhere.
  • If you create bytecode using suitable tools (such as BCEL or ASM), it still goes through several checks to prove that it is internally consistent.
  • If you modify running classes, you are limited to what you can change.
0
source

Throwing another one: Eclipse Java Compiler . Unlike BCEL, ASM or Jasmin, you do not need to know the assembly. ECJ is just a Java compiler written in Java.

0
source

All Articles