How to run Java source code within a Java program

后端 未结 3 877
遇见更好的自我
遇见更好的自我 2021-01-03 04:49

I have wrote some code to compile a Java source code. It then produces the .class file. The problem is how do I run it?

For example, I am ok with the name of the pro

3条回答
  •  感情败类
    2021-01-03 05:27

    You need to create a classloader (a URLClassLoader will probably be fine) which will load the just-compiled class file. (So for a URLClassLoader, the compilation output path should be one of the URLs).

    Then, load the compiled class using the classloader, and execute it using reflection.

    Class c = cl.loadClass("ClassName");

    ... etc.

提交回复
热议问题