Results 1 to 9 of 9
  1. #1
    Java Riser's Avatar
    Java Riser is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Default Why do you only compile the Test file?

    Yes, I'm a newbie. But all texts seem to assume everyone understands only to compile and run the "Test.java" file and not to compile or run the other non-test files.

    Sure, you only run the Test file because that's the main one... but I was under the impression that when you use the javac command, the compiler turns the java code into machine code.

    Then... how can the machine use and read the other files if those were not compiled, a.k.a. javac-ed, a.k.a. not turned into machine code?

    This has been bothering me for a while.

    thanks!

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Each file understand by the PC in binaries, any other text cannot understand. Actually those binaries are also not directly understandable to the system, those 1s and 0s are convert into voltages on gates and work (send signals) accordingly.

    Each application have there own conversion techniques, javac compilation do that in java and so on.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Each file understand by the PC in binaries, any other text cannot understand. Actually those binaries are also not directly understandable to the system, those 1s and 0s are convert into voltages on gates and work (send signals) accordingly.

    Each application have there own conversion techniques, javac compilation do that in java and so on.

  4. #4
    Java Riser's Avatar
    Java Riser is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Default

    And so "javac" turns a .java file into something a machine can deal with.

    But why can we "javac" just the nameTest.java file? The nameTest.java file accesses other .java files that we write, like name.java.

    Why can the computer deal with both files we wrote, when we only compiled the nameTest.java one and did not compile the other files? Does it automatically compile every file with the java extension that is referenced in the test.java file? Is that why?
    Last edited by Java Riser; 02-22-2010 at 06:18 AM.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,792
    Rep Power
    19

    Default

    javac turns a java file into byte-code, which is understandable by a JVM. The JVM turns that byte code into instructions the underlying machine can understand. This is why a Java program written and compiled on a Windows PC should still work on a Linux PC or a Mac, for example.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Java Riser View Post
    Does it automatically compile every file with the java extension that is referenced in the test.java file? Is that why?
    That's exactly what the Java compiler does; if a source file A.java depends on a file B.java the compiler notices the dependency; if it can find a file B.class that is newer than the source file B.java it doesn't compile B.java. Otherwise (the source is newer than the compiled form) it recompiles B.java again.

    This all can only happen if javac can find the .class files and/or the .java source files. There are special flags that instruct the compiler where to find the .class files (the classpath (or cp) flag) and where to find the .java source files (the sourcepath flag).

    Because of this behaviour javac isn't very suitable for the 'make' tool because sometimes it does more than it has been told to do and 'make' produces funny output that can be hard to understand ('ant' does a much better job).

    kind regards,

    Jos

  7. #7
    Java Riser's Avatar
    Java Riser is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Default

    Thanks, that mystery is history!

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Java Riser View Post
    Why can the computer deal with both files we wrote, when we only compiled the nameTest.java one and did not compile the other files? Does it automatically compile every file with the java extension that is referenced in the test.java file? Is that why?
    Do you want to compile multiple source file at once? Did you try javac *.java within your working folder?

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Tolls View Post
    This is why a Java program written and compiled on a Windows PC should still work on a Linux PC or a Mac, for example.
    This is a good point.

    Java's bytecodes are desgined to be read and interpreted in exactly same manner on any computer hardware or OS (Windows, Linux, MAC and so on) that supports JRE.

Similar Threads

  1. can someone help me compile and build a jar file
    By peahead in forum New To Java
    Replies: 2
    Last Post: 02-06-2010, 04:05 PM
  2. Replies: 1
    Last Post: 08-03-2009, 05:31 PM
  3. Junit test
    By alice in forum New To Java
    Replies: 1
    Last Post: 06-14-2008, 01:24 AM
  4. Replies: 0
    Last Post: 04-05-2008, 12:38 AM
  5. Help with Java Compile File
    By baltimore in forum New To Java
    Replies: 1
    Last Post: 08-06-2007, 07:48 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •