Difference in Compilation output of Java program, When trying to run with 'Intellij IDE' and 'Terminal'

When you run code from IDE, the IDE compiles all the classes in your project. When you run code from the command prompt, YOU must compile all the necessary classes. In this case, you didn't compile anotherClass.

Here is a link on how to compile multiple files from command line: https://docs.oracle.com/en/java/javase/13/docs/specs/man/javac.html. Go down to the section "Example of Compiling Multiple Source Files"

As a side note, use proper naming conventions. The industry-accepted naming convention for classes is "CamelCase".


This is happening because the compiled byte code of file anotherClass.java isn't available at location com/company/com. Check the folder where IntelliJ keeps the .class files and execute them directly.

Additionally, if you want to compile these files manually via terminal, do it from outside the package (src folder).