Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

program prints itself

 
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 ]
 
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
 
Bartender
Posts: 1840
Eclipse IDE Ruby Java
  • 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....
 
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 ]
 
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.
 
expectation is the root of all heartache - shakespeare. tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic