0

I am trying to to run the file Demo.java which is calling Protection class within the same package but it is giving error This is the main class.

package p1;
// Instantiate the various classes in p1.
class Demo {
  public static void main(String args[]) {
    Protection ob1 = new Protection();
    //Derived ob2 = new Derived();
    //SamePackage ob3 = new SamePackage();
  }
}

And this is the class that I want to use in the main class.

package p1;

public class Protection {

  public int n = 1;
  private int n_pri = 2;
  protected int n_pro = 3;
  public int n_pub = 4;

  public Protection() {
    System.out.println("base constructor");
    System.out.println("n = " + n);
    System.out.println("n_pri = " + n_pri);
    System.out.println("n_pro = " + n_pro);
    System.out.println("n_pub = " + n_pub);
  }
}

It is giving this error:

$ javac Demo.java
Demo.java:6: error: cannot find symbol
Protection ob1 = new Protection();
^
  symbol:   class Protection
  location: class Demo
Demo.java:6: error: cannot find symbol
Protection ob1 = new Protection();
                     ^
  symbol:   class Protection
  location: class Demo
2 errors
error: compilation failed
Arpit
  • 394
  • 1
  • 11
  • I complied `Protection.java` it is not giving any error – Arpit Aug 31 '20 at 09:06
  • It doesn't matter which file you compile first. One, containing another, makes compiler compile that another as well. – Giorgi Tsiklauri Aug 31 '20 at 09:08
  • You tried 'java Demo.java', it should be 'javac Demo.java' – Stultuske Aug 31 '20 at 09:08
  • Do you have both files in the same directory? Also, how you compile and how your run? Can you include this in your question as well? – Giorgi Tsiklauri Aug 31 '20 at 09:09
  • javac was also giving the same error then i tried to execute it this way – Arpit Aug 31 '20 at 09:10
  • I think the point is that you run the class file, not the source file. And you run it from one level above it, using full qualified name. – RealSkeptic Aug 31 '20 at 09:10
  • Once again: **Can you include how you compile the files, and how you run the program, in your question, as well?** also **are both files in the same directory?** – Giorgi Tsiklauri Aug 31 '20 at 09:12
  • @GiorgiTsiklauri he executes the java file directly with `java`, without compiling them. It's allowed by Java as long as you don't use any other custom classes. But here the OP uses other custom classes, so running it with only `java` doesn't work anymore. – Olivier Grégoire Aug 31 '20 at 09:15
  • Unrelated: consider using a different tutorial then. A) the java source violates java naming conventions (so the author doesnt know or care about them, which is bad). And B) a good tutorial exactly tells you all the important details about getting javac and java to work nicely. It seems your tutorial or book is missing that part ... so, consider to use something else. – GhostCat Aug 31 '20 at 09:16
  • @OlivierGrégoire what makes you think so? Pay attention, that he has *$ javac Demo.java* giving the error in the log.. and he said, that he uses `javac`. Also, in the later builds of JDK, you can run the program right with `java.exe`, which would implicitly compile sources. That's why I ask for more information in the question. – Giorgi Tsiklauri Aug 31 '20 at 09:17
  • @GhostCat or the OP didn't include this information. – Giorgi Tsiklauri Aug 31 '20 at 09:20
  • @GiorgiTsiklauri OP changed `java` to `javac` later on: check the post history. Now OP has the issue because he didn't put them in the `p1` directory. – Olivier Grégoire Aug 31 '20 at 09:28

2 Answers2

1

You should use javac, not java only

When you use the command java, you can execute a file, but only the classes in that file. Here you have several files, so you should compile them in order to use them.

Do the following:

$ mkdir p1
$ mv Demo.java Protection.java p1/
# edit p1/Demo.java to change `class Demo` to `public class Demo`
$ javac p1/*
$ java p1.Demo

This worked and resulted in the following:

base constructor
n = 1
n_pri = 2
n_pro = 3
n_pub = 4
Olivier Grégoire
  • 33,839
  • 23
  • 96
  • 137
  • @Arpit Have you moved your files in the `p1` directory? Because Java requires that files are in the directory corresponding to their package. – Olivier Grégoire Aug 31 '20 at 09:11
  • @Arpit See my changes – Olivier Grégoire Aug 31 '20 at 09:17
  • @Arpit follow the exact steps I provided, it will work. – Olivier Grégoire Aug 31 '20 at 09:31
  • It worked thanks. But can u please tell why we cannot move to the p1 directory and then compile them. like `cd p1` and then `javac Protection.java` – Arpit Aug 31 '20 at 09:43
  • 1
    Because Java sees the source code as a whole, not as a set of singular units. So the whole source code must be consistent. The java file has a reference to its package, so it basically says that the parent must be named like that, but Java can't see the parent if the parent isn't visible in the file hierarchy we give it, so must always compile from the root. When you'll work with IDEs, you'll see that they handle this for you so that you don't have to manage it yourself. It's a pain until you get to use IDEs. – Olivier Grégoire Aug 31 '20 at 09:51
0

You could try this:

  1. open cmd in p1, use javac .\Demo.java .\Protection.java; then you can see two .class files generated
  2. use cd .., then you can see your package p1
  3. use java p1.Demo then you can see the expected output