-1

I want to create two packages: the one containing problem-specific classes and methods and the one containing some typical mathematical apparatus. I will denote them "maxim.main_package" and "maxim.algebra". For some reason I can't import the algebra package to the main one. Please, help.

As the algebra package does not rely on the main one, I have successfully compiled the .java file into a .class via the command line. The .java file starts with "package maxim.algebra;". Now I want to compile the main .java file which starts with "package maxim.main;" followed by "import algebra.*;" The paths to the .java files with are resp.:

1) maxim\main\main.java
2) maxim\algebra\algebra.java
I run "javac maxim\main\main.java" command from maxim directory. 
It fails saying:

    `error: cannot access Algebra
    Algebra.matrix3x3 R = Algebra.some_method(parameters)
    bad class file: .\algebra\Algebra.class
    class file contains wrong class: maxim.algebra.Algebra`
  • Possible duplicate of [What is a classpath and how do I set it?](https://stackoverflow.com/questions/2396493/what-is-a-classpath-and-how-do-i-set-it) –  Aug 14 '19 at 09:20
  • 1
    A file containing the class `Algebra` **must** be named `Algebra.java`. –  Aug 14 '19 at 09:20
  • I believe you should also import `maxim.algebra.*`, not `algebra.*` – Roger Gustavsson Aug 14 '19 at 09:30
  • Lutz, the file is named Algebra.java. Roger, no, in fact if I import maxim.algebra.*, the package and its classes are not found at all. With import algebra.* the class is seen alright. – Maxime B.jac Aug 14 '19 at 09:39
  • There's some inconsistency here. You say you run `javac maxim\main\main.java` from `maxim` directory. That shouldn't work. Did you mean `javac main\main.java` with `maxim` as current directory? If so, step up one directory, import `maxim.algebra.*` in main.java and run `javac maxim\main\main.java`. – Roger Gustavsson Aug 14 '19 at 10:31

1 Answers1

0

Package names and the directory structure needs to match. Case matters. The packages then also needs to be available on the classpath. By default, the current directory is in the classpath. But the classpath can be changed with the environment variable CLASSPATH. If this variable is defined and . (current directory) is not in it, current directory will not be in the classpath. The classpath can also be set with the parameter -classpath or -cp for short. See also Setting the Class Path.

Here's a short guide on how to put classes in packages and import classes from different packages:

You have two packages, maxim.main and maxim.algebra. We start off in the current directory, where we create two directories named after the package names, maxim\main and maxim\algebra.

> mkdir maxim\main maxim\algebra
> dir
2019-08-14  12:39    <DIR>          .
2019-08-14  12:39    <DIR>          ..
2019-08-14  12:39    <DIR>          maxim

> dir maxim
2019-08-14  12:39    <DIR>          .
2019-08-14  12:39    <DIR>          ..
2019-08-14  12:41    <DIR>          algebra
2019-08-14  12:46    <DIR>          main

>

We create two classes, Main.java with the filename maxim\main\Main.java and the following content:

package maxim.main;

import maxim.algebra.Algebra;

public class Main {
    public static void main(String... args) {
        Algebra algebra = new Algebra();
        System.out.println(algebra.sayHello() + " World");
    }
}

and Algebra.java with the filename maxim\algebra\Algebra.java and the following content:

package maxim.algebra;

public class Algebra {
    public String sayHello() {
        return "Hello";
    }
}

Without having changed active directory, we compile the two classes, Algebra first:

> javac -cp . maxim\algebra\Algebra.java maxim\main\Main.java

>

We specify -cp . to override any CLASSPATH environment variable that may be set.

After the compile the directory structure looks like this:

.\
  maxim\
    main\
      Main.class
      Main.java
    algebra\
      Algebra.class
      Algebra.java

Now we can run the Main class:

> java -cp . maxim.main.Main
Hello World

>
Roger Gustavsson
  • 1,689
  • 10
  • 20