java.exe jar lib

The Jar tool automatically puts a default manifest with the pathname META-INF/MANIFEST.MF into any JAR file created. Special JAR file functionality can be enabled, such as package sealing, by modifying the default manifest. Typically, modifying the default manifest involves adding special-purpose headers to the manifest that allow the JAR file to perform a particular desired function.

Example:
We have file directory like this:
D:\logtest\.
D:\logtest\main\Hello.java #main
D:\logtest\com\brinado\World.java
D:\logtest\com\brinado\hello\JarTest.java

#Hello.java:
package main;

import com.brinado.hello.*;
import com.brinado.*;

public class Hello{
        public static void main(String[] args){
                JarTest a = new JarTest();
                World w = new World();
        }
}

#World.java:
package com.brinado;

public class World{
    public World(){
        System.out.println("This is the other test in different position of the directory.");
        System.out.println("That's all I want to write here.......");
    }
}

#JarTest.java:
package com.brinado.hello;

public class JarTest{
        public JarTest(){
                   System.out.println("This is only a test.");
                   System.out.println("Next test will be about include a new package into the previous jar file."
                   );
        }
}

First, we compile World.java and JarTest.java.
D:\logtest>javac ./com/brinado/World.java #World.class generated at D:/logtest/com/brinado/
D:\logtest>javac ./com/brinado/hello/JarTest.java #JarTest.class generated at D:/logtest/com/brinado/hello/

Then, we make a jar file to encapsulate World.class and JarTest.class.
D:\logtest>jar cf kknd.jar ./com/brinado/*.class ./com/brinado/hello/*.class #kknd.jar generated at D:/logtest/kknd.jar

Then, we compile main java file Hello.java.
D:\logtest>javac -cp kknd.jar; ./main/Hello.java #Hello.class generated at D:/logtest/main/Hello.java

At last, we run the main class file Hello.class.
D:\logtest>java -cp kknd.jar main.Hello

The result is as follows:
"
This is only a test.
Next test will be about include a new package into the previous jar file.
This is the other test in different position of the directory.
That's all I want to write here.......
"

If we do not like to include that kknd.jar when run main file, we can manually add that to "System environment variables"
1 we 'New' a variable called "KKND_HOME", with value "D:\logtest\"
2 we append behind variable "CLASSPATH" with ";%KKND_HOME%\kknd.jar;"
3 remember to start a new cmd window.

modify MANIFEST.MF file:
Example:
we have everything here:
D:\logtest\main\Hello.class #main
D:\logtest\com\brinado\World.class
D:\logtest\com\brinado\hello\JarTest.class

Or We can also modify MANIFEST.MF manually, adding a newline "Main-Class: packagename.classname" (classname should contain "main" method) -not tested yet

Then, we can add them all to a executable jar file.
D:\logtest>jar cfe app.jar main.Hello ./main/*.class ./com/brinado/*.class ./com/brinado/hello/JarTest.class #app.jar generated at D:\logtest\app.jar
At last, we can run it like:
D:\logtest>java -jar app.jar

你可能感兴趣的:(java)