Difference between revisions of "Compiling and Launching"

From Suhrid.net Wiki
Jump to navigationJump to search
Line 1: Line 1:
 
 
== javac ==
 
== javac ==
  
Line 7: Line 6:
 
* the directories for the package structure will however be automatically created by javac.
 
* the directories for the package structure will however be automatically created by javac.
  
* Options:
+
There are four things to keep in mind while compiling:
* -classpath and -cp are the same.  
+
 
* The argument can be the path to the directory which contains the package structure of the class files.
+
* Which are the files that need to be compiled ?
* The argument can be the location of the jar file - including the file name (which contains the package structure of the class files.)
+
* 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
 +
 
 +
* -classpath and -cp can be either of the arguments to instruct the compiler to look for classes used by the compiled file.
 +
* 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 - including the file name (which contains the package structure of the class files.)
  
 
[[Category:OCPJP]]
 
[[Category:OCPJP]]

Revision as of 04:41, 16 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

  • -classpath and -cp can be either of the arguments to instruct the compiler to look for classes used by the compiled file.
  • 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 - including the file name (which contains the package structure of the class files.)