Java Class build Jar package (GO)

Source: Internet
Author: User

Enter the DOS operator window
CD into all folder directories of the class file to be hit into the jar package
Jar CVF [Name of the generated jar] [list class file]//If there are multiple, separated by an empty spacer

Such as:
Single or several class files that operate sporadically
Jar CVF Ibstool.jar Com/cn/ibs/turnd.class com/cn/ibs/turnb.class

Two, a folder below all the class file into a jar package
Jar CVF Ibstool.jar * Enter

To build the executable jar package:
The class in the compiled class file must have the main entry function in order to generate the executable jar package, such as:
public static void Main (string[] args)

The generated method steps:
Method One:

1. Use the above method to make the class and the corresponding structure directory into JAR package
2. Unzip the jar package to the current directory
3, edit meta-inf/manifest. MF, specify the program entry main function for the jar package, as follows:

manifest-version:1.0
CREATED-BY:1.5.0_06 (Sun Microsystems Inc.)
Main-class:mutithread (carriage return)

(Note that the content of this file must be standard with a carriage return and a space)

4, then use WinZip to compress these files into a jar format package, or press into a ZIP package, and then change the suffix named jar can also.

Method Two (recommended):
2. Play into the executable jar package
The CMD action form enters the root directory of the class file, the reason is that the root directory, because below it can also have a lot of file package (folder);
A MANIFEST.MF file is prepared in this directory with the following contents:
manifest-version:1.0
CREATED-BY:1.5.0_06 (Sun Microsystems Inc.)
Main-class:mainentry

Main-class is the name of the class where the main method for all classes is specified.
Last command: Jar CVFM Jarname.jar MANIFEST.MF *

3. Test the executable jar package.
With command: Java-jar Jarname.jar

4. Make a bat file for the executable jar package.
For example:
@echo off
Rem
REM Set Bq_ibs_home before running this script.
Rem

If not "%bq_ibs_home%" = = "goto CONT0
Set Bq_ibs_home=c:\ibs
: CONT0
@echo on
Java-dbq_ibs_home=%bq_ibs_home%-jar Jarname.jar
: END

Network references:

Java jar Command
For example, your class package storage path is D:\com\demo (demo is a directory, below contains subfolders and class files)

Here's how:
1. Open the DOS window and enter the demo directory.
CD D:\com\demo
2. Execute Jar CVF Demo.jar * Enter
3, under the demo directory you will find Demo.jar

For specific parameters, please see the following explanations:

Usage: jar {ctxu}[vfm0mi] [jar-file] [manifest-file] [-C directory] File name ...
Options:
-C Create a new archive
-t lists the list of archived content
-X expands the named (or all) file in the archive
-U updates an existing archive
-V generates verbose output on standard output
-f Specifies the archive file name
-M contains marked information from the indicated file
-0 storage mode; Zip compression format not used
-M does not produce a list of all items (manifest) file
-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 will be processed recursively.

The manifest (both the Manifest) file name and the archive file name need to be specified, in the same order specified by the ' m ' and ' F ' flags.

Example 1: Archive two class files into an archive file named ' Classes.jar ':
Jar CVF Classes.jar Foo.class bar.class

Example 2: Archive all files under the foo/directory to an archive file named ' Classes.jar ' with an existing manifest (manifest) file ' mymanifest ':
Jar CVFM Classes.jar mymanifest-c foo/.


Let's try this for a little example:

We have only one HelloWorld, as follows:

public class helloworld{
public static void Main (string[] args) {
System.out.println ("Hi, Hello world!");
}
}

Save this Java file to the C drive and directory, OK, Next,

At a previously opened command prompt (jump to the C-drive prompt), we entered Javac Helloworld.java, and then continue to enter: Jar CVF Hello.jar Helloworld.class, go to your C-drive after the carriage return to see, more what, yes Hello.jar.

Basic steps we all know now, you can try it on your own. As the parameters behind the jar differ, the results change.
And then we look at how to run our jar package.

Before you get to the point, you have to open the jar package we just made and see, what's more, Meta-inf directory? And see what's inside, and a manifest.mf file, isn't it? Open it with a text editor (I'm UltraEdit here) to see:

manifest-version:1.0
created-by:1.4.2 (Sun Microsystems Inc.)

