• Post Reply Bookmark Topic Watch Topic
  • New Topic

program prints itself  RSS feed

 
Kim Vue
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is it possible to have a java program print its own source code without using a hardcoded path? If yes, how? Please share answer with me.
Thank you.

~Kim~
[ November 20, 2003: Message edited by: Kim Vue ]
[ November 20, 2003: Message edited by: Kim Vue ]
 
Tom Blough
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the filename has to be the same as the class name, you should be able to use this.getName(), append .java to it, open it and print the source. This assumes that the source is in the same directory as the class file.
 
Kim Vue
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Tom!
Kim
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just as a note, that would be "this.getClass().getName()"
If you don't have the source code on your system, I guess that it is theoretically possible to dynamically de-compile a class and print out the resulting source; the quality of the output would depend on the de-compiler, but it would be an interesting attempt....
 
Jaap van Hengstum
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A programs that print out its own source code is called a Quine. Search in google for examples.
This page has a lot of examples of Java Quines. It is possible to do a Java Quine without resorting to reflection, f.e. the first program on the above webpage:
class S{public static void main(String[]a){String s="class S{public static void main(String[]a){String s=;char c=34;System.out.println(s.substring(0,52)+c+s+c+s.substring(52));}}";char c=34;System.out.println(s.substring(0,52)+c+s+c+s.substring(52));}}
Edit: sorry for the layout, but this program only works if printed on one line.
[ Edited to remove code tags. Note that the code example is one, single, long line. -ds ]
[ November 21, 2003: Message edited by: Dirk Schreckmann ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to have a java program print its own source code without using a hardcoded path? If yes, how? Please share answer with me.
I'm not sure that this problem description is complete enough to really understand what you are trying to do. If these instructions are literally interpretted, Jaap has provided a correct solution that doesn't even need the source file. But your instructions seem to imply that use of the source file is desired.
How do you understand the instructions? What have you tried to do, or even just thought of to solve this problem?
Remeber that your instructor can use the internet, as well. And if they're a really good instructor, then they hang out at the Ranch, and they've probably noticed this thread. So, you might want to give this assignment a better shot, before looking at Jaap's posted solution.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!