javac java 命令行編譯運行程序

參考自:http://blog.csdn.net/xxyyww/article/details/5784803 java

前言: 程序員

在命令行運行JAVA的時候,要作的事情就不是點一下鼠標就能夠了。若是再用到第三方JAR包,命令就更長了。一個比較好的方法就是使用ant,不過我發現一個問題,在命令行中使用ant的時候,它的輸入和eclipse的console同樣蛋疼。好比: shell

Scanner in = new Scanner(System.in); dom

System.out.println("請輸入一個整數:"); eclipse

int a = in.nextInt(); ide

這三行代碼的運行結果: 佈局

23 this

請輸入一個整數 spa

這顯示不是我想要的,因此我這裏用的是Makefile。另外一個緣由就是我還不太會ant .net

PS:這裏不討論例如不打包或者.class文件與.java文件在同一目錄中的狀況

不帶第三方包的

個人文件佈局


laolang@laolang:~/code/gongda/data_java/test3$ tree
.
├── classes
├── Makefile
└── src
    └── com
        └── laolang
            ├── domain
            │   └── Person.java
            └── main
                └── Main.java

6 directories, 3 files
laolang@laolang:~/code/gongda/data_java/test3$
Person.java



laolang@laolang:~/code/gongda/data_java/test3$ cat src/com/laolang/domain/Person.java 
     1	package com.laolang.domain;
     2	
     3	public class Person {
     4	
     5		public Person() {
     6			super();
     7			// TODO Auto-generated constructor stub
     8		}
     9	
    10		public Person(String name, String birthday, String desc) {
    11			super();
    12			this.name = name;
    13			this.birthday = birthday;
    14			this.desc = desc;
    15		}
    16	
    17		@Override
    18		public String toString() {
    19			return "Person [name=" + name + ", birthday=" + birthday
    20					+ ", desc=" + desc + "]";
    21		}
    22	
    23		public String getName() {
    24			return name;
    25		}
    26	
    27		public void setName(String name) {
    28			this.name = name;
    29		}
    30	
    31		public String getBirthday() {
    32			return birthday;
    33		}
    34	
    35		public void setBirthday(String birthday) {
    36			this.birthday = birthday;
    37		}
    38	
    39		public String getDesc() {
    40			return desc;
    41		}
    42	
    43		public void setDesc(String desc) {
    44			this.desc = desc;
    45		}
    46	
    47		private String name;
    48		private String birthday;
    49		private String desc;
    50	}
laolang@laolang:~/code/gongda/data_java/test3$
Main.java



laolang@laolang:~/code/gongda/data_java/test3$ cat src/com/laolang/main/Main.java 
     1	package com.laolang.main;
     2	
     3	
     4	import com.laolang.domain.Person;
     5	
     6	public class Main {
     7	
     8		public static void main(String[] args) {
     9			
    10			Person person = new Person("小代碼","91-12-06","程序員");
    11			System.out.println(person);
    12			
    13		}
    14	}
laolang@laolang:~/code/gongda/data_java/test3$


個人Makefile



1	
     2	# javac 命令
     3	JAVAC=javac
     4	# java命令
     5	JAVA=java
     6	
     7	# .class 文件目錄
     8	CLASSES_DIR=classes
     9	
    10	# java源文件所在路徑
    11	JAVA_SRC=src
    12	
    13	# 啓動類類名
    14	MAINCLASS=com.laolang.main.Main
    15	
    16	# 啓動類文件所在路徑
    17	MAINFILE=src/com/laolang/main/Main.java
    18	
    19	
    20	comp:
    21		$(JAVAC) -sourcepath $(JAVA_SRC)  $(MAINFILE) -d $(CLASSES_DIR)
    22	
    23	run:
    24		$(JAVA) -classpath $(CLASSES_DIR) $(MAINCLASS)
    25	
    26	clean:
    27		cd classes;rm -rfv com
    28	
laolang@laolang:~/code/gongda/data_java/test3$


   運行效果:



aolang@laolang:~/code/gongda/data_java/test3$ tree
.
├── classes
├── Makefile
└── src
    └── com
        └── laolang
            ├── domain
            │   └── Person.java
            └── main
                └── Main.java

6 directories, 3 files
laolang@laolang:~/code/gongda/data_java/test3$ make
javac -sourcepath src  src/com/laolang/main/Main.java -d classes
laolang@laolang:~/code/gongda/data_java/test3$ tree
.
├── classes
│   └── com
│       └── laolang
│           ├── domain
│           │   └── Person.class
│           └── main
│               └── Main.class
├── Makefile
└── src
    └── com
        └── laolang
            ├── domain
            │   └── Person.java
            └── main
                └── Main.java

