on
Italy
- Get link
- X
- Other Apps
javac [options] [source files]Both the [options] and the [source files] are optional parts of the command, and both allow multiple entries.
javac -helpThe command above line doesn't compile any files but prints a summary for valid options. Let's look at the following invocation.
javac -classpath com:. -g myFirstCode.java mySecondCode.javaIt passes the compiler two options.
-g | Generate all debugging info. |
-g:none | Generate no debugging info. |
-g:{lines,vars,source} | Generate only some debugging info. |
-verbose | Output messages about what the compiler is doing. |
-nowarn | Generate no warnings. |
-deprecation | Output source locations where deprecated APIs are used. |
-classpath |
Specify where to find user class files and annotation processors. |
-cp |
Specify where to find user class files and annotation processors. |
-sourcepath |
Specify where to find input source files. |
-bootclasspath |
Override location of bootstrap class files. |
-extdirs |
Override location of installed extensions. |
-endorseddirs |
Override location of endorsed standards path. |
-proc:{none,only} | Control whether annotation processing and/or compilation is done. |
-processor |
Names of the annotation processors to run; bypasses default discovery process. |
-processorpath |
Specify where to find annotation processors. |
-d |
Specify where to place generated class files. |
-s |
Specify where to place generated source files. |
-implicit:{none,class} | Specify whether or not to generate class files for implicitly referenced files. |
-encoding |
Specify character encoding used by source files. |
-source |
Provide source compatibility with specified release. |
-target |
Generate class files for specific VM version. |
-version | Version information. |
-help | Print a synopsis of standard options. |
-Akey[=value] | Options to pass to annotation processors. |
-X | Print a synopsis of nonstandard options. |
-J |
Pass |
cd MyProject javac -d classes source/MyFirstJavaCode.javaNow, let's how packages work with the javac -d option. Suppose we have the following *.java file in the directory structure in the picture below:
package com.bogotobogo;
public class MyFirstJavaCode {
public static void main(String[] args) {
System.out.println("This is my first java");
}
}
If we were in the source directory, we would compile MyFirstJavaCode.java and put the resulting MyFirstJavaCode.class file into the classes/com/bogotobogo directory by using the following command:javac -d ../classes com/bogotobogo/MyFirstJavaCode.javaThe command says: "To set the destination directory, cd back to the MyProject directory then cd into the classes directory, which will be our destination. Then, compile the file named MyFirstJavaCode.java. Next, put the resulting put the resulting MyFirstJavaCode.class file into the directory structure that matches its package, in our case, classes/com/bogotobogo directory." Since MyFirstJavaCode.class is in a package, the compiler knows it and put the resulting *.class file into the classes/com/bogotobogo directory.
javac -d ../classes com/bogotobogo/MyFirstJavaCode.javaThe compiler will build two directories com and com/bogotobogo in order to put the resulting MyFirstJavaCode.class file into the correct package directory, com/bogotobogo.
javac: directory not found: ../classes Usage: javac use -help for a list of possible options
java [options] class [args ...] : to execute a class java [options] -jar jarfile [args ...] : to execute a jar file javaw [ options ] class [ args... ] : to execute a class javaw [ options ] -jar file.jar [ args ... ] : to execute a jar fileThe java tool launches a Java application. It does this by starting a Java runtime environment, loading a specified class, and invoking that class's main method. The method declaration must look like the following:
public static void main(String args[])The method must be declared public and static, it must not return any value, and it must accept a String array as a parameter. By default, the first non-option argument is the name of the class to be invoked. A fully-qualified class name should be used. If the -jar option is specified, the first non-option argument is the name of a JAR archive containing class and resource files for the application, with the startup class indicated by the Main-Class manifest header.
java -DmyProperty=myPropValue MyFirstJavaCode args1 args2What the command is telling: "Create a system property called myProperty and set its value to myPropValue. Then, launch the file named MyFirstJavaCode.class and sent it two String arguments whose values are args1 and args2.
-classpath /com/bogotobogo/dir1:com/bogotobogo/dir1/dir1AIt specifies two directories in which classes can be found: /com/bogotobogo/dir1 and /com/bogotobogo/dir1/dir1A. In both cases, these directories are absolutely tied to the root of the file system. We need to remember that when we specify a subdirectory, we're not specifying the directories above it. For example, in the previous example, the directory /com will not be searched.
-classpath /com/bogotobogo/dir1:com/bogotobogo/dir1/dir1A:.Note that we're talking about *.class files not *.java files. When we're telling javac which *.java file to compile, javac looks in the current directory by default.
cd MyCodes jar -cf myJar.jar myAppThe jar command will create a jar file called myJar.jar and it will contain the myApp directory and myApp's entire subdirectory tree and files. We can look at the contents of the jar file with the following command:
jar -tf myJar.jarThat will produce something like this:
META-INF/ META-INF/MANIFEST.MF myApp/ myApp/myTools/ myApp/myTools/copyIt.class myApp/myEngine/spawn.class myApp/myEngine/lock.class
jar {ctxui}[vfm0Me] [jar-file] [manifest-file] [entry-point] [-C dir] files ...Here is the list of options available for the jar command.
-c | create new archive. |
-t | list table of contents for archive. |
-x | extract named (or all) files from archive. |
-u | update existing archive. |
-v | generate verbose output on standard output. |
-f | specify archive file name. |
-m | include manifest information from specified manifest file. |
-e | specify application entry point for stand-alone application bundled into an executable jar file. |
-0 | store only; use no ZIP compression. |
-M | do not create a manifest file for the entries. |
-i | generate index information for the specified jar files. |
-C | change to the specified directory and include the following file. If any file is a directory then it is processed recursively. |
cd MyJava javac -classpath MyCodes/myJar.jar myExtra.javaCompare the use of the jar file to using a class in a package. If myExtra.java needed to use classes in the myApp.myTools package, and the class was not in a jar, we should issue the following command:
cd MyJava javac -classpath MyCodes myExtra.javaNote that when using a classpath, the last directory in the path must be the super-directory of the root directory for the package. Also note that myApp can be the root directory for more than one package (myApp.myTools and myApp.myEngine), and the java and the javac commands can find what they need across multiple packages like the example. So, if MyCodes is on the classpath and MyCodes is the super-directory of myApp, then classes in both the myApp.myTools and myApp.myEngine packages will be found.
Comments
Post a Comment