-2
package pkg1;
    public class demoFile1 {

            private int maze = 5;
            public demoFile1() {}
            public demoFile1 (int maze) {

                    this.maze = maze;
                    System.out.println(this.maze);

            }
    }

package pkg2;
import pkg1.*;
    public class demoFile2 {
            public static void main (String[] args) {

                    demoFile1 obj = new demoFile1 (10);

            }
    }

Here, I have created two packages pkg1 and pkg2. In pkg1, there is a class and another class inside pkg2 which imports the pkg1 to initailize the former class, hence it needs to import the .class from pkg1, this produces an error when tried to call from within this sub-directory:

error: package pkg1 does not exist

Please note that the program worked just fine when the pkg1.demoFile1.class file was imported by demoFile2.class from outside that sub-directory (different levels, without confining under pkg2) but, not when both these packages were on the same level, each having source code files and .class files i.e., when pkg2.demoFile2.class tries to import pkg1.demoFile1.class

Edit#1 : Folder Structure: click here

What I feel is that there may have been some issue with hierarchical ordering/precedence while creating or importing the .class file from the package OR the overall definition(s) would have been written wrong. Please help.

Sambhav Jain
  • 7
  • 1
  • 5
  • You get that error when doing what? Be as precise as you can. And post the folder/file structure. – JB Nizet May 14 '17 at 11:16
  • What do you mean? I tested above code and it compiled and executed successfully. – Abolfazl Hashemi May 14 '17 at 11:21
  • I would imagine OP gets the error when trying to compile the program from within `src/pkg2`. If so, please try compiling from `src` or specify `..` as sourcepath. – Turing85 May 14 '17 at 11:22
  • uploaded the folder structure, don't know why they embedded link instead the actual image – Sambhav Jain May 14 '17 at 11:53
  • Sigh. You get that error when doing what? Be as precise as you can. – JB Nizet May 14 '17 at 11:55
  • tried importing the files from package `pkg1` into the class defined inside package `pkg2`, it produces the error shown above, see the folder structure for details – Sambhav Jain May 14 '17 at 12:02
  • @SambhavJain when doing what? How do you compile the code? That's where the problem is: **you** incorrectly compiling the files. But you refuse to explain how you're doing it. So we can't explain what you're doing wrong. And we won't give a general explanation, because there are plenty of them onSO, and in the Java tutorials, etc. – JB Nizet May 14 '17 at 12:07
  • `~/pkg2$ javac demoFile2.java` this is what I compiled and where error occurred – Sambhav Jain May 14 '17 at 12:43
  • I have created same scenario you explained .It is working fine. Are you using eclipse or some other tool for coding.[![enter image description here](https://i.stack.imgur.com/q05IB.png)](https://i.stack.imgur.com/q05IB.png) – gati sahu May 14 '17 at 11:17
  • Classpath problem. Study classpath. – Lew Bloch May 14 '17 at 15:38

1 Answers1

0

Most likely you have a classpath problem. Class files should be placed inside folders that match the java packages declaration. The root of the folder structure may start at each of the folders/jars present in your classpath (edit: your folder structure is correct).

If you run

java pkg2.demoFile2

by default a "current directory" classpath is used (the current directory is called "."). So demoFile2.class must be inside the pkg2 folder and demoFile1.class inside the pkg2.class folder and you must execute the java program from the folder containing both the package folders.

This folder structure is usually handled by the java editor. So I suppose you are running "javac" from the command line. In this case specify an output folder so that it can create the required folder structure there:

javac -d . file1.java file2.java

If you want to compile one file at a time you need to tell javac where it can find the previously compiled classes, again using the class path (again run this from the root folder):

javac -d . -cp . file2.java

You can do all of this from any folder you want specifiying the classpath correctly (as an absolute or relative path). Running all the commands from the root folder is the simplest option.

In java there are no ordering/precedence issues in classloading or in any other context I can think of right now.

Community
  • 1
  • 1
lorenzo
  • 556
  • 5
  • 13