OCJP之javac,java,jar命令

来源:互联网 发布:淘宝刷粉丝刷收藏软件 编辑:程序博客网 时间:2024/05/17 02:04

1. classpath

1.1 语法

    javac -cp . Hellworld.java

    注意两点,

    1) -cp .是指当前路径,编译时不能再加上包名,但运行时,需要加上包名;

    2)-cp . 不能写/classes,否则是回到根目录;应该可以写成./classes

    编译时,-cp只需要指定关联库,比如用到了哪个jar包里的类  javac -cp *.jar HelloWorld.javar

    运行时,-cp既需要指定上述关联库,又需要指定编译的class路径,中间用:分隔 java -cp .:pack.jar pack.clients.Client 

1.2 实例

Given that the current directory is bigApp, and the following directory structure:
bigApp
    |-- classes
        -- com
            |-- wickedlysmart
                |-- BigAppMain.class


package com.wickedlysmart;  public class BigAppMain {    public static void main(String[] args) {    System.out.println("big app");  }}

java -cp classes com.wickedlysmart.BigAppMain 

java -cp .:classes com.wickedlysmart.BigAppMain  //1) multiple classpath will use : as concatenation sign 2) . represents current working path, including all packages and corresponding class;


1.3 实例二

Given that the working directory is bigApp, and the following directory structure:
bigApp
    |-- classes
        | |-- com
            | |-- wickedlysmart
    |-- source
        |-- com
            |-- wickedlysmart
                |-- BigAppClass2.java
And the code:
1. public class BigAppClass2 { int doMore() { return 17; } }


javac -d classes source/com/wickedlysmart/BigAppClass2.java   //will compile the file and place the .class file in the classes directory


javac -d classes/com/wickedlysmart source/com/wickedlysmart/BigAppClass2.java   //will compile the file and place the .class file in the wickedlysmart directory


1.4 实例三

2. package apollo;3. import apollo.modules.Lunar;4. public class Saturn {5. public static void main(String[] args){6. Lunar lunarModule = new Lunar();7. System.out.println(lunarModule);8. } }2. package apollo.modules;3. public interface Module { /* more code */ }2. package apollo.modules;3. public class Lunar implements Module { /* more code */ }

And given that Module.java and Lunar.java were successfully compiled and the directory
structure is shown below:


$ROOT
|-- apollo
        | |-- modules
                | |-- Lunar.class
|-- controls.jar
        | |-- apollo
                | |-- modules
                        | |-- Module.class
|-- Saturn.java

The command for compiling is javac -d . -cp . Saturn.java

The command for compiling is javac -d . -cp .:controls.jar Saturn.java

The command for running is java -cp .:controls.jar apollo.Saturn


In order to successfully compile the Saturn class, you ONLY need the Lunar class to be on the classpath

because in order to successfully run the Saturn class, the classpath should
include BOTH the JAR file and the base location of the Saturn and Lunar classes. Here,
the JAR file is required because the Lunar class has to access the Module class at runtime.
When specifying multiple locations for the classpath, each location should be separated
with the ":" symbol, although in real life that character depends on the platform.


1.5 实例四

Given the current directory is bigApp, and the directory structure:
bigApp
    |-- classes
        |-- Cloned.class
        |-- com
            |-- Cloned.class
            |-- wickedlysmart
                |-- Cloned.class
And the three files:

public class Cloned {public static void main(String[] args) { System.out.println("classes"); }}public class Cloned {public static void main(String[] args) { System.out.println("com"); }}public class Cloned {public static void main(String[] args) { System.out.println("ws"); }}

Have been compiled into the classes, com, and wickedlysmart directories, respectively.
Which will produce the output "ws"?

java -cp classes/com/wickedlysmart Cloned

java -cp classes/com/wickedlysmart:classes Cloned

java -cp .:classes/com/wickedlysmart:classes Cloned


use the classpath option (-cp) to find the desired version of
Cloned.class. The ":" separates the various paths to search, and paths are searched in
the order in which they appear in the command line.

1.6 实例五

2. package pack.clients;3. import pack.banking.Bank;4. public class Client{5. public static void main(String[] args){6. Bank bank = new Bank();7. System.out.println(bank.getMoney(2000L));8. } }

And given that Client.java resides in the $ROOT directory and is not yet compiled. There
is another class named pack.banking.Bank, which has a method called getMoney(long)
that returns a value. Bank class is compiled and deployed into a JAR file called pack.jar, as
shown in the following directory structure
$ROOT

    |-- Client.java
    |-- [pack.jar]
        |-- pack
            |-- banking
                |-- Bank.class



To compile, use javac -cp pack.jar -d . Client.java

To run, use java -cp .:pack.jar pack.clients.Client

2 jar命令

语法: jar cf jar-file input-file(s)


它将目录及子目录,全部压缩后,打成jar-file;

存放于META-INF平级的目录下


3 java -D操作Properties


import java.util.*;public class PropertyTest {public static void main(String... arrrrgs) {Properties p = System.getProperties();p.setProperty("pirate", "scurvy");//Property的键值不存时,可以返回null;//可以通过java -DargProp="dog," PropertyTest来赋值,//参数-D将name-value pair写入,value加引号和逗号号。必须放在类前面。//通过上述命令运行后,取出来的p.getProperty("argProp")就是dogString s = p.getProperty("argProp") + " ";s += p.getProperty("pirate");System.out.println(s);} }

Output:

null, scurvy


java -DargPro="Dog," PropertyTest

Output: dog, scurvy



原创粉丝点击