Javac,java usage in "package " && " make executable jar file "

Source: Internet
Author: User
Tags tomcat
Perform
The first command that Java beginners can use is javac and Java, but even this simple command will make our scalp numb-__-. Roger follows a simple explanation for "Javac,java usage in package" and "Making executable jar files."

The most basic thing is not nonsense, as for package first to explain my understanding, because I think it is very helpful to develop. For example, there are two categories Tom and Jerry belong to a package--partners

Tom.javapackage Partners;public class tom{public String toString () {return "It s Tom"; }}

Jerry.javapackage Partners;public class jerry{public static void Main (string[] args) {tom = new Tom           ();    SYSTEM.OUT.PRINTLN (Tom + "and I am Jerry"); }}

Roger below. To compile them as bytecode, @_@, you can use the following command: Tom First, this is obvious, and must be: Javac Tom.java or javac-d. \partners Tom.java

(You should set up path first, as Roger is C:\j2sdk1.4.2\bin, because Javac.exe and Java.exe are all in this directory, they are the source of Javac and Java; the same classpath will be set up, Roger's on the machine., C:\j2sdk1.4.2\lib\dt.jar and C:\j2sdk1.4.2\lib\tools.jar...,tools.jar are the source of the jar commands, but except., the other two are default, not set; If there are other things to set up, such as the machine mounted tomcat should be added to the Tomcat 5.0\common\lib under the jar file, anyway, the third party class libraries are added, So that the JVM can find it. If you want to use Eclipse to set up Tomcat in Eclipse, say it later.

The difference between these two commands is that when the latter is executed, a Tom.class folder is generated in the current directory with the generated. -d Specifies the directory under which the class file is generated, even if a long, nested directory is written, which is generated after the command is executed, not to worry. As for the former, you have to manually create a folder for the Tom.class and move it in. Why do you have to have this folder? It depends on your understanding of package. We know Tom's full name here is Partners.tom, and the JVM transforms it into Partners\tom or partners/tom (see different system) when it handles it. When you want to run it, you should use a directory on the folder of the folders to command: Java partners.tom. (In the directory of the group with this command does not work: Java Tom, or there will be an error said class name can not find, only partners\ Tom is the class name) so the JVM will follow this thread to parse the directory layer to find the Tom.class and execute it. If your package is longer, the same, for example: Com.blogdriver.bubbletea, try it if you don't believe it. Jerry is the same. After all the work is done, we have the following directory structure:

.--partners--tom.class--jerry.class

See here there must be a man will say why so complicated, simply do not use package forget. Package still has a lot of benefits, mainly to make access between classes limited, easy to control and manage. For example, it's good to control between develop and production versions, but if you use an IDE like eclipse, you don't need to be so complicated, just press the Run button.

The next step is to make them executable jar files. First create a. mf file, which writes "main-class:partners.jerry\n", followed by the following directory structure:.--partners--tom.class-- Jerry.class--manifest.mf

Manifest is equivalent to a manifest, which contains information such as the main class of execution, and other names.

Then run the following command: Jar CVFM Test.jar MANIFEST.MF (as far as CVFM is, look at this: Jar-help,help is very useful AH) This is actually a compression process. Then the executable jar file is generated, If the applet and so on the file double-click can be executed, and this example of double click after the results quickly appear and disappeared, the normal human eye is estimated to be invisible, use the following command bar: Java-jar Test.jar. See the results. The following Roger gives an example of an applet, With the steps above, you can see the obvious results by double-clicking the jar file.

Import Javax.swing.*;import java.awt.*;p ublic class MyApplet extends JApplet {public void init () {Getcontentpane (). A  DD (New JLabel ("applet!"));    public static void Main (string[] args) {JApplet applet = new MyApplet ();        JFrame frame = new JFrame ("Applet");    Frame.setdefaultcloseoperation (Jframe.exit_on_close);    Frame.getcontentpane (). Add (applet);    Frame.setsize (100,50);    Applet.init ();    Applet.start ();  Frame.setvisible (TRUE); }}

Commands are as follows: Javac Myapplet.javajar CVFM Test2.jar anothermf.mf myapplet.class

Note that this must have a main function, some applets do not have to add a bar, similar to the above example, mainly the Japplet.init () and Japplet.start () method. Other uses of Jars No, Roger said it, I'll see for myself with-help.




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.