标签:des blog http io ar os 使用 sp java
参考自:http://blog.csdn.net/xxyyww/article/details/5784803
前言:
在命令行运行JAVA的时候,要做的事情就不是点一下鼠标就可以了。如果再用到第三方JAR包,命令就更长了。一个比较好的方法就是使用ant,不过我发现一个问题,在命令行中使用ant的时候,它的输入和eclipse的console一样蛋疼。比如:
Scanner in = new Scanner(System.in);
System.out.println("请输入一个整数:");
int a = in.nextInt();
这三行代码的运行结果:
23
请输入一个整数
这显示不是我想要的,所以我这里用的是Makefile。另一个原因就是我还不太会ant
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$
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.Mainjava
这个也不用说
-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文件分开
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$
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这里和上面的一样
标签:des blog http io ar os 使用 sp java
原文地址:http://my.oschina.net/iamhere/blog/355800