Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

class that prints its own source code

 
Batista
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
helo,
I've got very simple program e.g. that only prints "Helo World"
and after that I'd like to print the whole source code of this file,
begining with import java.util.*; ending with } (closing the class declaration)
Is any simple way to do it?
thanks in advance for your hints...
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, replacing my previous answer completely ... a quick Google shows that a program designed to self replicate by writing its own source to stdout is called a Quine. There are some in Java.

A page about Quines.

If you're looking for some way for classes that do other useful things to print their own source, say to display the source and output together for teaching purposes, I suppose you could just read the source file and print it.
[ February 12, 2007: Message edited by: Stan James ]
 
Batista
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot...
 
satya subudhi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Maneesh Godbole
Saloon Keeper
Posts: 11092
13
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

In case you didn't notice, the last post before you is years back

Would your code work if the java file was not available?
 
Winston Gutkowski
Bartender
Pie
Posts: 10492
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stan James wrote:a quick Google shows that a program designed to self replicate by writing its own source to stdout is called a Quine.

Thanks for that. I knew there was a term for them, but I'd forgotten what it was. I also thought it was coined by Dennis Ritchie, but I see I'm wrong there too.

Winston
 
Winston Gutkowski
Bartender
Pie
Posts: 10492
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:In case you didn't notice, the last post before you is years back

Oh darn. Me too; I always forget to check.

Winston
 
fred rosenberger
lowercase baba
Bartender
Posts: 12183
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FWIW...This has also been discussed here
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I knew I had seen it before; there is some good discussion there. I seen some better examples of Quines, maybe written in LISP.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic