javac 命令用法
引用自己写的Class
在java中手动编译时,总提示找不到类,调试成功后,特把目录结构与编译成功的命令列出:
样例一:
文件名 |
MessageStore.java |
Hello.java |
源码 |
package test; |
package test;
import test.MessageStore; |
路径 |
E:\test |
E:\test |
编译 |
Javac MessageStore.java |
Javac Hello.java –classpath .. |
样例2:
文件名 |
MessageStore.java |
Hello.java |
源码 |
package test; |
package test;
import test.MessageStore; |
路径 |
E:\src\test |
E:\src\test |
编译 |
Javac MessageStore.java |
Javac Hello.java –classpath .. |
样例3:
文件名 |
MessageStore.java |
Hello.java |
源码 |
package bf.model; |
package bf.action;
import bf.model.MessageStore; |
路径 |
E:\src\bf\model |
E:\src\bf\action |
编译 |
Javac MessageStore.java |
Javac Hello.java –classpath E:\src |
结论:java文件编译后的classpath为不包括包名的路径,如上面E:\src
引用其它Jar包
1,目录结构
E:\test\lib\bson-3.0.4.jar
E:\test\lib\mongodb-driver-3.0.4.jar
E:\test\lib\mongodb-driver-core-3.0.4.jar
E:\test\Quick.java
2,源文件
import com.mongodb.MongoClient; import com.mongodb.client.MongoDatabase; import org.bson.Document; import com.mongodb.Block; import com.mongodb.client.FindIterable; import static com.mongodb.client.model.Filters.*; import static com.mongodb.client.model.Sorts.ascending; import static java.util.Arrays.asList; public class Quick { public static void main(String[] args) { // MongoClientURI connectionString = new MongoClientURI("mongodb://localhost:27017"); MongoClient mongoClient = new MongoClient(); MongoDatabase db = mongoClient.getDatabase("test"); FindIterable<Document> iterable = db.getCollection("restaurants").find(); iterable.forEach(new Block<Document>() { @Override public void apply(final Document document) { System.out.println(document); } }); } }
3,三种编译形式
javac Quick.java -cp E:\test\lib\bson-3.0.4.jar;E:\test\lib\mongodb-driver-core-3.0.4.jar;E:\test\lib\mongodb-driver-3.0.4.jar;
javac -cp E:\test\lib\bson-3.0.4.jar;E:\test\lib\mongodb-driver-core-3.0.4.jar;E:\test\lib\mongodb-driver-3.0.4.jar; Quick.java
javac -Djava.ext.dirs=lib Quick.java
注意:要JDK1.6以上才可以, 1.6以下可以用通配符
4,运行方法
java -cp E:\test\lib\bson-3.0.4.jar;E:\test\lib\mongodb-driver-core-3.0.4.jar;E:\test\lib\mongodb-driver-3.0.4.jar; Quick
常见错误:
1,运行时,带.class后辍
如:java Quick.class
2,运行时 -cp参数位置写错
如:java Quick -cp e:\...
3, 有包名时忘记传包名
如源文件修改为
package com.fb; import com.mongodb.MongoClient; import com.mongodb.client.MongoDatabase; import org.bson.Document; import com.mongodb.Block; import com.mongodb.client.FindIterable; import static com.mongodb.client.model.Filters.*; import static com.mongodb.client.model.Sorts.ascending; import static java.util.Arrays.asList; public class Quick { public static void main(String[] args) { // MongoClientURI connectionString = new MongoClientURI("mongodb://localhost:27017"); MongoClient mongoClient = new MongoClient(); MongoDatabase db = mongoClient.getDatabase("test"); FindIterable<Document> iterable = db.getCollection("restaurants").find(); iterable.forEach(new Block<Document>() { @Override public void apply(final Document document) { System.out.println(document); } }); } }
目录结构相应为:E:\test\com\fb\Quick.java
编译修正为:
cd e:\test
e:\test javac -Djava.ext.dirs=lib E:\test\com\fb\Quick.java
正确的执行方法为:
java -cp E:\test\lib\bson-3.0.4.jar;E:\test\lib\mongodb-driver-core-3.0.4.jar;E:\test\lib\mongodb-driver-3.0.4.jar; com.fb.Quick
4,进入cmd是通过shit + 鼠标右健进入
签名:删除冗余的代码最开心,找不到删除的代码最痛苦!