Assuming that the "TutorJoes.java" contains the following Java source:
public class TutorJoes { public static void main(String[] args) { System.out.println("Tutor Joes!"); } }
(For an explanation of the above code, please refer to Getting started with Java Language .
We can compile the above file using this command:
javac TutorJoes.java
This produces a file called "TutorJoes.class", which we can then run as follows:
java TutorJoes.java Tutor Joes!
The key points to note from this example are:
Most practical Java code uses packages to organize the namespace for classes and reduce the risk of accidental class name collision.
If we wanted to declare the TutorJoes class in a package call com.example, the "TutorJoes.java" would contain the following Java source:
package com.example; public class TutorJoes { public static void main(String[] args) { System.out.println("Tutor Joes!"); } }
This source code file needs to stored in a directory tree whose structure corresponds to the package naming.
# the current directory (for this example) | ----com | ----example | ----TutorJoes.java
We can compile the above file using this command:
javac com/example/TutorJoes.java
This produces a file called "com/example/TutorJoes.class"; i.e. after compilation, the file structure should look like this:
# the current directory (for this example) | ----com | ----example | ----TutorJoes.java ----TutorJoes.class
We can then run the application as follows:
java com.example.TutorJoes Tutor Joes!
Additional points to note from this example are:
If your application consists of multiple source code files (and most do!) you can compile them one at a time. Alternatively, you can compile multiple files at the same time by listing the pathnames:
javac Foo.java Bar.java
or using your command shell's filename wildcard functionality ...
$ javac *.java $ javac com/example/*.java $ javac */**/*.java #Only works on Zsh or with globstar enabled on your shell
This will compile all Java source files in the current directory, in the "com/example" directory, and recursively in child directories respectively. A third alternative is to supply a list of source filenames (and compiler options) as a file. For example:
javac @sourcefiles
where the sourcefiles file contains:
Foo.java Bar.java com/example/TutorJoes.java
Note : compiling code like this is appropriate for small one-person projects, and for once-off programs. Beyond that, it is advisable to select and use a Java build tool. Alternatively, most programmers use a Java IDE (e.g. NetBeans, eclipse, IntelliJ IDEA) which offers an embedded compiler and incremental building of "projects".
Here are a few options for the javac command that are likely to be useful to you
A more complete list of compiler options will be described in a separate exampl
The Java programming language (and its runtime) has undergone numerous changes since its release since its initial public release. These changes include
With very few exceptions (for example the enum keyword, changes to some "internal" classes, etc), these changes are backwards compatible.
If you need to (re-)compile older Java code on a newer Java platform to run on the newer platform, you generally don't need to give any special compilation flags. In a few cases (e.g. if you had used enum as an identifier) you could use the -source option to disable the new syntax. For example, given the following class:
public class OldSyntax { private static int enum; // invalid in Java 5 or later }
the following is required to compile the class using a Java 5 compiler (or later):
javac -source 1.4 OldSyntax.java
If you need to compile Java to run on an older Java platforms, the simplest approach is to install a JDK for the oldest version you need to support, and use that JDK's compiler in your builds.
You can also compile with a newer Java compiler, but there are complicated. First of all, there some important preconditions that must be satisfied:
Given the preconditions are met, you can recompile code for an older platform using the -target option. For example,
javac -target 1.4 SomeClass.java
will compile the above class to produce bytecodes that are compatible with Java 1.4 or later JVM. (In fact, the - source option implies a compatible -target, so javac -source 1.4 ... would have the same effect. The relationship between -source and -target is described in the Oracle documentation.)
Having said that, if you simply use -target or -source, you will still be compiling against the standard class libraries provided by the compiler's JDK. If you are not careful, you can end up with classes with the correct bytecode version, but with dependencies on APIs that are not available. The solution is to use the -bootclasspath option. For example:
javac -target 1.4 --bootclasspath path/to/java1.4/rt.jar SomeClass.java
will compile against an alternative set of runtime libraries. If the class being compiled has (accidental) dependencies on newer libraries, this will give you compilation errors.
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions