3

I'm new in Java and can not get idea of using packages. Am I stupid or is this some kind of bug?

Guys, how to deal with this error?

C:\Users\batievskiy\IdeaProjects\JavaPset\src>java Test.java
Test.java:1: error: package pkgcalculate does not exist
import pkgcalculate.Calculator;
                   ^
Test.java:5: error: cannot find symbol
        Calculator obj = new Calculator();
        ^
  symbol:   class Calculator
  location: class Test
Test.java:5: error: cannot find symbol
        Calculator obj = new Calculator();
                             ^
  symbol:   class Calculator
  location: class Test
3 errors
error: compilation failed

enter image description here

this is Calculator code:

package pkgcalculate;

public class Calculator {
    public int add(int a, int b){
        return a+b;
    }
    public static void main(String[] args){
        Calculator obj = new Calculator();
        System.out.println(obj.add(10, 20));
    }
}

from what I got: that I cannot run Test.java from the terminal directly using: java Test.java But if I run Test with Shift + F10, the program is compiled. Why is that? Is it normal behaviour?

Batiievskyi
  • 51
  • 1
  • 4
  • what does the package statement in Calculator say? please show code, instead of images – Stultuske May 20 '21 at 15:56
  • personally, if you are just starting, I would recommend against immediately starting with a professional IDE. – Stultuske May 20 '21 at 15:57
  • I suspect that the classpath is missing - are you able to run the class file in the IDE by right clicking the source code and clicking on "run"? – f1sh May 20 '21 at 15:59
  • I don't see an error in the code, anyway – Stultuske May 20 '21 at 16:02
  • there no errors in the code. But when I.m trying: java Test.java it gives me terminal error: package pkgcalculate does not exist – Batiievskyi May 20 '21 at 16:03
  • @Batiievskyi Try giving it the classpath. And you don't run `java` on a source file. – Dave Newton May 20 '21 at 16:06
  • from what I got: this is that I cannot run Test.java from the terminal directly using: java Test.java But if I run Test with Shift + F10, the program is compiled. Why is that? Is it normal behaviour? – Batiievskyi May 20 '21 at 16:20
  • I would love to use Eclipse instead. But JavaRush course and Nix Solutions Java course force us to use and get used to professional Intellij IDE – Batiievskyi May 20 '21 at 16:49

1 Answers1

1

If you compiling your code from command line you have to compile classes you depend on first.

Or compile them all together in one go.

talex
  • 17,973
  • 3
  • 29
  • 66
  • Thanks a lot.) Is it possible to run it from the terminal? or will only Shift + F10 start the program? – Batiievskyi May 20 '21 at 16:26
  • 1
    I found duplicate for yo question. There is good explanation in answers. https://stackoverflow.com/questions/4764768/java-how-can-i-compile-an-entire-directory-structure-of-code – talex May 21 '21 at 06:37
  • Talex, Thanks for this link.) There are more than enough information for me by now. Especially how to use terminal in what I'm interested in. I'm really appreciate your help.) – Batiievskyi May 21 '21 at 11:23