-1

I compiled a java project and generated .class files. If I try to run my code like below (refer https://stackoverflow.com/a/12044735/147530):

java com.mycompany.mysubdomain.App

I get this error:

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

understandable as I haven't specified the classpath where com.mysql.jdbc.Driver can be found. So I ran:

mvn dependency:build-classpath

to get classpaths of my dependencies. Then I have tried running like so:

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

but now I get this:

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

I don't understand why its complaining now. Note the crucial thing - when I omitted the classpath it was able to find App. The ClassNotFoundException happened because it couldn't find a dependency. But now why is it complaining that it can't find the App to begin with? How can I fix this?

1
  • 2
    Don't you think you should also provide the current directory (there your class is located) as part of the classpath? Commented Oct 31, 2015 at 21:26

1 Answer 1

4

By adding the directory where your own package tree resides to the classpath:

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.