简体   繁体   中英

Compiling multiple files in Java using TextPad

I'm trying to compile my HelloApp2 file using TextPad, but it references another class in a different file in the greeter class. I keep getting errors that say that Greeter is not recognized. DOes anybody know how to do this in TextPad?

HelloApp2.java:

public class HelloApp2
{
public static void main(String[] args)
{
    Greeter myGreeterObject = new Greeter();
    myGreeterObject.sayHello();
}
}

Greeter.java:

public class Greeter
{
public void sayHello()
{
    System.out.println("Hello, World!");
}
}

If HelloApp2 references Greeter , you need to put an import Greeter into the top of HelloApp2.java .

import Greeter

public class HelloApp2
{
    public static void main(String[] args)
    {
        Greeter myGreeterObject = new Greeter();
        myGreeterObject.sayHello();
    }
}

See the java docs about importing packages .

This has nothing to do with TextPad... I would highly recommend using an IDE like Eclipse/NetBeans/IntelliJ Community Edition which are free and will save you loads of time writing, building, and debugging your Java software.

You need to import that class

import Greeter.*;

only if you have that file in the same folder where your main program is located

or else you have to specify the package details like

import package.Greeter.*;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM