6

I have a little quiz that I cannot solve by myself. I want to build a program that will print its own source code with java. Anyone knows how to do this? Like this example:

public class SourcePrint {

    private static final long serialVersionUID = 1L;

    public void test(){
        System.out.println("Hi I'm test");

    }
    public static void main(String[] args) {
        new SourcePrint().test();
    }

}

when we run this, the output would be same like this:

public class SourcePrint {

    private static final long serialVersionUID = 1L;

    public void test(){
        System.out.println("Hi I'm test");

    }
    public static void main(String[] args) {
        new SourcePrint().test();
    }

}

I don't know how to do this. Anybody know the solution or at least the hint? This is not the decompiler, the quiz maker told me the hint is "STATIC".

durron597
  • 31,968
  • 17
  • 99
  • 158
user1481602
  • 77
  • 1
  • 1
  • 2
  • Quines generally involve printing the source without reading it in from its own source file, which is regarded as cheating [in regards to this kind of problem]. Does your solution requirement specify whether you allowed to do this? – yoonsi Jun 26 '12 at 04:15

4 Answers4

6

http://en.wikipedia.org/wiki/Quine_%28computing%29

gobernador
  • 5,659
  • 3
  • 32
  • 51
Rick Mangi
  • 3,761
  • 1
  • 14
  • 17
3

Here is an example that may be of use for Quine in java:

public class Quine {

    static String s = "public class Quine { public static void main(String[] args) { char c=34; System.out.println(s+c+s+c+';'+'}'); } static String s=";

    public static void main(String[] args) {
        char c = 34;
        System.out.println(s + c + s + c + ';' + '}');
    }
}

and of course the seemingly mandatory link to Wikipedia: Wikipedia: Quine. depending on how large the program is this link here may also have some good examples: Java Quine

jazzpi
  • 1,399
  • 12
  • 18
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
3
import java.io.*;  
class Source  
{  
    public static void main(String args[])  
    {  
        try{  

            //open the file  
        FileInputStream fstream=new FileInputStream("D://Springs WorkSpace/Testing/src/Source.java");//here pass the own java file name with full path 

            // use DataInputStream to read binary NOT text  
        // DataInputStream in=new DataInputStream(fstream);  

            //  
        BufferedReader br=new BufferedReader(new InputStreamReader(fstream));  
            //read data line by line from the file  
            String s;  
            while((s = br.readLine() ) != null)  
            {  
                System.out.println(s);  
            }  
            in.close();  
        }  
        catch(Exception e){  
            e.printStackTrace();  
        }  
    }  
}  
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
Pratap A.K
  • 4,337
  • 11
  • 42
  • 79
2

a quick google search turned up the following link:

http://en.wikipedia.org/wiki/Quine_%28computing%29

the type of program you are looking for is generally referred to as a quine.

gobernador
  • 5,659
  • 3
  • 32
  • 51
schadr
  • 398
  • 2
  • 10