无法从命令行

时间:2015-10-31 21:23:13

标签: java

我编译了一个java项目并生成了.class个文件。如果我尝试运行下面的代码(请参阅https://stackoverflow.com/a/12044735/147530):

java com.mycompany.mysubdomain.App

我收到此错误:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

可以理解,因为我没有指定可以找到com.mysql.jdbc.Driver的类路径。所以我跑了:

mvn dependency:build-classpath

获取我的依赖项的类路径。然后我尝试了这样跑:

java -classpath /Users/me/.m2/repository/mysql/mysql-connector-java/5.1.6/mysql-connector-java-5.1.6.jar:/Users/me/.m2/repository/com/vividsolutions/jts/1.13/jts-1.13.jar:/Users/me/.m2/repository/junit/junit/4.11/junit-4.11.jar:/Users/me/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar com.mycompany.mysubdomain.App

但现在我明白了:

Error: Could not find or load main class com.mycompany.mysubdomain.App

我不明白为什么它现在抱怨。请注意关键的事情 - 当我省略类路径时,它能够找到App。发生ClassNotFoundException因为无法找到依赖项。但现在为什么它抱怨它找不到App开始?我怎样才能解决这个问题?

1 个答案:

答案 0 :(得分:4)

通过将您自己的包树所在的目录添加到类路径中:

java -cp .:/Users/me/.m2/repository/mysql/mysql-connector-java/5.1.6/mysql-connector-java-5.1.6.jar:/Users/me/.m2/repository/com/vividsolutions/jts/1.13/jts-1.13.jar:/Users/me/.m2/repository/junit/junit/4.11/junit-4.11.jar:/Users/me/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar com.mycompany.mysubdomain.App