如何使用Javac将类导入java程序?

时间:2017-10-21 09:53:42

标签: java linux javac

简介

我使用终端和Javac来编译和运行我的java程序。个人计划工作正常。但是,当我想将一个类导入到我的主Java程序中时,我会收到错误。

源代码 - 我的班级(Name.java)

public class Name {

//Fields
private String firstName;
private String familyName;


//Constructors
public Name() {
    firstName = "";
    familyName = "";
}

public Name(String firstName, String familyName) {
    this.firstName = firstName;
    this.familyName = familyName;
}


//Methods
public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public void setFamilyName(String familyName) {
    this.familyName = familyName;
}

public String getFirstName() {
    return firstName;
}

public String getFamilyName() {
    return familyName;
}

public String getFullName() {
    if (firstName.equals("") && familyName.equals("")) {
        return "";
    } else {
        return firstName + " " + familyName;
    }
}

@Override
public String toString() {
    return "Name:[firstName=" + firstName + ", familyName=" + familyName + "]";
}
}

Main - NameDemo.java

import Name;

public class NameDemo {

public static void main(String[] args) {
    /* 
     * See below evidence of how arrays can store elements of a custom reference
     * type that you create (Name in this case), as well as both existing
     * reference types (e.g. String) or primitive types (e.g. int), that you have
     * previously worked with.
     */

    //declare array to hold four Name objects
    Name[] friends = new Name[4];

    //create Name object and assign reference into array
    Name joe = new Name("Joe", "Bloggs");
    friends[0] = joe;

    //create Name objects and assign reference directly into array
    friends[1] = new Name("Fred", "Perry");
    friends[2] = new Name("Diti", "Hammond");
    friends[3] = new Name("Bulsar", "Raymond");

    //loop through each element
    for(int i = 0; i < friends.length; i++) {
        //use accessor method to output full name of each
        System.out.println(friends[i].getFullName());
    }

    //declare boolean used as a flag
    boolean exists = false;

    //loop through each Name object using a for-each loop (you will see more on this when we cover ArrayLists)
    for (Name nm : friends) {
        //check family name
        if (nm.getFamilyName().equals("Smith")) {
            exists = true;
        }
    }

    //output result of search
    if (exists) {
        System.out.println("There is a person with family name of Smith");
    } else {
        System.out.println("There is not a person with family name of Smith");
    }

    //Checking output of toString
    System.out.println("\n" + joe.toString());

}

}

问题

两个文件都在同一目录中。除此之外,我已将该类导入主程序源代码中。然后我继续尝试编译它们。

  

导入名称;

javac Name.java
javac NameDemo.java
NameDemo.java:4: error: '.' expected
import Name;
           ^
NameDemo.java:4: error: ';' expected
import Name;
            ^
2 errors

为什么会发生这种情况,我应该编译课程吗?或者类需要以.class还是.java结尾?以及这是导入的语法问题?

2 个答案:

答案 0 :(得分:2)

所以:

  1. 您应该下载 IDE ,我建议您使用Eclipse。这将有助于您稍后编程。
  2. 您应该将 EVERY 文件的名称传递给javac。这样javac就会知道类#2中的方法存在而且不需要拯救。
  3. NO 需要导入 - 文件在同一个包中 - 也就是说,在同一目录中。 Javac希望您以这种格式传递内容:import packagename.classname;,而您只提供classname
  4. 作为编译字节码的类需要具有扩展名 .class ,而像您编写的源代码中的类需要具有扩展名 .java

答案 1 :(得分:-1)

尝试使用以下命令。在文件所在的同一目录中。

javac * .java