3

In my main program I am allowing users to create Java classes and storing them in a .java file within the package UserInputs. I am now looking for a way to instantiate the user created class within my main program and also running the public methods within the class.

Here is the code which gets executed when the user presses a JButton to finish creating their class.

public void actionPerformed(ActionEvent e) {

    if(e.getSource() == inputButt.getButtons()){
        try{

            PrintWriter writer = new PrintWriter("C:/Users/human/Desktop/UserInputTest/src/UserInputs/UserCreatedClass.java", "UTF-8");
            writer.println(textArea.getText());
            writer.close();

        }catch(Exception except){
            except.printStackTrace();
        }
    }   
}
Human
  • 545
  • 2
  • 7
  • 22
  • I don't think you can compile a java file during runtime like you're proposing – Cruncher Nov 13 '13 at 18:11
  • 5
    See [compiling and running user code with JavaCompiler and ClassLoader](http://stackoverflow.com/questions/2158429/compiling-and-running-user-code-with-javacompiler-and-classloader). – Zong Nov 13 '13 at 18:12
  • interesting question but i dont think this is possible. when you run the main program, UserCreatedClass.java has not been compiled, so how could it be instantiated? – Sionnach733 Nov 13 '13 at 18:13
  • possible duplicate of [How to create Java classes Dynamically by Java Reflection?](http://stackoverflow.com/questions/9125583/how-to-create-java-classes-dynamically-by-java-reflection) – OlivierH Nov 13 '13 at 18:13
  • @OlivierH I don't think that this is a duplicate of this post, since he wants to compile and execute a dynamically created java file and don't want to create one via byte code manipulation. – mvieghofer Nov 13 '13 at 18:21

2 Answers2

0

You need to compile the file at runtime. Maybe this or this post here on SO helps you.

What the first link says is that you should use the Java 6 Compiler API. What you need to do is this:

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int compilationResult = compiler.run(null, null, null, fileToCompile);

Where fileToCompile is the path to your file, in your case "C:/Users/human/Desktop/UserInputTest/src/UserInputs/UserCreatedClass.java". Then you can execute the code via Reflection.

I would be very carefully with letting people create and execute their own Java code, though. I don't know what you plan to do but if you are running this code on a server, I would not recommend doing such things. In case this application should run locally on the clients computer (so they can only harm themselves) this should not be a problem. Otherwise I would not let them program what they want.

Community
  • 1
  • 1
mvieghofer
  • 2,846
  • 4
  • 22
  • 51
  • So I did what you suggested and used this website to help me invoke the methods: http://www.mkyong.com/java/how-to-use-reflection-to-call-java-method-at-runtime/ – Human Nov 14 '13 at 14:58
0

You might also want to consider Groovy compiler which is almost fully compatible with Java syntax, more functional and has simpler API. Example from a Groovy page:

ClassLoader parent = getClass().getClassLoader();
GroovyClassLoader loader = new GroovyClassLoader(parent);
Class groovyClass = loader.parseClass(new File("src/test/groovy/script/HelloWorld.groovy"));

// let's call some method on an instance
GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
Object[] args = {};
groovyObject.invokeMethod("run", args);
Andrey Chaschev
  • 16,160
  • 5
  • 51
  • 68