my dog learned polymorphism*
The moose likes Beginning Java and the fly likes class that prints its own source code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "class that prints its own source code" Watch "class that prints its own source code" New topic
Author

class that prints its own source code

Batista
Greenhorn

Joined: Feb 11, 2007
Posts: 2
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

Joined: Jan 29, 2003
Posts: 8791
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 ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Batista
Greenhorn

Joined: Feb 11, 2007
Posts: 2
Thanks a lot...
satya subudhi
Greenhorn

Joined: May 29, 2012
Posts: 1
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

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?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7484
    
  18

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7484
    
  18

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

Joined: Oct 02, 2003
Posts: 11139
    
  16

FWIW...This has also been discussed here


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
I knew I had seen it before; there is some good discussion there. I seen some better examples of Quines, maybe written in LISP.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: class that prints its own source code
 
Similar Threads
ThreaDish
Doubt with run method..
Print RTF
convert given unicode literal
GC