1

So I was following this tutorial: https://spring.io/guides/gs/maven/

I cloned their repositories for the software for fear of mistyping something. The code does not work when I compile Greeter.java using javac and then use java to run HelloWorld.java file It gives me the following error:

HelloWorld.java:5: error: cannot find symbol
        Greeter greeter = new Greeter();
        ^
  symbol:   class Greeter
  location: class HelloWorld
HelloWorld.java:5: error: cannot find symbol
        Greeter greeter = new Greeter();
                              ^
  symbol:   class Greeter
  location: class HelloWorld
2 errors

I tried explicitly importing Greeter into HelloWorld using ìmport hello.Greeter The code works fine when I run it without the package hello;statements.

Any idea why I am getting this error??

So I followed through with the tutorial and using mvn package command and the jar file generated the project works.

So is this issue with trying to compile it with java command in the command line.

Adding directory structure of the project

pom.xml src     target

./src:
main

./src/main:
java

./src/main/java:
hello

./src/main/java/hello:
Greeter.java    HelloWorld.java


2
  • show code of both classes Commented Dec 15, 2019 at 11:42
  • If you are unable to compile Greeter, you will not be able to run the HelloWorld class either. Makes sense! Now, how are you trying to compile the greeter class? What is your working directory and what is your javac command? Commented Dec 15, 2019 at 11:51

1 Answer 1

5

I assume you try to compile the sources while you're in the directory src/test/java/hello. That's the wrong directory, you have to do it from directory src/test/java and pass the directory (i.e. package) to the compiler, e.g.

javac hello/*.java

Another reason might be that you haven't compiled Greeter.java, so the compiler doesn't find the class-file while compiling Hello.java. Above command should solve that.

If you have a main method in hello run java hello/name-of-file.java to run your main method.

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.