How to update the Class in the Java package, or re-package the class in the package

Source: Internet
Author: User

How to update the Class in the Java package, or re-package the class in the package

The following describes how to execute basic commands:

Only the javac, jar, and java (*. exe) commands are used,

I. javac [compiling Java source files into Class]

1. Common commands: javac-cp specifies multiple classpath

Javac-classpath can specify the class path to compile java files

For example:
Javac-classpath ** \ servlet-api.jar **. java to compile a java File

Need to specify multiple paths? Are separated by semicolons (;).
For example:
Javac-cp classes; C: \ Tomcat \ lib \ servlet-api.jar-encoding UTF-8 BeerSelect. java

(Directly typed in CMD) This is to use the class files under the classes and the servlet-api.jar together to compile

BeerSelect. java

PS: Keep in mind that you must specify classpath for all. jar packages that may be used when running this main program,

Internal compilation references do not report errors. If the project is small, there are not many packages, and the compilation timeliness is not very important, it is recommended that the project

The package is usually placed under a class library file. During packaging, all the jar packages in this folder are compiled.

In addition,-encoding UTF-8 is if the files to be compiled are not all in English, that is, 'there are Chinese characters and the like, you can set encoding, saving

Compilation error.

 

2. For other commands or parameters, enter "javac" (plus enter key) or "javac-help" in CMD"

(Plus enter key) See the note:

Usage: javac <option> <Source File>

Among them, possible options include:-g to generate all debugging information-g: none not to generate any debugging information-g: {lines, vars, source} only generate some debugging information.-nowarn does not generate any warning.-verbose outputs the message about the operation being executed by the compiler.-deprecation outputs the source location of an outdated API.-classpath <path> specify the location for searching user-class files-cp <path> specify the location for searching user-class files-sourcepath <path> specify the location for searching input source files-bootclasspath <path> overwrites location-extdirs <directory> overwrite the location of the installed extension directory-endorseddirs <directory> overwrite the location of the standard path of the signature-d <directory> specify the location where the generated class files are stored-encoding <encoding> the character encoding used by the specified source file-source <version> provides source compatibility with the specified version-target <version> generates class files of a specific VM version-version Information-help output standard option feed-X outputs a non-standard option feed-J <flag> directly passes the <flag> to the runtime

 

 

Ii. jar [*. jar operations on the packages referenced in Java]

1. Common commands: jar cf updates the specified file or repackage in the specified package

E.g.: % JAVA_HOME % \ bin \ jar cf lib \ A. jar sample \ B. class (this format is mostly written in *. bat, etc.

Batch File Execution, the same below)

When you use the jar command to directly modify the file in the package (usually the Class file), you must understand the structure of the original package before you start.

Remember the principle that the relationship between the package reference and the actual structure is "." = "\" (the actual folder will be found after decompression)

 

2. For other commands or parameters, enter "jar" (plus enter key) or "jar-help" (plus enter key) in CMD)

Note:

Usage: jar {ctxu} [vfm0Mi] [jar-file] [manifest-file] [-C Directory] File Name...

Option:

-C. Create a new archive

-T list the archived content

-X expand the named (or all) files in the archive

-U: update an existing archive

-V generates detailed output to the standard output

-F specifies the archive file name

-M contains the marker information from the specified file.

-0 storage only; zip compression format not used

-M does not generate a manifest file for all items

-I generates index information for the specified jar File

-C changes to the specified directory and contains the following files:

If a file name is a directory, it is recursively processed.

Both the configuration (manifest) file name and the archive file name must be specified, in the same order specified by the 'M' and 'F' signs.

First, write a configuration file example. mf in the current directory of the resource file.

For a simple example, see: http://blog.csdn.net/studyvcmfc/article/details/7745500

 

 

Iii. java [actual running of Class files]

This should be familiar, that is, executing the Java Class file (last step )'

For example