That's it. Here we modify it, add a sentence: Main-class:helloworld (in the third row). This is the class we wrote earlier, which is our entry class. That is,
manifest-version:1.0
created-by:1.4.2 (Sun Microsystems Inc.)
Main-class:helloworld
Next, we execute at the command prompt:
Jar UMF MANIFEST. MF App.jar (should be Hello.jar bar)
This allows us to use our own MANIFEST.MF file to update the original default. You might as well go inside and see if you've added Main-class:helloworld to this sentence. (yes, I did not try to come out, hint Java.io.FileNotFoundException:MANIFEST.) MF (the system cannot find the file specified) What's going on? )
Ok, this last step, to verify everything we do, at the command prompt, enter:
Java-jar Hello.jar (Executive)
What happened, Hi, Hello world!
Let's look at the jar file released in Tomcat, note: In Tomcat we can no longer use the format jar, and the war format, it is dedicated to the Web application, in fact, the whole process is basically similar to the jar:
Prepare the resources we want to pack first.
Find the WebApps directory where tomcat is stored, enter it, create a new folder, name it Hello, go in to create a new Web-inf folder, go inside the new classes folder, and we will also be our only servlet, Helloworld.java put it here, create a file, Web. XML, with the classes directory peer. Ok, we have initially built a simple Web application.
About MANIFEST. MF notation.
For example, our packaged jar is Test.jar
manifest-version:1.0
Main-class:windows. Videowindow
Class-path:lib\org.eclipse.swt_3.3.0.v3346.jar Lib\org.eclipse.swt.win32.win32.x86_3.3.0.v3346.jar
The package description file is packaged as a jar above. The following error has been present.
Could not find main class. Program would exit
This error message is not actually found in the main class. It's the jar. This error can occur if an error is not executed. After packaging is complete, run cmd-java-jar Your.jar to confirm that there are no problems to execute the jar. After using the-jar parameter, the system's CLASSPATH variable no longer works. The virtual machine will go to MANIFEST.MF in the Class-path to find the relevant package.
Then you can succeed from: Windows. Videowindow's main method enters. Otherwise, there is no associated connection package, and you will be prompted to find the main class. And people may still be confused to find the north. Is there a main class?
If that's not the right thing to write. will always be the error.
Take a look at the above Class-path: the following notation. Separate spaces. There must be no semicolon. Remember.
Finally, there must be a line break. This is also to be noted. Otherwise the hint is not mainclass.
Don't delay your time because of this little thing. Where lib/indicates that the jar must be in a Lib subdirectory of the directory where the Test.jar is packaged.
command to run the jar: Java-jar Test.jar
Double-click can also, then use JAVAW to open.
About Java-classpath A.jar;b.jar; Class Test. When Test executes. A.jar;b.jar; There must be a semicolon behind them. Otherwise the error. Don't expect the system to look for itself without writing classpath. This is not in eclipse. You must write the class you want to use. Otherwise, you will always be prompted to compile an error.

Can be used on the:
1. Main-class Specifies the entry of the program, so that the program can be run directly with Java-jar Xxx.jar.
2. Class-path specifies the dependency of the jar package, and class loader searches for the class based on this path.
I usually use MF files are generally used to make the jar can be directly run this purpose. Although the purpose is so simple, it is easy to have a variety of problems every time you pack up a headache. MF is never applied, or it's a mistake.
Let's start with a simple and standard example:

manifest-version:1.0
Main-class:ui. Jframemain
Class-path:jtds-1.2.jar C3p0-0.9.1.2.jar
Esri_mo20.jar Esri_mo20res.jar Jsde83_sdk.jar

Explain:
Manifest-version MF File version number, this line copied
Main-class contains the class of the main function, the entry of the program
Some of the packages used by Class-path

Analyze the main points:

Point 1: Each line of ":" (colon) is used to separate key-value pairs. After the colon must be followed by a space!!!
This means that it must be written as:

Main-class: (space) Youclassname
Class-path: (space) Class1.jar

Otherwise JBuilder will report ("MANIFEST.MF": Error reading Manifest:java.io.IOException:invalid header field
) Such a mistake. The jar, which is no longer done with the help of the IDE, will find that MF does not play its proper role. By the way, I've seen the JBuilder report another mistake. It's probably the wrong line break, and it seems to be caused by this problem. The original thought is that the carriage return in Windows is caused by a carriage return, a newline of 2 characters. Now it seems that it is not, because there is less space behind the colon.

Important 2:class-path the contents are separated by spaces instead of commas or semicolons. (Alas ...) I always subconsciously use a semicolon)
In other words, be sure to write:
Class-path: (space) Class1.jar (space) Class2.jar (space) Class3.jar

Point 3: Some articles say that the MF file can not exceed more than 70 characters per line, my experience is that if the JBuilder edit MF more than the same, I usually try to finish the line. The continuation of a very troublesome and the beginning of a space or something. I also often directly with the WinRAR open jar and then directly edit the inside of the MF file, this is a good way, very direct when, the disadvantage is that if you change the bad will not error, only MF Some part of the failure, as if the runtime will also say "wrong MF file version."

Here are a few tips:

Tip 1: Use WinRAR to open the jar, double-click the MF file, you can edit, edit ctrl+s save. Special convenience. You can also write a manifest.mf on the outside, and then drag directly into the previous overlay.
Tip 2: Write a run.bat to run your jar. Run.bat general content is as follows:

Java-jar Yours.jar
Pause
Add a pause behind to let you see where the specific error, in general, if the problem in the MF, then often is which package, which class did not find the noclassdef this anomaly.

Http://blog.chinaunix.net/uid-22028680-id-3042272.html

Java Class build Jar package (GO)

Related Article

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.