Compiling Code

We have the complete file, ch10_01.java, and we're ready to run it. The first step is compiling it into a bytecode file, ch10_01.class. To compile ch10_01.java, you use the Java tool javac, the Java compiler (on Windows machines, this program is called javac.exe and is located in the bin subdirectory). Here's how you use javac in general (all the arguments here are optional, and I'll place them in square brackets to indicate that, which is the convention Sun itself uses in the java documentation):

 javac [  options  ] [  sourcefiles  ] [  files  ] 

Here are the arguments to javac:

  • options Command-line options. See the Java documentation for the details; we won't need any command-line options here.

  • sourcefiles One or more code files to be compiled. (Here, that'll be just ch10_01.java.)

  • files One or more files that list command-line options code files to compile.

In this case, I'll compile ch10_01.java with this command:

 %javac ch10_01.java 

The Java compiler, javac, compiles the file ch10_01.java ( assuming that there are no errors), translating it and creating a new file named ch10_01.class. If there are errors, the Java compiler will tell you what they are, including what line of code is wrong, as in this case, in which I've forgotten the name of the println method and tried to use one called printText :

 %javac ch10_01.java  ch10_01.java:5: Method printText(java.lang.String) not found in class java.io.Print Stream.         System.out.printText("Welcome to Java");                             ^ 1 error 

At this point, we've created the file ch10_01.class, the bytecode file that Java will need to run the application. This bytecode file will run unchanged on any system that supports Java.

So how do you actually run ch10_01.class? I'll take a look at that in the next section.



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net