10 directories, 5 files
laolang@laolang:~/code/gongda/data_java/test3$ make run
java -classpath classes com.laolang.main.Main
Person [name=小代碼, birthday=91-12-06, desc=程序員]
laolang@laolang:~/code/gongda/data_java/test3$ make clean
cd classes;rm -rfv com
已刪除"com/laolang/main/Main.class"
已刪除目錄:"com/laolang/main"
已刪除"com/laolang/domain/Person.class"
已刪除目錄:"com/laolang/domain"
已刪除目錄:"com/laolang"
已刪除目錄:"com"
laolang@laolang:~/code/gongda/data_java/test3$ tree
.
├── classes
├── Makefile
└── src
    └── com
        └── laolang
            ├── domain
            │   └── Person.java
            └── main
                └── Main.java

6 directories, 3 files
laolang@laolang:~/code/gongda/data_java/test3$


命令分析:


{其實也說不上分析,只是一個筆記}

其中編譯的命令是:


javac -sourcepath src  src/com/laolang/main/Main.java -d classes


javac

        這個就不用說了

-sourcepath src

        來自javac自帶的說明:-sourcepath <路徑>           指定查找輸入源文件的位置

        也就是指定源文件的位置

src/com/laolang/main/Main.java

        啓動類的源文件

-d classes

        來自javac自帶的說明:-d <目錄>                    指定放置生成的類文件的位置

        這裏說是指定生成的.class文件放到哪裏



運行的命令:


java -classpath classes com.laolang.main.Main
java


        這個也不用說

-classpath classes

        -classpath 和 -cp 同樣,區別就是一個是全稱一個是簡稱,指定.class文件所在目錄

com.laolang.main.Main

        啓動類的.class文件


規範命令和代碼佈局

這裏要說明的是好比個人源文件放在src下

Main.java與Person.java的打包代碼分別是

        Main.java:package com.laolang.main;

        Person.java:package com.laolang.domain;

那麼個人Main.java的路徑應該是 src/com/laolang/main/Main.java

Person.java的路徑應該是 src/com/laolang/domain/Person.java

而後我應該創建一個目錄classes或者其它的什麼,用來存放.class文件,這樣使用javac命令時,使用-d classes選項就能夠把.java文件和.class文件分開


帶第三方jar包的

代碼佈局:


laolang@laolang:~/code/gongda/data_java/test2$ tree
.
├── classes
├── data
│   └── Person.xml
├── lib
│   └── dom4j.jar
├── Makefile
└── src
    └── com
        └── laolang
            ├── domain
            │   └── Person.java
            ├── handle
            │   └── XmlHandle.java
            └── main
                └── Main.java

9 directories, 6 files
laolang@laolang:~/code/gongda/data_java/test2$
XmlHandle.java



laolang@laolang:~/code/gongda/data_java/test2$ cat src/com/laolang/handle/XmlHandle.java 
     1	package com.laolang.handle;
     2	
     3	import java.io.File;
     4	import java.util.Iterator;
     5	
     6	import org.dom4j.Document;
     7	import org.dom4j.DocumentException;
     8	import org.dom4j.Element;
     9	import org.dom4j.io.SAXReader;
    10	
    11	public class XmlHandle {
    12		
    13		public static void main(String[] args) {
    14			try {
    15				XmlHandle handle = new XmlHandle("data/Person.xml");
    16				handle.show();
    17			} catch (DocumentException e) {
    18				e.printStackTrace();
    19			}
    20		}
    21	
    22		public XmlHandle() {
    23			super();
    24		}
    25	
    26		public XmlHandle(String fileName) throws DocumentException {
    27			this.fileName = fileName;
    28			SAXReader sax = new SAXReader();
    29			this.document = sax.read(new File(fileName));
    30		}
    31	
    32		@Override
    33		public String toString() {
    34			return "XmlHanle [fileName=" + fileName + "]";
    35		}
    36	
    37		public void show() {
    38			Element root = this.document.getRootElement();
    39			list(root);
    40		}
    41	
    42		private void list(Element element) {
    43			System.out.println(element.getName());
    44	
    45			Iterator it = element.elementIterator();
    46	
    47			while (it.hasNext()) {
    48				Element e = (Element) it.next();
    49				list(e);
    50			}
    51	
    52		}
    53	
    54		public String getFileName() {
    55			return fileName;
    56		}
    57	
    58		public void setFileName(String fileName) {
    59			this.fileName = fileName;
    60		}
    61	
    62		private String fileName;
    63	
    64		private Document document;
    65	
    66	}
laolang@laolang:~/code/gongda/data_java/test2$
Main.java



laolang@laolang:~/code/gongda/data_java/test2$ cat src/com/laolang/main/Main.java 
     1	package com.laolang.main;
     2	
     3	import org.dom4j.DocumentException;
     4	
     5	import com.laolang.domain.Person;
     6	import com.laolang.handle.XmlHandle;
     7	
     8	public class Main {
     9	
    10		public static void main(String[] args) {
    11			
    12			Person person = new Person("小代碼","91-12-06","程序員");
    13			System.out.println(person);
    14			
    15			try {
    16				XmlHandle handle = new XmlHandle("data/Person.xml");
    17				handle.show();
    18			} catch (DocumentException e) {
    19				e.printStackTrace();
    20			}
    21		}
    22	}
laolang@laolang:~/code/gongda/data_java/test2$
Person.xml



