Reputation: 17
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.
Upvotes: 0
Views: 510
Reputation: 2694
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
Upvotes: 0