Java Tutorial/Language/Introduction

Материал из Java эксперт
Перейти к: навигация, поиск

A profiler is used to analyze how much time a program spends in each part of the code.

You can use this information to determine which parts of a program to optimize.

java -prof TestDrive


Commonly Used Java Command Options

   <source lang="java">

Option Description -client Runs the client VM -serve Runs the server VM, which is optimized for server systems -classpath adirectories A list of directories or JAR or Zip archive and archives files used to search for class files -cp <search path> Same as -classpath -D name=value Sets a system property -verbose Enables verbose output -version Displays the JRE version number, then stops -showversion Displays the JRE version number, then continues -? or -help Lists the standard options -X Lists nonstandard options -ea or -enableassertions Enables the assert command -ea classes or packages Enables assertions for the specified classes or packages -esa or -enablesystemassertions Enables system assertions -dsa or -disablesystemassertions Disables system assertions</source>





Developing a Java program involves

  1. writing code,
  2. compiling it into bytecode
  3. running the bytecode.

Your First Java Program.



   <source lang="java">

public class MainClass {

   public static void main(String[] args) {
       System.out.println("Java");
   }

}</source>





Java Compiler Options

   <source lang="java">

Folder Description -g Generate all debugging info -g:none Generate no debugging info -g:{lines, vars, source} Generate only some debugging info -nowarn Generate no warnings -verbose Output messages about what the compiler is doing -deprecation Output source locations where deprecated APIs are used -classpath <path> Specify where to find user class files -cp <path> Specify where to find user class files -sourcepath <path> Specify where to find input source files -bootclasspath <path> Override location of bootstrap class files -extdirs <dirs> Override location of installed extensions -endorseddirs <dirs> Override location of endorsed standards path -d <directory> Specify where to place generated class files -encoding <encoding> Specify character encoding used by source files -source <release> Provide source compatibility with specified release -target <release> Generate class files for specific VM version -version Version information -help Print a synopsis of standard options -X Print a synopsis of nonstandard options -J<flag> Pass <flag> directly to the runtime system</source>





Java memory command line arguments

   <source lang="java">

java -Xms32m -Xmx200m -XX:MaxPermSize=128m com.SomeWonderfulApp</source>





JDK Utilities

ToolFunctionjavacThe Java compiler. Converts Java source code into bytecodes.javaThe Java interpreter. Executes Java application bytecodes directly from class files.appletviewerA Java interpreter that executes Java applet classes hosted by HTML files.javadocCreates HTML documentation based on Java source code and the comments it contains.rmicCreates class files that support Remote Method Invocation (RMI).rmiregistryRegistry used to gain access to RMI objects on a specific machine.rmidActivation system daemon for RMI object registration and activation.native2asciiSpecial program used to convert between standard Latin-1 Unicode characters and other international encoding schemes.jarJava Archive (JAR) file generator. JAR files allow multiple Java classes and resources to be distributed in one compressed file.keytoolUsed for security key generation and management.jarsignerImplements digital signing of JAR and class files. Allows applets to be certified by trusted authorities.policytoolAllows user-installation-level security policy configuration.


See the details of the compilation: use the verbose option

javac -verbose TestDrive.java


Set the memory available to the JVM

   <source lang="java">

java -mx20m myApp // for max java -ms20m myApp // for start</source>





Some of the Options Supported by the javap Utility

   <source lang="java">

Option Description -b This command ensures backward compatibility with earlier versions of javap. -bootclasspath This command, followed by a path, specifies the path from which to load the bootstrap classes. Normally these would be classes contained in the /lib/rt.jar archive. -c This command prints the JVM instructions for the execution of each method. This tells you what the bytecode for each method actually does. -classpath This command, followed by a user-specified class path, overrides the system CLASSPATH environment variable. -extdirs This command, followed by a directory, overrides the location the system searches for installed extensions. The default location is /lib/ext. -help This command prints information about the javap utility. -Jflag This command passes the specified flag directly to the runtime system. -l This command displays line and local variables. -package This command shows only package, protected, and public classes and members. This is the default. -private This command shows information about all classes and members. -protected This command displays information about protected and public classes and members only. -public This command shows information only about public classes and members. -s This command prints internal type signatures. -verbose This command prints additional information for each method including stack size, local variable information, and arguments.</source>





The jarsignature Tool

The jarsigner tool enables you to sign Java ARchive (JAR) files and verify the signatures of signed JAR files.

The jarsigner tool uses key and certificate information from a keystore to generate digital signatures for JAR files.

A keystore is a database of private keys.

The jarsignature also uses an entity"s private key to generate a signature.

The syntax to use the jarsignature tool is:



   <source lang="java">

jarsigner [ options ] jar-file alias jarsigner -verify [ options ] jar-file</source>





The jar Tool

The jar tool creates a JAR archive file by combining multiple files of a Java application.

The jar tool compresses content based on the ZIP and ZLIB compression formats.

