1

I have a java project build using Maven "quickstart" template. The structure looks like

src
  main
    java
      myapp
        HelloWorld.java

The HelloWorld.java is declared to be under package "myapp"

inside pom.xml, only junit is included.

Now, after I run maven compile, I get a class file under

target
  classes
    myapp
      HelloWorld.class

but after I cd to target/classes/myapp, and do

java -cp . HelloWorld

I get NoClassDefFoundError, i did it according to How to execute a java .class from the command line

I tried

java -cp . myapp.HelloWorld    (myapp is the package)

I still get "could not find or load main class"

My question is: I set the classpath using "-cp .", then why i still can't run the class? does it has anything to do with Maven?

I know I don't need to use command line, I am just curious.

Community
  • 1
  • 1
user2628641
  • 2,035
  • 4
  • 29
  • 45
  • I assume that you need to specify the package name - i.e. go to target/classes and run `java -cp . myapp.HelloWorld` – stdunbar May 26 '16 at 21:32
  • @PetterFriberg, I read all those questions, but the solutions not work for me, that's why I ask this question, and I knew it has a high change to get downvote because of that. I just want someone to take 3 minutes to reproduce it, I am really frustrated over this seemed simple question.. – user2628641 May 26 '16 at 22:05
  • 2
    The problem is like in the linked question: you need to be careful with the package declaration. `HelloWorld` is a class under the `myapp` package. So to run it, you **must not** `cd` into `myapp`. You should `cd` to the root folder which is `target/classes` here and invoke `java myapp\HelloWorld`. That said, it would be preferable for you to make a JAR out of your project (which Maven creates under `target` by default), `cd` to the basedir and launch the jar with `java -jar target\name-of-jar.jar` – Tunaki May 26 '16 at 22:08
  • @Tunaki, your suggestion works, but could you explain why java -cp . works here http://stackoverflow.com/questions/1279542/how-to-execute-a-java-class-from-the-command-line ? – user2628641 May 26 '16 at 22:22

1 Answers1

5

-cp . indicates to Java that you want it to look in the current directory for classes. To make this work, you'd need to use:

java -cp target/classes myapp.HelloWorld 

(Note that "myapp" is the package that your HelloWorld class is defined in, and is declared on the first line of your class, as

package myapp;

)

By using the above java command, you don't need to cd into the target/classes/myapp directory. If you want to cd into target/classes, you can use the following command instead:

java -cp . myapp.HelloWorld 

UPDATE: Sample Project Contents:

app/
app/pom.xml
app/src/
app/src/main/
app/src/main/java/
app/src/main/java/myapp/
app/src/main/java/myapp/HelloWorld.java

after running mvn clean install, the structure changes to:

app/
app/pom.xml
app/src/
app/src/main/
app/src/main/java/
app/src/main/java/myapp/
app/src/main/java/myapp/HelloWorld.java
app/target/
app/target/classes/
app/target/classes/test-1.0.0.jar
app/target/classes/myapp/
app/target/classes/myapp/HelloWorld.class

After compiling, I was able to cd to app/target/classes, and run java -cp . myapp.HelloWorld

If you're still having trouble, can you post your sample class and pom.xml?

Robert K
  • 472
  • 4
  • 17
  • I tried it, it doesn't work. I mean, in theory it should work, but I guess it has to do with maven. Could you create a simple project and try it? – user2628641 May 26 '16 at 21:39
  • java -cp target/classes myapp.HelloWorld works, but after i cd and then use java -cp . myapp.HelloWorld , it can't load main class – user2628641 May 26 '16 at 21:45
  • Where did you cd to? I just ran through a sample application, and updated my answer with the project's the structure. – Robert K May 27 '16 at 13:01
  • I knew what concept I got wrong, I cd into the most inner directory. Since my java file is put under a package, I shouldn't do that. Thank you! – user2628641 May 27 '16 at 20:37