Compile and Run C code from Java via embedded TCC Compiler (Works on Linux, Windows and macOS)
To execute C code, the following method can be used:
// execute C code"
String code = "#include <stdio.h>\n" +
"int main(int argc, char* argv[]) {\n" +
" printf(\"Hello, World!\\n\");\n"+
" return 0;\n"+
"}";
CInterpreter.execute(code);
To execute a specified C script, use:
// execute main.c
CInterpreter.execute(new File("main.c"));
To print the output to given print streams, use the print(...)
method:
// execute main.c
CInterpreter.execute(new File("main.c")).print(System.out,System.err);
To wait until the execution has finished, use the waitFor()
method:
// execute main.c and wait until exution finishes"
CInterpreter.execute(new File("main.c")).waitFor();
The exit value can be accessed via:
// execute main.c
CInterpreter.execute(new File("main.c")).getProcess().exitValue();
To destroy the current process, the destroy()
method can be used:
// execute main.c
CInterpreter.execute(new File("main.c")).destroy();
- Java >= 1.8
- Internet connection (dependencies are downloaded automatically)
- IDE: Gradle Plugin (not necessary for command line usage)
Open the VTCC
Gradle project in your favourite IDE and build it
by calling the assemble
task.
Navigate to the Gradle project (e.g., path/to/VTCC
) and enter the following command
sh gradlew assemble
gradlew assemble