You can also use the jar tool to extract the content of a JAR file.

The syntax to use the jar tool is:



   <source lang="java">

Usage: jar {ctxui}[vfm0Me] [jar-file] [manifest-file] [entry-point] [-C dir] files</source>



OptionDescriptioncCreates a new archive.tLists the contents of an archive.xExtracts contents from an archive.uUpdates an existing archive.vDisplays output at the command prompt while the jar tool performs an operation.fSpecifies the name of the archive file.mIncludes a specified manifest file in the archive.oInstructs the jar tool not to compress content.MInstructs the jar tool not to create a manifest file for an archive. The jar tool, by default, creates an empty manifest file in an archive.CSpecifies the directories to include in the archive.


The javadoc Tool

The javadoc tool generates HTML documentation from Java source files.

The javadoc tool locates and parses the documentation comments present in a Java source file and produces a set of HTML pages describing the class, methods, and variables present in the source file.

The syntax to use the javadoc tool is:



   <source lang="java">

javadoc [options] [packagenames] [sourcefilenames] [-subpackages pkg1:pkg2:...] [@argfiles]</source>



  1. options refers to the command line options that you can pass to the javadoc tool.
  2. packagenames refers to multiple packages, separated by spaces, that javadoc should use to generate documentation. For example:
  3. packagesnames java.lang java.lang.reflect java.awt
  4. sourcefilenames refers to the source files for which javadoc needs to generate documentation.
  5. -subpackages: Generates documentation from the source files in specified packages and recursively in their subpackages.
  6. @argfiles: Refers to a file that contains Javadoc options.


The javap Tool

The javap command displays information about the methods, variables, and parameters present in a class file.

The output of the javap tool depends on the options used. If you do not specify any options while using the jap tool, the javap tool prints the package, protected, and public fields and methods of the classes passed to the tool.

The syntax to use the javap tool is:



   <source lang="java">

javap [options] class. . .</source>



OptionDescription-helpDisplays a help message for the javap tool.publicShows only the public classes and members.protectedShows only the public and protected classes and members.privateShows all classes and members.packageShows the package, protected, and public classes and members. This is the default option of the javap tool.-classpath pathSpecifies the class path that javap should use to locate the class file to disassemble.


The java Tool

The java tool executes a Java class.

To execute a Java class, the java tool loads the bytecode of the specified class and invokes the main() method of the class.

You can also use the java tool to run executable JAR files.

The syntax to use the java tool to execute a Java class is:

java [options] <class-name>

options refers to the command line options that you can pass to the java tool.

The class-name specifies the name of the class file to execute.

OptionDescription-clientSelects the Java HotSpot Client Virtual Machine (VM) to use for executing a class.-serverSelects the Java HotSpot Server VM to use for executing a class.-cp classpathSpecifies a list of directories, JAR archives, and ZIP archives to search class files.-jarExecutes a JAR file.-versionDisplays the JVM version.


The jconsole Tool

The jconsoletool is a JMX-compliant graphical tool for monitoring a Java virtual machine.

The jconsoletool can monitor both local and remote JVMs.

The jconsoletool can monitor and manage an application.

The syntax to use jconsoletool is:



   <source lang="java">

jconsole [ options ] [ connection ... ]</source>



OptionDescription-interval=nAllows you to set the update interval to n seconds.-notileSpecifies not to tile windows initially.-pluginpath pluginsSpecifies a list of the directories or JAR files that are searched for JConsole plug-ins.-helpDescribes the jconsoletool option.


The native2ascii Tool

You can use the native2ascii tool to convert files with native encoded characters to a file with Unicode encoded characters.

The syntax to use the native2ascii tool is:



   <source lang="java">

native2ascii [options] [inputfile [outputfile]]</source>



  1. options: Refers to various options that you can use to pass information or configure the native2ascii tool.
  2. inputfile: Refers to the file that contains native encoded characters.
  3. outputfile: Represents the file that will contain the converted text.

Options of the native2ascii Tool

OptionDescription-reverseAllows you to perform the reverse operation, which reverse the sense of the conversion.-encoding encoding_nameSpecifies the encoding name that is used by the conversion procedure.-JoptionAllows you to pass configuration options to JVM.


The syntax to use the javac tool

javac [options] [sourcefiles] [classes]
  1. options: Refers to command line options.
  2. sourcefiles: Refers to the source files that need to be compiled.
  3. classes: Refers to one or more classes to be processed for annotations.

OptionDescription-cp path or -classpath pathSpecifies the Java classpath.-d directorySets the destination directory to store class files.-gGenerates debugging information.-g:noneInstructs the javac tool not to generate any debugging information.-helpPrints information about the options of the javac tool.-XlintDisplays warnings against the use of deprecated classes and methods.-Xlint:-nameDisables deprecation warnings for a specified class, interface, or method.-Xlint:pathDisplays a warning when you specify a directory to compile but the directory does not exist.