-1

I want to make a program that takes a class name and class path from the user and compile this class in runtime and make object from this class for Example:

String path = "C:\\Users\\classes"; String classname = "test.java"; //compile this class //create new object and use methods

I did research on the subject but could not find a concrete example.

  • 3
    Does this answer your question? [Java - Load a class outside of classpath. What are the options](https://stackoverflow.com/questions/24286379/java-load-a-class-outside-of-classpath-what-are-the-options) – Dmitry Pisklov Nov 27 '20 at 20:01
  • Sounds like [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) to me. What is the actual problem you are trying to solve? – PM 77-1 Nov 27 '20 at 20:37
  • @PM77-1 He commented under my post and verified that he wants to compile and load the class during runtime. He hasn't responded to my edits though. –  Nov 27 '20 at 20:55
  • Although I have to admit that his post isn't clear enough and need more details. –  Nov 27 '20 at 20:56

1 Answers1

0

You cannot use .java files in runtime as they are not compiled yet. You have to find a .class or .jar file (or import the .java as a library and compile it with the rest of the project).

If you have the class's fully qualified name but for some reason can't access it at compile-time, you can use Class.forName(package.Class) to load the class, and then call .getConstructor().newInstance() to access its default (no argument) constructor. The resulting object will be an instance of the class. If you have to use this solution, I'd recommend you to do some research on reflection.

If you use the .java file as a library you can import it as a library using your IDE, if you google it there will be plenty of examples.

If you want to compile the file in runtime it will be a bit more difficult, but you can use the installed JDK for that: JavaCompiler javac = ToolProvider.getSystemJavaCompiler(); Or by executing the javac command in the command prompt via Runtime.getRuntime().exec(). I'm not going to cover full solutions for these as they would be too long and you can find them elsewhere.

Note: If the class is not in your class path it is not loaded into the virtual machine. In this case (if it is compiled) you have to modify your JVM arguments to cover the file.

Edit: Code for runtime compilation via javac:

Process p = Runtime.getRuntime().exec(javac + " \"" + pathToFile + "\"");
p.waitFor();

This executes the javac command on your java file and creates a .class file in the same directory (I wrapped the path in "" to fix issues with path containing whitespaces).

Now you need to load the compiled class:

URL[] urls = new URL[]{new File(pathToFile).getParentFile().toURI().toURL()};
ClassLoader cl = new URLClassLoader(urls);
Class yourClass = cl.loadClass(className);

Then you can use the yourClass.getConstructor().newInstance() to create an instance of the class, as it is said in the first solution.