fork download
  1. import java.io.*;
  2. import java.lang.reflect.*;
  3. class Test2 {
  4. public static void main(String[] args) throws Exception {
  5. args = new String[0];
  6. System.setIn(new ByteArrayInputStream(new byte[0]));
  7.  
  8. Field modifiersField = Field.class.getDeclaredField("modifiers");
  9. modifiersField.setAccessible(true);
  10.  
  11. Class<?> fdClass = java.io.FileDescriptor.class;
  12. Field outField = fdClass.getDeclaredField("out");
  13. outField.setAccessible(true);
  14. modifiersField.setInt(outField, outField.getModifiers() & ~Modifier.FINAL);
  15. outField.set(null, new FileDescriptor());
  16. Field errField = fdClass.getDeclaredField("err");
  17. errField.setAccessible(true);
  18. modifiersField.setInt(errField, errField.getModifiers() & ~Modifier.FINAL);
  19. errField.set(null, new FileDescriptor());
  20. Field inField = fdClass.getDeclaredField("in");
  21. inField.setAccessible(true);
  22. modifiersField.setInt(inField, inField.getModifiers() & ~Modifier.FINAL);
  23. inField.set(null, new FileDescriptor());
  24. // program here
  25. Constructor<?> c = fdClass.getDeclaredConstructor(int.class);
  26. c.setAccessible(true);
  27. System.setOut(new PrintStream(new FileOutputStream((FileDescriptor)c.newInstance(1))));
  28. System.out.println("Hello, World!");
  29. }
  30. }
Success #stdin #stdout 0.1s 321600KB
stdin
Standard input is empty
stdout
Hello, World!