Reference from: http://blog.csdn.net/xxyyww/article/details/5784803
Objective:
When running Java at the command line, the thing to do is not to click on the mouse. If a third-party jar package is used again, the command is longer. A good way to do this is to use ant, but I found a problem when using ant in the command line, its input is just as painful as the Eclipse console. Like what:
Scanner in = new Scanner (system.in);
System.out.println ("Please enter an integer:");
int a = In.nextint ();
The results of these three lines of code:
23
Please enter an integer
This show is not what I want, so I use makefile here. Another reason is I'm not very good at ant.
PS: Not discussed here such as not packing or. class files in the same directory as the. java files
Without a third-party packageMy file layout
[Email protected]:~/code/gongda/data_java/test3$ tree.├──classes├──makefile└──src └──com └──laolang ├──domain │└──person.java └──main └──main.java6 directories, 3 Files[email Protected]:~/code/gongda/da ta_java/test3$
Person.java
[email protected]:~/code/gongda/data_java/test3$ Cat Src/com/laolang/domain/person.java 1package Com.laolang.domain; 2 3public class Person {4 5public person () {6super (); 7//TODO auto-generated constructor stub 8} 9 10public Person (string name, string birthday, String desc) {1 1super (); 12this.name = name; 13this.birthday = Birthday; 14THIS.DESC = desc; 17@override 18public String toString () {19return "person [name=" + name + ", birthday=" + Birthday 20+ ", desc=" + desc + "]"; 23public String GetName () {24return name; 27public void SetName (String name) {28this.name = name; 31public String Getbirthday () {32return birthday; 35public void Setbirthday (String birthday) {36this.birthday = birthday; PNS 39public String GetDesc () {40return desc; 43public void Setdesc (String desc) {44this.desc = Desc 47private String name; 48private String birthday; 49private String desc; 50}[email protected]:~/code/gongda/data_java/test3$
Main.java
[Email protected]:~/code/gongda/data_java/test3$ cat Src/com/laolang/main/main.java 1package com.laolang.main; 2 3 4import Com.laolang.domain.Person; 5 6public class Main { 7 8public static void Main (string[] args) { 9 10Person person = new Person ("small Code "," 91-12-06 "," programmer "); 11SYSTEM.OUT.PRINTLN (person); 14}[email protected]:~/code/gongda/data_java/test3$
My makefile.
1 javac command 3javac=javac 4# java command 5java=java 6 7#. class file directory 8classes_dir=classes 9 10# Java source file path 11java_src=src 13# start class name 14mainclass=com.laolang.main.main 16# Startup class file path 17mainfile=src/com/laolang/main/main.java 20comp: 21 $ (JAVAC)-sourcepath $ (JAVA_SRC) $ (mainfile)-D $ (classes_dir ) 23run: 24$ (JAVA)-classpath $ ( Classes_dir) $ (MAINCLASS) 26clean: 27cd classes;rm-rfv com 28[email protected]:~/code/ gongda/data_java/test3$
Operating effect:
[Email protected]:~/code/gongda/data_java/test3$ Tree.├──classes├──makefile└──src└──com└──laolang ├──DOMAIN│  └──PERSON.JAVA└──MAIN└──MAIN.JAVA6 Directories , 3 files[email protected]:~/code/gongda/data_java/test3$ makejavac-sourcepath src src/com/laolang/main/ Main.java-d classes[email protected]:~/code/gongda/data_java/test3$ tree.├──classes│ └──com│ └──laolang│ ├──domain│ │ └──person.class│ &nb Sp └──main│ └──main.class├──makefile└──src└──com└──laolang├──domain │  └──PERSON.JAVA└──MAIN└──MAIN.JAVA10 directories, 5 files[email protected]:~/code/gongda/data_java/test3$ make Runjava-classpath classes Com.laolang.main.MainPerson [name= Small Code, birthday=91-12-06, desc= programmer][EMAIL PRotected]:~/code/gongda/data_java/test3$ make cleancd classes;rm-rfv com deleted "Com/laolang/main/main.class" deleted directory: "com/ Laolang/main "deleted" Com/laolang/domain/person.class "deleted directory:" Com/laolang/domain "deleted directory:" Com/laolang "deleted directory:" com "[ email protected]:~/code/gongda/data_java/test3$ Tree.├──classes├──makefile└──src└──com└──laolang ├──DOMAIN│  └──PERSON.JAVA└──MAIN└──MAIN.JAVA6 directories, 3 files[email protected]:~/code/gongda/data_java/test3$
Command analysis:
{In fact, no analysis, just a note}
The commands which are compiled are:
Javac-sourcepath src src/com/laolang/main/main.java-d classes
Javac
I don't have to say that.
-sourcepath SRC
Instructions from Javac:-sourcepath < paths > specifying where to find input source files
That is, the location of the specified source file
Src/com/laolang/main/main.java
Start the source file for the class
-D Classes
Instructions from Javac:-D < directory > specify where to place the generated class file
This is where the specified generated. class file is placed
command to run:
Java-classpath Classes Com.laolang.main.Main
Java
That's not to mention.
-classpath classes
-classpath and-CP, the difference is that a full name is a short, specify the directory where the. class file resides
Com.laolang.main.Main
The. class file for the startup classes
Canonical command and Code layout
This is to say, for example, my source files are placed under SRC
The packing code for Main.java and Person.java is
Main.java:package Com.laolang.main;
Person.java:package Com.laolang.domain;
Then my Main.java's path should be Src/com/laolang/main/main.java.
The path to the Person.java should be Src/com/laolang/domain/person.java
Then I should create a directory classes or something, to store the. class file, so that using the-D classes option, you can separate the. java file from the. class file with the Javac command.
With third-party jar packagesCode layout:
[Email protected]:~/code/gongda/data_java/test2$ Tree.├──classes├──data│└──person.xml├──lib│└──dom4j.jar├──mak EFILE└──SRC └──com └──laolang ├──domain │└──person.java ├──handle │└──xmlhandle.java< C6/>└──main
Xmlhandle.java
[email protected]:~/code/gongda/data_java/test2$ Cat Src/com/laolang/handle/xmlhandle.java 1package Com.laolang.handle; 2 3import Java.io.File; 4import Java.util.Iterator; 5 6import org.dom4j.Document; 7import org.dom4j.DocumentException; 8import org.dom4j.Element; 9import Org.dom4j.io.SAXReader; 11public class Xmlhandle {13public static void main (string[] args) {14try {15XmlHandle handle = NE W xmlhandle ("Data/person.xml"); 16handle.show (); The catch (Documentexception e) {18e.printstacktrace (); 22public Xmlhandle () {23super (); 26public Xmlhandle (String fileName) throws documentexception {27this.filename = FileName; 28SAXReader sax = new Saxreader (); 29this.document = Sax.read (new File); 32@override 33public String toString () {34return "Xmlhanle [filename=" + FileName + "]"; 37public void Show () {38ElemeNT root = This.document.getRootElement (); 39list (root); 42private void list (element element) {43system.out.println (Element.getname ()); 45Iterator it = Element.elementiterator (); 47while (It.hasnext ()) {48Element E = (Element) it.next (); 49list (e); (): 54public String GetFileName () {55return fileName; 58public void Setfilename (String fileName) {59this.filename = FileName; 62private String FileName; 64private document document; 66}[email protected]:~/code/gongda/data_java/test2$
Main.java
[Email protected]:~/code/gongda/data_java/test2$ cat Src/com/laolang/main/main.java 1package com.laolang.main; 2 3import org.dom4j.DocumentException; 4 5import Com.laolang.domain.Person; 6import Com.laolang.handle.XmlHandle; 7 8public class Main { 9 10public static void Main (string[] args) { one 12Person person = new Person ( "Small Code", "91-12-06", "programmer"); 13SYSTEM.OUT.PRINTLN (person); 15try { 16XmlHandle handle = new Xmlhandle ("Data/person.xml"); 17handle.show (); * catch (Documentexception e) { 19e.printstacktrace (); 21}
Person.xml
[Email protected]:~/code/gongda/data_java/test2$ cat data/person.xml 1<?xml version= "1.0" encoding= "UTF-8"? > 2<perlist> 3<person> 4<name> Small code </name> 5<birthday>91-12-06 </birthday> 6<desc> A god-like man </desc> 7</person> 8 9<person> 10 <name> Tianya </name> 11<birthday>91-11-01</birthday> 12<desc> Rangers </desc > 13</person> 15<person> 16<name> old Wolf </name> 17< Birthday>71-12-06</birthday> 18<desc> killer </desc> 19</person> 21<person> 22<name> Walker </name> 23<birthday>81-12-06</birthday> 24 <desc> Monk </desc> 25</person>
My present makefile.
[Email protected]:~/code/gongda/data_java/test2$ cat Makefile 1 * javac command 3javac=javac 4# java command 5java=java 6 7#. class file directory 8classes_dir=classes 9 10# The path where the Java source files are located 11java_src=src 13# Startup class name 14mainclass=com.laolang.main.main 16# the path where the startup class is located 17mainfile=src/com/ Laolang/main/main.java 19# dom4j path 20dom4j=lib/dom4j.jar 22comp: 23$ ( JAVAC)-sourcepath $ (JAVA_SRC)-classpath $ (Classes_dir): $ (dom4j) $ (mainfile)-D $ (classes_dir ) 25run: 26$ (JAVA)-classpath $ (Classes_dir): $ (dom4j) $ (MAINCLASS) 28clean: 29cd classes;rm-rfv com
Operating effect:
[Email protected]:~/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.JAVA9 directories, 6 files[email protected]:~/code/gongda/data_java/test2$ makejavac-sourcepath src- Classpath Classes:lib/dom4j.jar src/com/laolang/main/main.java-d Classes[email protected]:~/code/gongda/data _java/test2$ Tree.├──classes│ └──com│ └──laolang│ ├──domain│ &nbs P │ └──person.class│ ├──handle│ │ └──xmlhandle.class│&nbs p; └──main│ └──main.class├──data│ └──person.xml├──lib│ └──dom4j.jar├──makefile└──src└── com└──laolang├──domain│ └──person.java├──handle│& NBSP;&NBSP;└──XMLHANDLE.JAVA└──MAIN└──MAIN.JAVA14 directories, 9 files[email protecte d]:~/code/gongda/data_java/test2$ make Runjava-classpath classes:lib/dom4j.jar Com.laolang.main.MainPerson [name= Small code, BIRTHDAY=91-12-06, desc= programmer] perlistpersonnamebirthdaydescpersonnamebirthdaydescpersonnamebirthdaydescpersonnamebirthdaydesc[email protected]:~/code/gongda/data_java/test2$ make cleancd classes;rm-rfv com deleted "Com/laolang/main/main.class" Deleted directory: " Com/laolang/main "deleted" Com/laolang/domain/person.class "deleted directory:" Com/laolang/domain "deleted" com/laolang/handle/ Xmlhandle.class "deleted directory:" Com/laolang/handle "deleted directory:" Com/laolang "deleted directory:" com "[email protected]:~/code/gongda/ data_java/test2$ tree.├──classes├──data│ └──person.xml├──lib│ └──dom4j.jar├──makefile└── Src└──com└──laolang├──domain │ └──person.java├──handle│ └──xmlhandle.java└──mai N└──MAIN.JAVA9 directories, 6 files[email protected]:~/code/gongda/data_java/test2$
Command analysis:
Compile command
Javac-sourcepath Src-classpath Classes:lib/dom4j.jar src/com/laolang/main/main.java-d Classes
Here's the point:-classpath Classes:lib/dom4j.jar
For the time being not understand what this writing principle is, basically is what jar package, just add after classes, multiple jar package to; Separated
I tried one, without a third-party jar package, the compile command plus-classpath classes is also possible.
command to run:
Java-classpath Classes:lib/dom4j.jar Com.laolang.main.Main
It's the same here as above.
Javac Java Command-line compilation Run program