使用Java档案文件中的类

时间:2019-07-14 12:50:27

标签: java jar

我正在尝试使用在Java档案库中创建的类,这是我的目录结构: enter image description here
所有这些文件所在的目录是:/home/user/learning_java, 现在我已经看到了这个问题:using classes in java archieve,但是由于某种原因,这并不能解决我的问题(因此这不是重复的)。

我仅使用文本编辑器 vscode ,因此只能从终端(ubuntu上的AM)进行编译。

请忽略.class文件, constructor.java文件具有源代码:

    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;
   }
}

,而run_test.java具有源代码:

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);

    }
}

然后我从目录/home/user/learning_java在终端中进行编译,该目录是run_test.java所在的目录,使用:

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

这是我得到的错误:

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

我正在犯什么错误?

1 个答案:

答案 0 :(得分:1)

在主类中,您正在导入错误的类。当您将软件包声明为“ com.beez.java”作为软件包声明语句时,mian类将导入“ com.beez.java.construc.constructor”。要么在run_test类中更正导入,要么在“ com.beez.java.construc”包中移动构造函数类。

相关问题