0

That may sound stupid. But is it at all possible? I was thinking about creating a program that would allow someone to code inside it, and then run the code to test the output. To do this of course, I need to be able to compile and run Java from within Java. Thanks in advance.

2 Answers 2

2

JDK6 added a compiler tools api

That should do what you want.

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

Comments

0

You could very well try to simulate the terminal or command line from a java program using the following code snippet ...

import java.io.IOException;


public class Test {
    public static void main(String[] args) throws IOException {
        Process p = Runtime.getRuntime().exec(new String[]{"bash","-c","echo abc > test.txt"});
        p = Runtime.getRuntime().exec(new String[]{"bash","-c","java HelloWorld > test.txt"});

    }
}

So what this code basically does is that it executes a bash shell in which it executes various shell commands like echo abc > test.txt (the first line in the main method) or java HelloWorld > test.txt (the second line in the main method). So here on, it is just like firing up shell commands but from a Java program

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.