javaquine

Programs that reproduces itself


Is it possible to make a Java program that prints its source code to a new file, and compiles it, and runs the compiled program?


Solution

  • Yes, it is possible. A trivial implementation would be: have the source code contain itself in a string, save the string to a file and fill its own string with the same string (otherwise, the initial string would be of infinite size, due to the recursive manner of this implementation), compile the file, and run the compiled file (which will, in turn, do the very same).

    Non-trivial implementations are significantly harder.