-1

I'm trying to use a class that I've created that is within a java archieve, this is my directory structure: enter image description here
and the directory where all of this is located is the: /home/user/learning_java, Now I have already seen this question: using classes in java archieve, but for some reason this didn't solve my problem(so this is not a duplicate).

I am only using the text editor vscode and hence only compile from the terminal(am on ubuntu).

Please ignore the .class files, The constructor.java file has the source code:

    package com.beez.java;

public class constructor {
   private String name;
   private int age;
   private int weight;

   public constructor(String var1, int var2) {
      this.name = var1;
      this.age = var2;
      this.weight();
      this.weight = this.get_weight();
   }

   public void weight() {
      double var1 = 5.13D;
      this.weight = (int)((double)this.age * var1 + 3.0D);
   }

   public int get_weight() {
      return this.weight;
   }

   public String get_name() {
      return this.name;
   }

   public int get_age() {
      return this.age;
   }
}

and the run_test.java has the source code:

import com.beez.java.construc.constructor;
import static java.lang.System.*;

public class run_test{

    public static void main(String[] args){
        constructor niraj = new constructor("niraj", 12);
        int niraj_weight = niraj.get_weight();
        out.println("weight of niraj: "+niraj_weight);

    }
}

and I compile in the terminal from the directory: /home/user/learning_java which is where the run_test.java resides using:

javac -classpath '.:com.beez.java.construc.jar' run_test.java  

and this is the error I get:

run_test.java:1: error: package com.beez.java.construc does not exist
import com.beez.java.construc.constructor;
                             ^
run_test.java:7: error: cannot find symbol
        constructor niraj = new constructor("niraj", 12);
        ^
  symbol:   class constructor
  location: class run_test
run_test.java:7: error: cannot find symbol
        constructor niraj = new constructor("niraj", 12);
                                ^
  symbol:   class constructor
  location: class run_test
3 errors

What is the error I'm making?

juztcode
  • 1,196
  • 2
  • 21
  • 46
  • Read the error message. What is the name of the package that it says it cannot find? Read your `constructor.java` file. What is the name of the package you are declaring? Are those two names the same? – Jörg W Mittag Jul 14 '19 at 12:54
  • in the classpath you specify location. so try `'.:com/beez/java/constructor.jar'` – Sharon Ben Asher Jul 14 '19 at 12:54
  • @JörgWMittag , I changed the import to `import com.beez.java.constructor`, but nothing changed. – juztcode Jul 14 '19 at 13:22
  • @SharonBenAsher , that tells me now that construc.jar is a bad class file – juztcode Jul 14 '19 at 13:45
  • @juztcode, so perhaps it is bad indeed. check is there were errors when you built the jar file – Sharon Ben Asher Jul 14 '19 at 13:51
  • @SharonBenAsher, no, there were not any errors, all it says is that the ` class file contains wrong class: com.beez.java.constructor Please remove or make sure it appears in the correct subdirectory of the classpath` – juztcode Jul 14 '19 at 13:58
  • @juztcode why do you have jar file and class file? you should have only one, not both – Sharon Ben Asher Jul 14 '19 at 14:46
  • @SharonBenAsher , I've deleted the .class file, but it doesn't change a thing and, you see, I'm just in the phase of learning core java, so, I'm doing all these experiments to learn the language. – juztcode Jul 14 '19 at 14:47
  • "I changed the import to `import com.beez.java.constructor`, but nothing changed" – Then you didn't recompile, or you recompiled the wrong thing, or you are running an old version of your code, or something along that lines. Since you are now importing from a different package, *at least* the name of the package in the error message *must* have changed. – Jörg W Mittag Jul 14 '19 at 15:04
  • @JörgWMittag, yes actually that did change, it used to say `com.beez.java.construc` doesn't exist and now it's saying `com.beez.java` doesn't exist, the jar file's constructor.java successfully compiled to a .class so, that doesn't have to be recompiled? and repacked into the jar? that's not changed at all and it's 100% correct(I mean the code is so simple), and the `run_test.java`, as you can see is doing just this. – juztcode Jul 14 '19 at 15:11

1 Answers1

1

in main class you are importing wrong class. The mian class is importing 'com.beez.java.construc.constructor' while you are declaring the package as 'com.beez.java' as package declartion statement. Either correct your import in run_test class or move your constructor class in 'com.beez.java.construc' package.

Dinesh
  • 1,046
  • 1
  • 8
  • 17
  • I changed the import to: `import com.beez.java.constructor`, but the error didn't change at all. – juztcode Jul 14 '19 at 13:24
  • and the `construc` was suppose to be the jar file name, since the .class file was called constructor inside the jar file, wasn't that correct, if not what should the import be? – juztcode Jul 14 '19 at 13:26