private String execute(final String name, final String code, final String input) throws Throwable { bc.compile(new StringReader(code), name, name, cw); ByteArrayOutputStream bos = new ByteArrayOutputStream(); InputStream is = System.in; PrintStream os = System.out; System.setIn(new ByteArrayInputStream(input.getBytes())); System.setOut(new PrintStream(bos)); try { TestClassLoader cl = new TestClassLoader(getClass().getClassLoader(), name, cw.toByteArray()); Class<?> c = cl.loadClass(name); Method m = c.getDeclaredMethod("main", new Class<?>[] {String[].class}); m.invoke(null, new Object[] {new String[0]}); } catch (InvocationTargetException ex) { throw ex.getCause(); } finally { System.setIn(is); System.setOut(os); } String output = new String(bos.toByteArray(), "ASCII"); System.out.println(code + " WITH INPUT '" + input + "' GIVES " + output); return output; }