laolang@laolang:~/code/gongda/data_java/test2$ cat data/Person.xml 
     1	<?xml version="1.0" encoding="UTF-8"?>
     2	<PerList>
     3		<Person>
     4			<name>小代碼</name>
     5			<birthday>91-12-06</birthday>
     6			<desc>一個神同樣的男人</desc>
     7		</Person>
     8	
     9		<Person>
    10			<name>天涯</name>
    11			<birthday>91-11-01</birthday>
    12			<desc>流浪者</desc>
    13		</Person>
    14	
    15		<Person>
    16			<name>老狼</name>
    17			<birthday>71-12-06</birthday>
    18			<desc>殺手</desc>
    19		</Person>
    20	
    21		<Person>
    22			<name>行者</name>
    23			<birthday>81-12-06</birthday>
    24			<desc>出家人</desc>
    25		</Person>
    26	</PerList>
laolang@laolang:~/code/gongda/data_java/test2$




我如今的Makefile


laolang@laolang:~/code/gongda/data_java/test2$ cat Makefile 
     1	
     2	# javac 命令
     3	JAVAC=javac
     4	# java命令
     5	JAVA=java
     6	
     7	# .class 文件目錄
     8	CLASSES_DIR=classes
     9	
    10	# java源文件所在路徑
    11	JAVA_SRC=src
    12	
    13	# 啓動類類名
    14	MAINCLASS=com.laolang.main.Main
    15	
    16	# 啓動類所在路徑
    17	MAINFILE=src/com/laolang/main/Main.java
    18	
    19	# dom4j 所在路徑
    20	DOM4J=lib/dom4j.jar
    21	
    22	comp:
    23		$(JAVAC) -sourcepath $(JAVA_SRC) -classpath $(CLASSES_DIR):$(DOM4J) $(MAINFILE) -d $(CLASSES_DIR)
    24	
    25	run:
    26		$(JAVA) -classpath $(CLASSES_DIR):$(DOM4J) $(MAINCLASS)
    27	
    28	clean:
    29		cd classes;rm -rfv com
    30	
laolang@laolang:~/code/gongda/data_java/test2$




運行效果:


laolang@laolang:~/code/gongda/data_java/test2$ tree
.
├── classes
├── data
│   └── Person.xml
├── lib
│   └── dom4j.jar
├── Makefile
└── src
    └── com
        └── laolang
            ├── domain
            │   └── Person.java
            ├── handle
            │   └── XmlHandle.java
            └── main
                └── Main.java

9 directories, 6 files
laolang@laolang:~/code/gongda/data_java/test2$ make
javac -sourcepath src -classpath classes:lib/dom4j.jar src/com/laolang/main/Main.java -d classes
laolang@laolang:~/code/gongda/data_java/test2$ tree
.
├── classes
│   └── com
│       └── laolang
│           ├── domain
│           │   └── Person.class
│           ├── handle
│           │   └── XmlHandle.class
│           └── main
│               └── Main.class
├── data
│   └── Person.xml
├── lib
│   └── dom4j.jar
├── Makefile
└── src
    └── com
        └── laolang
            ├── domain
            │   └── Person.java
            ├── handle
            │   └── XmlHandle.java
            └── main
                └── Main.java

14 directories, 9 files
laolang@laolang:~/code/gongda/data_java/test2$ make run
java -classpath classes:lib/dom4j.jar com.laolang.main.Main
Person [name=小代碼, birthday=91-12-06, desc=程序員]
PerList
Person
name
birthday
desc
Person
name
birthday
desc
Person
name
birthday
desc
Person
name
birthday
desc
laolang@laolang:~/code/gongda/data_java/test2$ make clean
cd classes;rm -rfv com
已刪除"com/laolang/main/Main.class"
已刪除目錄:"com/laolang/main"
已刪除"com/laolang/domain/Person.class"
已刪除目錄:"com/laolang/domain"
已刪除"com/laolang/handle/XmlHandle.class"
已刪除目錄:"com/laolang/handle"
已刪除目錄:"com/laolang"
已刪除目錄:"com"
laolang@laolang:~/code/gongda/data_java/test2$ tree
.
├── classes
├── data
│   └── Person.xml
├── lib
│   └── dom4j.jar
├── Makefile
└── src
    └── com
        └── laolang
            ├── domain
            │   └── Person.java
            ├── handle
            │   └── XmlHandle.java
            └── main
                └── Main.java

9 directories, 6 files
laolang@laolang:~/code/gongda/data_java/test2$




命令分析:

編譯命令


javac -sourcepath src -classpath classes:lib/dom4j.jar src/com/laolang/main/Main.java -d classes
這裏重點是:-classpath classes:lib/dom4j.jar


暫時還不明白這樣寫的原理是什麼,基本上就是有什麼jar包,就在classes後面添加,多個jar包以冒號隔開,特此修正

我試了一個,不帶第三方jar包時,編譯命令加上 -classpath classes也是能夠的

運行的命令:


java -classpath classes:lib/dom4j.jar com.laolang.main.Main
這裏和上面的同樣
相關文章
相關標籤/搜索