Java-cp.; c: \ dir1 \ lib. jar Test
-Cp, like-classpath, specifies the path of other classes that the class depends on for running, usually class libraries, jar packages, and so on.

Full path to jar package, window Semicolon ";"

Also> e.g.: @ "% JAVA_HOME % \ bin \ java"-cp lib \ A. jar; ...... sample. B "% ~ 1"

Similarly, you can use java or Java-help to view:

Options command line option (Chinese is not found on the internet, and I am not using Chinese now. Let's look at it first. Use a Chinese System and try it by myself. It's very easy) Usage: java [-options] class [args...] (to execute a class)
Or java [-options]-jar jarfile [args...] (to execute a jar file)
Where options include:
-Client to select the "client" VM
-Server to select the "server" VM
-Hotspot is a synonym for the "client" VM [deprecated]
The default VM is client.
-Cp <class search path of directories and zip/jar files>
-Classpath <class search path of directories and zip/jar files>
A; separated list of directories, JAR archives,
And ZIP archives to search for class files.
-D <name >=< value>
Set a system property
-Verbose [: class | gc | jni]
Enable verbose output
-Version print product version and exit
-Version: <value>
Require the specified version to run
-Showversion print product version and continue
-Jre-restrict-search |-jre-no-restrict-search
Include/exclude user private JREs in the version search
-? -Help print this help message
-X print help on non-standard options
-Ea [: <packagename>... |: <classname>]
-Enableassertions [: <packagename>... |: <classname>]
Enable assertions
-Da [: <packagename>... |: <classname>]
-Disableassertions [: <packagename>... |: <classname>]
Disable assertions
-Esa |-enablesystemassertions
Enable system assertions
-Dsa |-disablesystemassertions
Disable system assertions
-Agentlib: <libname> [= <options>]
Load native agent library <libname>, e.g.-agentlib: hprof
See also,-agentlib: jdwp = help and-agentlib: hprof = help
-Agentpath: <pathname> [= <options>]
Load native agent library by full pathname
-Javaagent: <jarpath> [= <options>]
Load Java programming language agent, see java. lang. instrument

In short, do not make a mistake in the step sequence (step 1, step 2, step 3). If you forget the parameter, go to du Niang.

Example '(code in Bat)

Bat1.bat source

----------------------------------------------------------

REM @ echo off

REM re-compiled B. java into B. class
% JAVA_HOME % \ bin \ javac-cp lib \ A. jar; ......-encoding UTF-8 sample \ B. java

REM updates B. class in S. jar
% JAVA_HOME % \ bin \ jar cf lib \ S. jar sample \ B. class

REM sets internal environment variables (this step depends on the source situation, and some do not need)
Set FUNC_LANG = zh
Set INNER_HOME =...
PATH = % INNER_HOME % \ bin; % PATH %

REM actually calls another. bat file to run the. java File
Bat2.bat sampleFile \ test. tif> C: \ test \ result.txt

 

Bat2.bat source

----------------------------------------------------------

@ "% JAVA_HOME % \ bin \ java"-cp lib \ A. jar;...-Dxx = % ss % sample. B "% ~ 1"

The purpose of this example is to modify the B. class file in S. jar (. lib \ S. jar) in the lib folder, and update it.

S. jar Tree:

S. jar
| _____ Sample
| ______ B. class
| _____ META-INF

| ______ MANIFEST. MF

Therefore, to change the package, you must familiarize yourself with the structure of the package, back up the package, and then change it.

※: The last prompt is a key point that is easily ignored. in step 1 of compilation, the entity source is stored. If

Do not want to change a lot of source, compile at a time, and directly update

Class. To save the. java file after rewriting, you must create a file relative to the package to be modified.

Path with the same location relationship; otherwise, an error occurs. For example, if you overwrite the B. java file

In the source directory, create a sample folder and put the B. java file that you have rewritten in it.

This can also be seen from the Code such as package sample in B. java.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.