4

I've done some searching, and it seems a few people have asked similar questions, but none of them were quite what I was looking for (some suggested some scripting stuff but i didn't have enough knowledge of what i was doing so i want to use java only if possible).

I need to be able to read lines of code from a String, and then execute them in Java (like this):

String code = "System.out.println(\"Test code\");";

A lot of people reading this post might ask why don't you just execute

System.out.println("Test code");

but I want to execute code other than the println method from Strings.

Is it possible to execute code using java alone, if so how would it compile??

15
  • 4
    write the string to a file.java, compile and run it... Commented Aug 22, 2017 at 14:57
  • 1
    What for do you want to do this? Commented Aug 22, 2017 at 15:00
  • 2
    @JohnD nope, code always must be compiled. Commented Aug 22, 2017 at 15:01
  • 1
    This answer may provide some pointers. Commented Aug 22, 2017 at 15:04
  • 2
    @JohnD as I understand you don't need to compile anything. You only want to input a string and have it "typed" 1to1 in notepad. Is that right? Commented Aug 22, 2017 at 15:10

3 Answers 3

4

You can't do this as easily as I imagine you hope to. The kind of thing you're describing is something you might see in a dynamic language. Java is very much not a dynamic language - this is part of its character.

If you have tools.jar in your classpath, you can compile Java code from within your Java program:

com.sun.tools.javac.Main javac = new com.sun.tools.javac.Main();
String[] options = new String[] {
    "-classpath", classpath, "-d", outputDir, filename
};
javac.compile(options);

From there you could wrestle with the classloader, load the class you have compiled, and run it.

However this is not an easy or mainstream way to use Java, and generally people don't do it because it's neither good practice nor necessary. It is a security feature that you can't run code supplied by the user after compilation.


If you want a more dynamic language, in the Java ecosystem, you could look at Groovy.


Alternatively, you can run user-supplied Javascript, giving it controlled access to your Java program's data, using the Java Scripting API.

Sign up to request clarification or add additional context in comments.

4 Comments

Internal classes like com.sun ... is bad proposition, specially when official javax.tools.JavaCompiler exist
I don't have tools.jar in my classpath, i use IntelliJ how do i add it?
Full JDK (JRE has not this jar)
@JohnD Don't. I hoped to dissuade you in the body of the answer. It's difficult enough that you need to be experienced to do it. And once you're experienced you understand why it's not wise.
1

It is possible but the java code cannot be executed as such, it's not interpreted, it needs to be compiled first. So you need to put your string into a main function of another java file, then compile it and finally run it.

You might need this : http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html

Comments

1

If I understand correct, you want to input a string into the java console. This given string then should be magically be typed-in in an external program. If I did not understand correct, I'll remove that answer again.

public class Test {

    public static void main(String[] args) throws IOException, ClassNotFoundException, Exception {
    Robot robot = new Robot();
    String text = "Hello";
    File file = File.createTempFile("tmp", ".txt");

    if (Desktop.isDesktopSupported()) {
        Desktop.getDesktop().edit(file);
        Thread.sleep(1000);
    } else {
        // dunno, up to you to handle this
    }
    for (char c : text.toCharArray()) {
        int keyEvent = KeyEvent.getExtendedKeyCodeForChar(c);
        robot.keyPress(keyEvent);
        Thread.sleep(500);
    }
    }
}

If you run this basic code, notepad should open and the text Hello is getting typed. That's what I've prepared for you. Now what you need to do is using a Scanner to get the string from the console instead from a static string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.