8 Replies Latest reply: Jul 3, 2007 10:45 AM by 807605 RSS

    detemining .java and .class files

    807605
      Hello All,

      I have faced a tricky and intresting question. If any know the solution, plz tell me.

      The question is like this:

      you have been given two files. one .java file and other .class file. Their extensions are been removed. Without modifing their names you have to determine which one is .java file and which one is .class file. You have been given command prompt only. So using some commands you have to determine which file is .java and which one is .class file.

      Plz tell me how to determine??

      Thanks in advance.................
        • 1. Re: detemining .java and .class files
          807605
          First, it should be impossible. The Java file and Class file have the same name before the .ext, and no file system I know of supports that mis-feature. Second, depends on the os, but open one file in a text editor, if it is plain text, it is java file, otherwise it is .class file

          ~Tim
          • 2. Re: detemining .java and .class files
            800435
            open the file in a text editor

            damn slow...
            Message was edited by:
            manuel.leiria
            • 3. Re: detemining .java and .class files
              807605
              Thanks for ur reply. But i should not open them in any editor. If we were given that chance we would have determined easily. But thats not the case. You are not suppose to open and view the contents of those files.
              • 4. Re: detemining .java and .class files
                807605
                Thanks for ur reply. But i should not open them in
                any editor. If we were given that chance we would
                have determined easily. But thats not the case. You
                are not suppose to open and view the contents of
                those files.
                You could probably make a reasonable estimate based on file sizes. But I'm guessing that, too, will be excluded by your mystical and fluid set of rules, which seems centred around "you have to determine this without any sort of measuring". Which can't be done
                • 5. Re: detemining .java and .class files
                  Herko_ter_Horst
                  On Linux you could use the "file" command...

                  Very uninteresting and contrived in any case.
                  • 6. Re: detemining .java and .class files
                    807605
                    you have a command window?

                    use
                    C:> edit MythicalFile

                    it will open the file in an editor inside the command window.

                    ALTERNATE:

                    use a simple dir command to look at the files sizes.

                    3rd Alternate:

                    use the command ;(requires JDK)
                    java mythicalFile
                    if it runs, it is the class,
                    if it kicks back an error its not.


                    and why are you trying to cheat anyhow? i imagine this is from an exam or trivia question, that you should probably be solving on your own.
                    • 7. Re: detemining .java and .class files
                      807605
                      Thanks for ur reply. But i should not open them in
                      any editor. If we were given that chance we would
                      have determined easily. But thats not the case.
                      You
                      are not suppose to open and view the contents of
                      those files.
                      You could probably make a reasonable estimate based
                      on file sizes. But I'm guessing that, too, will be
                      excluded by your mystical and fluid set of rules,
                      which seems centred around "you have to determine
                      this without any sort of measuring". Which can't be
                      done
                      This does not seem to be be a good way to judge. I tried a couple files and it seems there is no correlation between size of .java file and size of .class file. But as another poster said, contrived and meaningless.

                      you could rename one of the files by adding .class to the end then running java <classname> and see what happens. Or in windows use the type command, but that is just another way of seeing the file contents.

                      ~Tim
                      • 8. Re: detemining .java and .class files
                        807605
                        Class files start with a "magic" number. The first four bytes should always be:

                        0xCAFEBABE

                        This is extremely unlikely for the first 4 bytes of a source file.