0

I know I can use mvn exec:java ... to run my Maven project. But since I want to learn more about Java, I'm trying to run my project with the java command. Here is my project structure:

pom.xml
src/
    main/java/com/company/prod/
        Obj.java
        Main.java
target/
    classes/com/company/prod/
        Obj.class
        Main.class

I'm trying to run Main by executing java -cp target/classes/com/company/prod target/classes/com/company/prod/Main. But I get java.lang.ClassNotFoundException: target.classes.com.company.prod.Main.

I also tried to cd into the target/classes/com/company/prod/ directory and run java -cp . Main but then I got Error: Could not find or load main class Main Caused by: java.lang.NoClassDefFoundError: com/company/prod/Main (wrong name: Main).

Why is this not working?

Sahand
  • 7,980
  • 23
  • 69
  • 137

2 Answers2

1

Use this:

java -cp target/classes com.company.prod.Main

The class path ends just before the package directories start.

Henry
  • 42,982
  • 7
  • 68
  • 84
  • That worked, only now, it can't find my maven dependencies. So I guess I need to add these dependencies to my classpath manually. – Sahand Nov 20 '20 at 12:39
  • Yes. All dependencies must be reachable via the class path. – Henry Nov 20 '20 at 12:40
0

Mentioned Main class in POM by

<mainClass>src.main.java.org.jis.Main</mainClass>

Create executable jar file and run command java -jar your_jar_name.jar