Difference between revisions of "Compiling and Launching"

From Suhrid.net Wiki
Jump to navigationJump to search
Line 64: Line 64:
 
<syntaxhighlight lang="java5">
 
<syntaxhighlight lang="java5">
 
import static java.lang.System.out;
 
import static java.lang.System.out;
 +
import static java.lang.Thread.MAX_PRIORITY;
  
 
public void foo() {
 
public void foo() {
  out.println("Hello");
+
  out.println("A thread's max priority is " + MAX_PRIORITY);
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 
 
  
  
 
[[Category:OCPJP]]
 
[[Category:OCPJP]]

Revision as of 04:34, 18 June 2011

javac

  • javac by default places the generated class files in the same directory as the source files.
  • use the javac -d option to place the class files in some other directory.
  • if the directory does not exist, there will be an error.
  • the directories for the package structure will however be automatically created by javac.

There are four things to keep in mind while compiling:

  • Which are the files that need to be compiled ?
  • The directory where javac looks for other .java source files that are used by the file to be compiled.
  • The directory where javac looks for class files that are used by the file to be compiled.
  • The directory to place the generated class files in.

Options:

  • the -sourcepath is the directory where the compiler looks for other java files.
  • It has to be the directory that contains the hierarchy of the package structure of the source files.

for e.g. if we are compiling ClassB.java in directory src/net/suhrid/pkgB/ClassB.java which uses ClassA located in net/suhrid/pkgA/pkgA/ClassA.java then argument to sourcepath must be the "src" directory: javac -sourcepath src net/suhrid/pkgB/ClassB.java


  • this option will also compile the files which are referenced by the file being compiled.
  • -classpath and -cp can be either of the arguments to instruct the compiler to look for classes used by the compiled file.
  • Typically used for pre-compiled third party classes/libraries.
  • In case of directories, the directory which contains the hierarchy of the package structure of the class files. (similar to sourcepath)
  • In case of jar files, the argument is the location of the jar file - till the file name. Here the jar file acts as the directory which contains the hierarchy of the package structure of the third party class files.
  • Note multiple directories can be specified in the -sourcepath and the -classpath options (separated by ":" in unix and ";" in Windows). Typically the sourcepath will be a single directory whereas the classpath will have multiples directories or jar files for various third party libraries.

java

  • Launches the JVM.
  • Exactly 1 class must be specified which has the main() method.
  • java [options] class-name [cmd-line arguments to the class]

cmd-line arguments

  • args are space separated.
  • If no args are passed, String[] args will be a zero-length array - it won't be null.
  • The first-argument is args[0] and the fourth will be args[3]

options

  • -D is used to pass properties to the JVM which can be retrieved using System.getProperties().
  • e.g. java -Dname=suhrid someclass
  • There is no space between -D and the name=value pair!
  • -cp / -classpath : same as in javac.

jar

  • jar -cf <jartobecreated.jar> <dirname-withclasses>
  • jar is recursive when adding directories

imports

  • import is only an alias for a qualified class name. It's only purpose is to save typing and make code more readable.
  • import java.util.* will allow us to use HashMap without saying java.util.HashMap everytime
  • import java.util.* says use short name for all classes in java.util package ONLY.
  • This does not extend to subpackages! for e.g it won't apply to java.util.regex. Import only applies to a single package.

Static imports

  • Used to alias a class's static members (field's and methods)
  • e.g.
import static java.lang.System.out;
import static java.lang.Thread.MAX_PRIORITY;

public void foo() {
  out.println("A thread's max priority is " + MAX_PRIORITY);
}